retrofit - android Retrofit2 with OkHTTP3 sequence call with unexpected same result -
i using android retrofit2 okhttp3 , need call same api twice. , take action based on each response got old response always.
how clear old response every time api called?
here code
@get("/state/{id}") observable<result<responsestatemodel>> getstatebyid(@path("id") string id); public class cachinginterceptor implements interceptor { @override public okhttp3.response intercept(interceptor.chain chain) throws ioexception { request request = chain.request(); request = new request.builder() .cachecontrol(new cachecontrol.builder() .maxage(0, timeunit.days) .minfresh(0, timeunit.hours) .maxstale(0, timeunit.hours) .build()) .url(request.url()) .build(); return chain.proceed(request); } } retrofit retrofit = new retrofit.builder() .baseurl(base_url) .addconverterfactory(gsonconverterfactory.create()) .addcalladapterfactory(rxjavacalladapterfactory.create()) .client(okhttpclient) .build(); okhttpclient = new okhttpclient.builder() .addinterceptor(logging) .addinterceptor(new cachinginterceptor()) .cookiejar(new appcookiejar()) .authenticator(authenticator) .build(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_details); getstate(); }
here called api method
public void getstate(){ backgroundthreadobservable.tobackground(getstatebyid(id)) .subscribe(new action1<result<responsestatemodel>>() { @override public void call(retrofit2.adapter.rxjava.result<responsestatemodel> responsestatemodel) { log.e("response => ", new gson().tojson(responsestatemodel)); btnchangestate.setvisibility(visible); } }); } btnchangestate.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { changestate(); } });
change state method , call state again
public void changestate(){ backgroundthreadobservable.tobackground(changestatebyid(id)) .subscribe(new action1<result<responsechangestatemodel>>() { @override public void call(retrofit2.adapter.rxjava.result<responsechangestatemodel> responsechangestatetmodel) { log.e("response => ", new gson().tojson(responsechangestatetmodel)); if (responsechangestatetmodel.response().body().getstate().getis_change() == false) { log.e("state", "false"); }else if (responsechangestatetmodel.response().body().getstate().getis_change() == true) { getstate(id); } } });
regrads,
add interceptor:
public class cachinginterceptor implements interceptor { @override public okhttp3.response intercept(chain chain) throws ioexception { request request = chain.request(); request = new request.builder() .cachecontrol(new cachecontrol.builder() .maxage(0, timeunit.days) .minfresh(0, timeunit.hours) .maxstale(0, timeunit.hours) .build()) .url(request.url()) .build(); return chain.proceed(request); } }
Comments
Post a Comment