android - arrayList becomes null after response in jsonArrayRequest -


error: in backgroundtask data in arraylist in try block,but after when reach after errorlistener(), arraylist becomes null. in logcat found these problem.how solve problem..??

package com.example.rahul.volley_jarray;  import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log;  import java.util.arraylist;  public class displaylist extends appcompatactivity {      recyclerview recyclerview;     recyclerview.adapter adapter;     recyclerview.layoutmanager layoutmanager;     arraylist<contact> arraylist=new arraylist<>();     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_display_list);          recyclerview= (recyclerview) findviewbyid(r.id.recylerview);         layoutmanager=new linearlayoutmanager(this);         recyclerview.setlayoutmanager(layoutmanager);         recyclerview.sethasfixedsize(true);          backgroundtask backgroundtask=new backgroundtask(displaylist.this);         arraylist=backgroundtask.getlist();          log.d("dispaly array list",""+arraylist.tostring());          adapter=new recycleradapter(arraylist);         log.d("my adapter",arraylist.tostring());         recyclerview.setadapter(adapter);      } } 

error: @ place. backgroundtask

package com.example.rahul.volley_jarray;  import android.content.context; import android.util.log; import android.widget.toast;  import com.android.volley.request; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.jsonarrayrequest;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import java.util.arraylist;  /**  * created rahul on 7/4/2016.  */ public class backgroundtask {      context context;     arraylist<contact> arraylist=new arraylist<>();     string str_url="https://raw.githubusercontent.com/ianbar20/json-volley-tutorial/master/example-json-files/example-array.json";     string data="";     public backgroundtask(context context)     {         this.context=context;     }     public arraylist<contact> getlist()     {         final jsonarrayrequest jsonarrayrequest=new jsonarrayrequest(request.method.get, str_url, (string) null, new response.listener<jsonarray>() {             @override             public void onresponse(jsonarray response) {                  log.d("my json",response.tostring());                       int count = 0;                     while (count < response.length()) {                         try {                             jsonobject jsonobject = response.getjsonobject(count);                             jsonobject jsonobject1 = jsonobject.getjsonobject("phone");                             contact contact = new contact(jsonobject.getstring("name"), jsonobject.getstring("email"), jsonobject1.getstring("home"));                             log.d("contact", "" + contact.tostring());                             arraylist.add(contact);                             log.d("arraylist" + count, "" + arraylist.tostring());                             count++;                           } catch (jsonexception e) {                             e.printstacktrace();                             log.d("mytag", e.tostring());                         }                         log.d("arraylist in while" + count, "" + arraylist.tostring());                          log.d("arraylist2" + count, "" + arraylist.tostring());                         if (arraylist.tostring() == null) {                             log.d("if first", "null");                         } else {                             log.d("else first", "not null");                         }                      }             }         },          new response.errorlistener() {             @override             public void onerrorresponse(volleyerror error) {                  toast.maketext(context,"error...!!!",toast.length_short).show();                 error.printstacktrace();             }         });          log.d("final arraylist",""+arraylist.tostring());         //return arraylist;         if(arraylist.tostring()==null)         {             log.d("if second","null");         }         else {             log.d("else second","not null");         }          log.d("jsonarray request",""+jsonarrayrequest.tostring());          mysingleton.getinstance(context).addtorequestqueue(jsonarrayrequest); //         return arraylist;     } } 

//mysinglton class package com.example.rahul.volley_jarray;

import android.content.context; import android.util.log;  import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.toolbox.volley;  /**  * created rahul on 7/4/2016.  */ public class mysingleton {      private static mysingleton minstance;     private static context mctx;     private requestqueue requestqueue;      private mysingleton(context context) {         mctx = context;         requestqueue = getrequestqueue();         log.d("request queue", "" + requestqueue.tostring());     }      public requestqueue getrequestqueue() {         if (requestqueue == null) {             requestqueue = volley.newrequestqueue(mctx.getapplicationcontext());         }         return requestqueue;     }      public static synchronized mysingleton getinstance(context context) {         if (minstance == null) {             minstance = new mysingleton(context);         }         log.d("minstaces", "" + minstance);         return minstance;     }      public <t> void addtorequestqueue(request<t> request) {         log.d("request",""+request.tostring());         requestqueue.add(request);         log.d("now request queue",""+requestqueue.tostring());     } } 

volley async requests. when check arraylist hasn't finished yet (actually it's not in queue yet), that's why null (actually should empty since init in field already, not null).

you can use custom listener, pass request class , call onresponse.

some pseudo code clarify:

//define interface in background task , implement in caller activity public interface customreqfinished(){     public void oncustomreqfinished(arraylist list){} }  //in backgroundtask constructor store activity pass follows listener = (customreqfinished) activity; //set activity param instead of context //in onresponse last step listener.oncustomreqfinished(arraylist); 

edit clarify requested. in displaylist modify follows:

public class displaylist extends appcompatactivity implements customreqfinished {      recyclerview recyclerview;     recyclerview.adapter adapter;     recyclerview.layoutmanager layoutmanager;     arraylist<contact> arraylist=new arraylist<>();     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_display_list);          recyclerview= (recyclerview) findviewbyid(r.id.recylerview);         layoutmanager=new linearlayoutmanager(this);         recyclerview.setlayoutmanager(layoutmanager);         recyclerview.sethasfixedsize(true);          backgroundtask backgroundtask=new backgroundtask(this);         backgroundtask.getlist();     }      @override     public void oncustomreqfinished(arraylist<contact> list) {         log.d("dispaly array list",""+list.tostring());          adapter=new recycleradapter(list);         recyclerview.setadapter(adapter);     } } 

in backgroundtask modify follows:

public class backgroundtask {      public interface customreqfinished{         public void oncustomreqfinished(arraylist<contact> list);     }      context context;     customreqfinished listener;     arraylist<contact> arraylist=new arraylist<>();     string str_url="https://raw.githubusercontent.com/ianbar20/json-volley-tutorial/master/example-json-files/example-array.json";     string data="";     public backgroundtask(activity activity)     {         this.context = activity;         this.listener = (customreqfinished) activity;      }     public void getlist()     {         final jsonarrayrequest jsonarrayrequest=new jsonarrayrequest(str_url, new response.listener<jsonarray>() {             @override             public void onresponse(jsonarray response) {                  log.d("my json",response.tostring());                       int count = 0;                     while (count < response.length()) {                         try {                             jsonobject jsonobject = response.getjsonobject(count);                             jsonobject jsonobject1 = jsonobject.getjsonobject("phone");                             contact contact = new contact(jsonobject.getstring("name"), jsonobject.getstring("email"), jsonobject1.getstring("home"));                             log.d("contact", "" + contact.tostring());                             arraylist.add(contact);                             log.d("arraylist" + count, "" + arraylist.tostring());                             count++;                           } catch (jsonexception e) {                             e.printstacktrace();                         }                     }                      listener.oncustomreqfinished(arraylist);             }         },          new response.errorlistener() {             @override             public void onerrorresponse(volleyerror error) {                  toast.maketext(context,"error...!!!",toast.length_short).show();                 error.printstacktrace();             }         });          mysingleton.getinstance(context).addtorequestqueue(jsonarrayrequest);     }     } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -