Get json data in ListView using AsyncTask

I would like to display some data from my server in my ListView using asyncTask . Connection and JsonParser work fine, but I have an error in my OnPostExecute() when I try to put my data in a ListView .

So this is the class that calls asyncTask :

 public class GetInfo extends Activity{ ListView listeView; protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.listdata); listeView = (ListView) findViewById(R.id.maListeView); GetJson getJson = new GetJson(GetInfo.this); getJson.execute(); } } 

There is no problem. This is my asyncTask class:

 class GetJson extends AsyncTask<Void, Integer, ArrayList<String>> { private Context Mycontext; private ListView listview; private ArrayAdapter<String> arrayadapter; private ProgressDialog pDialog; private ArrayList<String> donnees; private ListActivity listAct; public GetJson(GetInfo getInfo) { Mycontext = getInfo; } @Override protected void onPreExecute() { // Showing progress dialog before sending http request this.pDialog = new ProgressDialog(Mycontext); this.pDialog.setMessage("Please wait.."); this.pDialog.setIndeterminate(true); this.pDialog.setCancelable(false); this.pDialog.show(); } @Override protected ArrayList<String> doInBackground(Void... arg0) { String result = null; InputStream is = null; JSONObject json_data=null; ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); this.donnees = new ArrayList<String>(); try{ //commmand http HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://192.168.0.100/timesync.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch(Exception e){ Log.i("taghttppost",""+e.toString()); } //parse response try { BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch(Exception e) { Log.i("tagconvertstr",""+e.toString()); } //get json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++) { json_data = jArray.getJSONObject(i); this.donnees.add("date :"+ json_data.getString("time") + " ou " + json_data.getString("date") + json_data.getString("hour")); } } catch(JSONException e){ Log.i("tagjsonexp",""+e.toString()); } catch (ParseException e) { Log.i("tagjsonpars",""+e.toString()); } return this.donnees; } @Override protected void onPostExecute(ArrayList<String> donnees) { super.onPostExecute(donnees); this.pDialog.dismiss(); for(int i=0;i<donnees.size();i++) { Log.i("time", donnees.get(i)); } // this.arrayadapter = new ArrayAdapter<String>(Mycontext, android.R.layout.simple_list_item_1, this.donnees); // this.listview.setAdapter(this.arrayadapter); } } 

My problem is the commented lines:

 // this.arrayadapter = new ArrayAdapter<String>(Mycontext, android.R.layout.simple_list_item_1, this.donnees); // this.listview.setAdapter(this.arrayadapter); 

and I don’t understand why ...

That's my fault:

 11-05 16:43:36.560: E/AndroidRuntime(11200): FATAL EXCEPTION: main 11-05 16:43:36.560: E/AndroidRuntime(11200): java.lang.NullPointerException 11-05 16:43:36.560: E/AndroidRuntime(11200):at com.json.GetJson.onPostExecute(Adapter.java:134) 11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask.finish(AsyncTask.java:631) 11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask.access$600(AsyncTask.java:177) 11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) . . . 

The result is logged, so I'm sure my code is passed to OnPostExecute() .

0
source share
5 answers

The problem is that your list is not initialized.

Step 1) Update the AsyncTask constructor:

 public GetJson(GetInfo getInfo, ListView list) { Mycontext = getInfo; listView = list; } 

Step 2) Do it in OnCreate

 GetJson getJson = new GetJson(GetInfo.this, listeView); getJson.execute(); 
0
source

Remove the line private ListView listview; from the GetJson class. The problem is that you are accessing the listview object of the GetJson class (which isnt initialized) instead of the Activity's listview object.

Get access to the list from GetInfo Activity just like

 listview.setAdapter(this.arrayadapter); 
0
source

ListView listview not initialized in GetJSON . Therefore, you get a NullPointerException .

You probably want to make asynctask the inner class of your activity and remove the listview declaration in GetJSON , since if you make it an inner class, it is already declared as a class member in GetInfo .

listeView = (ListView) findViewById(R.id.maListeView) is in GetInfo .

Or use the interface as a callback and set listview to your activity class like

How to return boolean value from AsyncTask?

Instead of boolean in the example, its ArrayList<String> donnees in your case.

0
source

Which ListView are you trying to use? One in the GetInfo class or one in the GetJson class?

The one you declared in the GetJson class must be created if you want to use it as

 listeView = (ListView) findViewById(R.id.xxxx); 
0
source

If you are processing any user interface actions in PostExecute Menthod, try the following:

 Handler refresh = new Handler(Looper.getMainLooper()); refresh.post(new Runnable() { public void run( ){ // try UI here . }); 
0
source

Source: https://habr.com/ru/post/1261836/


All Articles