android - Getting volley response is null on first call -


this class network calls. here executing method networkcallbyvolley saving information on shared preferences.

 public class networkcall extends activity {         context context;         string res = "something";          sharedpreferences userdetails;            arraylist<string> type = new arraylist<string>();          arraylist<string> value = new arraylist<string>();       public networkcall(context context){         this.context = context;       }          public void networkcallbyvolley(final arraylist<string> type, final arraylist<string> value){              this.type = type;             this.value = value;             log.i("type", type.tostring());             log.i("value", value.tostring());                requestqueue queue = volley.newrequestqueue(context);             stringrequest myreq = new stringrequest(method.post,                     "http://my url",                     new response.listener<string>() {                          @override                         public void onresponse(string response) {                                 log.i("rrrrr", response);                              res = response;                               userdetails = context.getsharedpreferences("userdetails", mode_private);                             editor edit = userdetails.edit();                             edit.clear();                             edit.putstring("response", response);                              edit.commit();                              //log.i("rrrrr", response);                            }                     },                     new response.errorlistener() {                          @override                         public void onerrorresponse(volleyerror error) {                           }                     }) {                 protected map<string, string> getparams() throws com.android.volley.authfailureerror {                     map<string, string> params = new hashmap<string, string>();                   //for(int = 0; i<= params1.size(); i++)                      for(int =0 ; i< type.size(); i++){                     params.put(type.get(i), value.get(i));                     //params.put("password", "aaaaaa");                    log.i("typpppp", type.get(i));                     }                       return params;                 };             };              queue.add(myreq);             }          public string getcharlie(){              userdetails = context.getsharedpreferences("userdetails", mode_private);              return userdetails.getstring("response", "no value found");          }         public void clearcharlie(){             sharedpreferences.editor edit = userdetails.edit();             edit.clear();             edit.commit();         }          } 

when trying use class login activity getting message below log "pref response" "no value found". if run again getting proper response expecting. don't know how fix bug. appreciated. main activity

public class login extends activity {     button signup,submit;     edittext email,password; protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);      setcontentview(r.layout.login);   final  networkcall net = new networkcall(getapplicationcontext());      final arraylist<string> type = new arraylist<string>();        final arraylist<string> value = new arraylist<string>();       submit = (button) findviewbyid(r.id.submit);     email = (edittext) findviewbyid(r.id.edittext_email);     password = (edittext) findviewbyid(r.id.edittext_pwd);      type.add("user_email");     type.add("user_password");      value.add(email.gettext().tostring().trim());     value.add(password.gettext().tostring().trim());       submit.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {          net.networkcallbyvolley(type, value);          string response = net.getcharlie();        log.i("pref resonse", response);          }     });     } } 

you need use async task or handler. doing calling getcharlie() method right after made call net.networkcallbyvolley(type, value); takes time response sever after write response shared prefrences. getting no result found because @ time there no response. respone being calculated on parallel thread. thread gets response show until having null. wait thread response using async task.

@override         public void onclick(view v) {          net.networkcallbyvolley(type, value);           string response = net.getcharlie();           while(response.equalsignorecase("no result found")){}           log.i("pref resonse", response);          } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -