android - How return response using okhttp library from class method in activity method -


i want response using enque process in okhttp did not exact solution here code activity code

 public void homefragmentdataimplementation() {  notification notification = new notification();  kprogresshud = notification.activityindicator(mactivity);  new thread(new runnable() {      @override      public void run() {          okhttprequest okhttprequest = new okhttprequest();          homeapiresponse = okhttprequest.getokhttprequest(urls.homescreen_api +utility.getdeviceid(mfragment.getcontext()));                     handler.sendemptymessage(1);      }}).start(); }  private handler handler = new handler(){      @override      public void handlemessage(message msg) {          if(msg.what==1) {               notification notification = new notification();              notification.scheduledismiss(kprogresshud);              swipecontainer.setrefreshing(false);              new jsonreadingmodel(mactivity,homeapiresponse);              homescreenbusinessadapter.notifydatasetchanged();          }      }  }; 

here okhttp class method getting response , response in onresponse method give me null in activity code

 okhttpclient client = new okhttpclient();     string mresponse;     public string getokhttprequest(string url) {         try {              request request = new request.builder()                     .url(url)                     .build();             client.newcall(request).enqueue(new callback() {                 @override                 public void onfailure(call call, ioexception e) {                  }                  @override                 public void onresponse(call call, response response) throws ioexception {                     mresponse=response.body().string();                   }             });              return mresponse;           } catch (exception e) {             e.printstacktrace();         }         return "";     } 

you returning mresponse before callback gets executed due async nature of callback.

option 1) make request sync

// instead of  client.newcall(request).enqueue( )  // use  string body = client.newcall(request).execute( ).body().tostring(); 

option 2) make method async , return when callback called

// instead of  public string getokhttprequest(string url) { }  // change method  public void getokhttprequest(string url) { }  // , have callback continue logic client.newcall(request).enqueue(new callback() {             @override             public void onfailure(call call, ioexception e) {                // todo handle error correctly, instead of try/catch             }              @override             public void onresponse(call call, response response) throws ioexception {                 mresponse=response.body().string();                  // todo call method uses response values             }         }); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -