android - enqueue multiple GET request when using nested retrofit 2.0 -


i using retrofit 2.0 make api calls nesting multiple requests. api's works fine individually. when nested retrofit, first request execute after when register second request it's not callback in enqueue method (i.e. it's directly returning null without inserting enqueue's inner methods onresponse, onfailure)

my code :-

public class main2activity extends appcompatactivity {     gson gson;     jsonobject jsonresult=null;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main2);         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);          gson=new gsonbuilder().create();         firstrequest(); //-- first retrofit request     }       private void firstrequest() {         retrofit retrofit=new retrofit.builder().baseurl(getresources().getstring(r.string.api_url)).addconverterfactory(gsonconverterfactory.create(gson)).build();         cityretailsapi service = retrofit.create(cityretailsapi.class);         call call_first= service.getmaincatflag();          call_first.enqueue(new callback() {             @override             public void onresponse(call call, response response) {                         log.d("mainactivity", "status code = " + response.code());                 if (response.issuccessful()){                     maincatflag result = (maincatflag) response.body(); //-- first request response                                         jsonobject json2nd = secondrequest(); //-- second request                 }             }              @override             public void onfailure(call call, throwable t) {                 log.d("mainactivity", "error");             }         });     }      private jsonobject secondrequest() {         try {                         retrofit retrofit=new retrofit.builder().baseurl(getresources().getstring(r.string.api_url)).addconverterfactory(gsonconverterfactory.create(gson)).build();             cityretailsapi service = retrofit.create(cityretailsapi.class);             call call_second= service.getmaincat();              call_second.enqueue(new callback() {                 @override                 public void onresponse(call call2, response response1) {                                         log.d("mainactivity", "status code = " + response1.code());                     if (response1.issuccessful()) {                                                 maincat result = (maincat) response1.body();                         if (result.getsuccess()==1)                         {                             try {                                 jsonresult= new jsonobject(new gson().tojson(result));                             } catch (jsonexception e) {                                 e.printstacktrace();                             }                         }                     }                 }                  @override                 public void onfailure(call call, throwable t) {                     log.d("mainactivity", "error");                 }             });         }catch (exception e){             log.d("mainactivity", "error= " + e);         }         return jsonresult;     } }  

in above code firstrequest() executed correctly , proving response secondrequest (inside firstrequest() enqueue method) not working fine. not showing error, success message in console. can 1 please me override problem.

if problem in code, please let me know. thank in advance.

you made mistake when using retrofit enquene,it's called asynchronously, can't result outside of callback method!

so, should process result inside onresponse method this:

private void secondrequest() {     try {                      call_second.enqueue(new callback() {             @override              public void onresponse(call call2, response response1) {                                     log.d("mainactivity", "status code = " + response1.code());                 if (response1.issuccessful()) {                                             maincat result = (maincat) response1.body();                     if (result.getsuccess()==1)                     {                          try {                              jsonresult= new jsonobject(new gson().tojson(result));                              // process jsonresult here                             ...                          } catch (jsonexception e) {                             e.printstacktrace();                         }                      }                  }              }               @override              public void onfailure(call call, throwable t) {                 log.d("mainactivity", "error");             }          });      }catch (exception e){         log.d("mainactivity", "error= " + e);     }  }  

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -