android - Recursive Retrofit Call -
i'm uploading files server using hawk authentication , retrofit 1.6 on android. need handle case devices clock out of sync server time. when make first request if timestamp out of sync server respond 401 , supply correct server time in response. adjust client timestamp , resubmit. approach recursively within callback's failure(). problem recursive call timing out 95% of time , succeeds 5% of time. suspect there deadlocking issue recursive call , request/response threads. i'm not setting executor threads rather using default. illustrates restadapter , basic call flow (shortened brevity). can suggest going on , how solve this?
public static restadapter getretrofitadapter(final map<string, string> headers) { requestinterceptor requestinterceptor = new requestinterceptor() { @override public void intercept(requestfacade request) { if (headers != null) { (string key : headers.keyset()) { request.addheader(key, headers.get(key)); } } } }; okhttpclient client = new okhttpclient(); cookiemanager cookiemanager = new cookiemanager(); cookiemanager.setcookiepolicy(cookiepolicy.accept_all); client.setreadtimeout(60, timeunit.seconds); client.setconnecttimeout(60, timeunit.seconds); client.setcookiehandler(cookiemanager); final restadapter restadapter = new restadapter.builder() .setendpoint(url) .setrequestinterceptor(requestinterceptor) .setloglevel(restadapter.loglevel.full) .setclient(new okclient(client)) .build(); return restadapter; } public void uploadmedia(final string localfilename, final uploadcallback callback, final context context) { try { map<string, string> headers = generatehawkauthorizationheader(mediapath, http_post); restadapter restadapter = getretrofitadapter(headers); mediauploadservice service = restadapter.create(mediauploadservice.class); file localfile = new file(localfilename); if (localfile.exists()) { typedfile mediafile = new typedfile("multipart/form-data", new file(localfilename)); callback<media> callback = new callback<media>() { @override public void success(media media, response response) { callback.onuploadresponse(media, response.getstatus()); } @override public void failure(retrofiterror error) { if (error != null && error.getresponse() != null && error.getresponse().getstatus() == 401){ if(hastimedifference((jsonelement)) { adjusttimestamp(); uploadmedia(localfilename, callback, context); } } else { int statuscode = error.getresponse() != null ? error.getresponse().getstatus() : -1; callback.onuploadresponse(null, statuscode); } } }; service.uploadmedia(mediafile, callback); } else { callback.onuploadresponse(null, 404); } } catch (exception e) {} }
Comments
Post a Comment