There is a code that ensures data retrieval from a WCF service. It works, a JSON string is obtained from the service, which is parsed and displayed on the screen.
I want to fill the class with data from this line, and use it further, outside the AsynkTask block, but I cannot “pull” it out of this block. How to do it?
public class MainActivity extends ActionBarActivity { public static String LOG_TAG = "my_log"; AutoCompleteTextView txtSearch; List<Fruit> mList; FruitAdapter adapter; public List<Fruit> retrievePeople;//массив, который надо возвращать @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new ParseTask1().execute(); mList = retrievePeople; } public void BTN_click(View view) { new ParseTask1().execute(); } //код для чтения из WCF private class ParseTask1 extends AsyncTask<Void, Void, String> { HttpURLConnection urlConnection = null; BufferedReader reader = null; String resultJson = ""; @Override protected String doInBackground(Void... params) { try { URL url = new URL("http://192.168.1.94:8080/Test.svc/jsonaray"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } resultJson = buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return resultJson; } //получаем json-массив, распарсиваем его и читаем @Override protected void onPostExecute(String resultJson){ super.onPostExecute(resultJson); Log.d(LOG_TAG, resultJson); JSONObject dataJsonObj = null; String Name = ""; try { String resj=resultJson.substring(1,resultJson.length()-1); resj=resj.replace("\\",""); dataJsonObj = new JSONObject(resj); JSONArray arr = dataJsonObj.getJSONArray("Prod"); // 1. достаем инфо о элементе с индексом 1 JSONObject secondFriend = arr.getJSONObject(3); Name = secondFriend.getString("Name"); Log.d(LOG_TAG, "Название: " + Name); // 2. здесь идет заполнение массива, который надо вернуть for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); String _name = obj.getString("Name"); String _expiry = obj.getString("Expiry"); retrievePeople = new ArrayList<Fruit>(); retrievePeople.add(new Fruit(_name,_expiry)); Log.d(LOG_TAG, "name: " + _name); Log.d(LOG_TAG, "data: " + _expiry); } EditText editText =(EditText)findViewById(R.id.editText); editText.setText(Name); } catch (JSONException e) { e.printStackTrace(); } } } }
retrievePeople, but this is not an array at all . You declaredretrievePeopleas a class field (more precisely, not a class, but a class object)MainActivitywith thepublicmodifier, therefore it is visible everywhere. - post_zeewretrievePeople = new ArrayList<Fruit>();should be performed at least before the cycle, because at each iteration of the cycle you create a new object, which eventually will contain only (one) last element. - post_zeewFruit[] retrievePeople;, you have theList<Fruit> retrievePeople, and more specifically theArrayListis a list. Here it ismList = retrievePeopleimmediately after the launch of asynstask, and it is quite possible (even likely) that at this momentretrievePeopleis onlynull. You start a new stream, well. And these two threads are executed asynchronously . - post_zeewmList = retrievePeople;You can register at the end of theonPostExecute(...)method, then get what you want (although I don’t understand the meaning of these cunning manipulations at all). - post_zeew