java - Retrofit calls are adding up -
i have button in application logging out user's session , when clicked retrofit sends basic post request localhost , technically logged out , if click logout button again following happens:
1st time : 1 request goes through 2nd time click logout : 2 requests made logout @ same time 3rd time click logout : 3 requests made logout
and on ..
honestly , situation puzzling me , yes use retrofit okhttp interceptors , token management .
note : - (tinydb simple shared preference manager)
code posted below:
apiinterfacer.java - api interface
@formurlencoded @post("/api/token/logout") call<basicresponse> log_out(@field("guid") string guid);
apiservice.java - api service creator
public class apiservice { public static final string baseurl = staticvars.localhost; public static okhttpclient.builder httpclientbuilder = new okhttpclient.builder(); private static retrofit.builder retrofitbuilder = new retrofit.builder().baseurl(baseurl).addconverterfactory(gsonconverterfactory.create()); public static <s>s createservice(class<s> serviceclass , context context) // service class rest class , ex : githubclient { retrofit retrofit = retrofitbuilder.build(); httpclientbuilder.interceptors().add(new apitokeninterceptor(context)); okhttpclient client = httpclientbuilder.build(); retrofitbuilder.client(client); return retrofit.create(serviceclass); } }
apitokeninterceptor.java
public class apitokeninterceptor implements interceptor { private final context context; public apitokeninterceptor(context context){ this.context = context; } @override public response intercept(chain chain) throws ioexception { log.i("intercept" , "first " + chain.request().url()); tinydb db = new tinydb(context); request request = chain.request(); request modifiedreq = request; if(!db.getstring("token").equals("")) { modifiedreq = request.newbuilder().addheader("token" ,db.getstring("token")) .build(); } response response = chain.proceed(modifiedreq); boolean unauthorized = response.code() == 401; log.i("intercept" , "second - logout - proceed " + modifiedreq.url() + " " + unauthorized); if(unauthorized){ response.body().close(); db.remove("token"); string refreshtoken = db.getstring("refken"); request request1 = new request.builder() .url(staticvars.localhost + "/api/token") .addheader("refken" , refreshtoken) .get() .build(); okhttpclient client = new okhttpclient(); response response1 = client.newcall(request1).execute(); string response_string = response1.body().string(); log.d("intercept" , "response token string " + response_string); try{ jsonobject object = new jsonobject(response_string); string token = object.getstring("token"); //set token db log.d("tokenb" , " "+token); db.putstring("token" , token); }catch (jsonexception e){ e.printstacktrace(); log.d("invalid response" , " "+response_string); db.putstring("token" , "placeholder"); log.i("intercept" , "afinal - logout - proceed " + modifiedreq.url()); return response; } modifiedreq = request.newbuilder().addheader("token" , db.getstring("token")) .build(); log.i("intercept" , "bfinal - logout - proceed " + modifiedreq.url()); return chain.proceed(modifiedreq); } return response; } }
(usage).java - usage takes place in onclicklistener of button
retro.setonclicklistener(new view.onclicklistener() { @override public void onclick(final view v) { //call retrofit :) final progressdialog pd = new progressdialog(v.getcontext()); pd.setmessage("loading..."); pd.setcancelable(false); pd.setindeterminate(true); pd.show(); apiinterfacer client = apiservice.createservice(apiinterfacer.class, v.getcontext()); call<basicresponse> call = client.log_out(db.getstring("guid")); call.enqueue(new callback<basicresponse>() { @override public void onresponse(call<basicresponse> call, response<basicresponse> response) { log.d("response : string" , "" + response.message()); switch(response.code()) { case 401: log.d("logout" , "unauthorized : "+401); break; case 400: log.d("logout" , "malformed body : " + 400); break; case 404: log.d("logout" , "url not found :" + 404); break; case 200: log.d("message" ,""+ response.body().getmessage()+" "+response.body().getcode()); if(response.body().getcode().equals("loggedout")) { db.remove("token"); db.remove("refken"); startactivity(new intent(getactivity(), loginactivity.class)); getactivity().finish(); } break; default: basicresponse resp = response.body(); if(resp==null) { log.d("error" , "response null : " + response.code()); }else { log.d("code", "" + resp.getcode()); log.d("source", "" + resp.getsource()); log.d("message", "" + resp.getmessage()); log.d("error", "" + resp.geterror()); } } if(!call.iscanceled()) { log.i("intercept" , "force cancel"); call.cancel(); } pd.cancel(); } @override public void onfailure(call<basicresponse> call, throwable t) { t.printstacktrace(); pd.cancel(); //page divert intent toast.maketext(v.getcontext() , "could not connect server" , toast.length_short).show(); log.d("fail" , ""+t.getmessage()); if(!call.iscanceled()) { log.i("intercept" , "force cancel"); call.cancel(); } } }); } });
log : -
1st click : ( 0 * first - run @ starting of interceptor ? interceptor not running ?? ) 09-13 00:40:04.657 29600-29600/some.example.okhttp i/intercept: force cancel 2nd click : ( 1 * first - run @ starting of interceptor ) 09-13 00:41:45.156 29600-31375/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:41:45.166 29600-31375/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout false 09-13 00:41:45.217 29600-29600/some.example.okhttp i/intercept: force cancel 3rd click: ( 3 * first ) 09-13 00:41:54.364 29600-31375/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:41:54.364 29600-31375/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:41:54.370 29600-31375/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:41:54.382 29600-31375/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:41:54.431 29600-31375/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:41:54.454 29600-31375/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:41:54.454 29600-31375/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:41:54.466 29600-31375/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:41:54.503 29600-31375/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:41:54.695 29600-29600/some.example.okhttp i/intercept: force cancel 4th click: ( 7 * first ) 09-13 00:42:57.726 29600-32408/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:42:57.726 29600-32408/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:42:57.726 29600-32408/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:42:57.731 29600-32408/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:42:57.740 29600-32408/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:42:57.779 29600-32408/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:42:57.819 29600-32408/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:42:57.819 29600-32408/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:42:57.831 29600-32408/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:42:57.865 29600-32408/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:42:57.892 29600-32408/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:42:57.942 29600-32408/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:42:57.942 29600-32408/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:42:57.942 29600-32408/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:42:57.968 29600-32408/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:42:58.006 29600-32408/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:42:58.029 29600-32408/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:42:58.053 29600-32408/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:42:58.053 29600-32408/some.example.okhttp i/intercept: first http://192.168.2.2/api/token/logout 09-13 00:42:58.086 29600-32408/some.example.okhttp i/intercept: second - logout - proceed http://192.168.2.2/api/token/logout true 09-13 00:42:58.152 29600-32408/some.example.okhttp i/intercept: bfinal - logout - proceed http://192.168.2.2/api/token/logout 09-13 00:42:58.245 29600-29600/some.example.okhttp i/intercept: force cancel
each time create retrofit service, instanciate service again might cause issue.
try making factory
return singleton
of service instead:
private static amazonservices amazonservices = null; private static purpleservices purpleservices = null; static okhttpclient.builder httpclient = getunsafeokhttpclient(); private static retrofit.builder builder = new retrofit.builder(); public static <s> s createservice(class<s> serviceclass) { s mapi = null; if (serviceclass.getsimplename().equals("purpleservice")) { if (purpleservices == null) { purpleservices = (purpleservices) createpurpleapi(serviceclass); } mapi = (s) purpleservices; } else if (serviceclass.getsimplename().equals("amazonservices")) { if (amazonservices == null) { amazonservices = (amazonservices) createamazonapi(serviceclass); } mapi = (s) amazonservices; } return mapi; }
and call this:
private amazonservices amazonservices = retrofitcreator.createservice(amazonservices.class);
Comments
Post a Comment