Why Http call using Retrofit2 (and rxjava observer) always waits for 5 secs on Wifi (Android M Nexus 6) -
on nexus 6 android m (v23), calling http api using retrofit waits around 5 secs, before getting request hit on server. both phone , server on same wifi. browser on other workstation in same wifi gets results instantaneously (~40-60ms). opening same api android chrome on same mobile takes 5 secs. can issue delayed http call?
code: retrofit2/okhttp service factory
public class apigenerator { public static string tag = apigenerator.class.getsimplename(); public static final string api_base_url = buildconfig.api_endpoint; public static final gson gson = new gsonbuilder() .setfieldnamingpolicy(fieldnamingpolicy.lower_case_with_underscores) .create(); private static okhttpclient.builder httpclient = new okhttpclient.builder() .connecttimeout(15, timeunit.seconds) .writetimeout(15, timeunit.seconds) .readtimeout(15, timeunit.seconds) .addinterceptor(new interceptor() { @override public response intercept(chain chain) throws ioexception { request request = chain.request(); response response = chain.proceed(request); return response; } }).addinterceptor(new logginginterceptor()); private static retrofit.builder builder = new retrofit.builder() .baseurl(api_base_url) .addconverterfactory(gsonconverterfactory.create(gson)) .addcalladapterfactory(rxjavacalladapterfactory.create()); .....
retrofit2 service
public interface authservice { @get("/api/v1/auth") public void authenticate(@query("token") string token); @post("/api/v1/signup") call<object> signup(); }
service call -
observable<response<signupv1response>> observable = service.signuprx(signupreq); observable.subscribeon(schedulers.io()) .observeon(androidschedulers.mainthread()) .unsubscribeon(schedulers.io()) .subscribe(new subscriber<response<signupv1response>>() { @override public void oncompleted() { ... } @override public void onerror(throwable e) { ... } @override public void onnext(response<signupv1response> response) { if (response != null) { final int statuscode = response.code(); if (response.issuccess()) { // } else { responsebody errorbody = response.errorbody(); // show error } } } });
at last found that, has nothing okhttp , retrofit. android networking issue, has ipv6 enabled, on andoid l (v21) , higher. if wifi network not correctly handle ipv6 (most routers including recent releases), these http client libraries takes few secs before getting response. refer issue - https://code.google.com/p/android/issues/detail?id=79576
for workaround, can tunnel local development server amazon ec2 ssh or use ngrok. works fine approach.
Comments
Post a Comment