Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OkHttp fails to return cached response after app cold start when offline #1727

Closed
felipecsl opened this issue Jun 25, 2015 · 1 comment
Closed

Comments

@felipecsl
Copy link
Contributor

if I make a request when I’m offline and I know I should get a cached response from OkHttp, it throws an exception instead of returning the cached response if It can’t resolve the hostname (java.net.UnknownHostException). However, if it was able to make a successful request to that URL before, then it happily returns from the cache, even though I’m offline.

I can easily reproduce this with the following steps:

  1. Open the app (while online);
  2. Make a request and make sure the response is successful and properly cached by OkHttp;
  3. Kill the app;
  4. Set the device to Airplane mode;
  5. Reopen the app;
  6. Make same request again;
  7. Observe that it fails;

If I follow the same steps above, but skipping step 3 (kill the app), it works correctly.

Heres the stack trace (there is an interceptor in the middle):

W  java.net.UnknownHostException: Unable to resolve host "api.example.com": No address associated with hostname
W      at java.net.InetAddress.lookupHostByName(InetAddress.java:457)
W      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
W      at java.net.InetAddress.getAllByName(InetAddress.java:215)
W      at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
W      at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
W      at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
W      at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
W      at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
W      at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
W      at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
W      at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
W      at com.squareup.okhttp.Call.getResponse(Call.java:273)
W      at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
W      at com.example.MyInterceptor.intercept(MyInterceptor.java:35)
W      at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:227)
W      at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
W      at com.squareup.okhttp.Call.access$100(Call.java:36)
W      at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:164)
W      at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
W      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W      at java.lang.Thread.run(Thread.java:818)
W  Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W      at libcore.io.Posix.android_getaddrinfo(Native Method)
W      at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
W      at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
W      ... 21 more

OkHttp version 2.4.0

@felipecsl felipecsl changed the title OkHttp fails to return cached response if it can't resolve the host OkHttp fails to return cached response after app cold start when offline Jun 25, 2015
@felipecsl
Copy link
Contributor Author

Never mind, I screwed up with the okhttp disk cache directory. Had it being used by something else and this was causing a conflict or something, then all the cached files were getting deleted upon app startup. Closing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant