chocospaz chocospaz - 1 year ago 154
Java Question

Caching Data with OKHttp

I'm trying to cache data with OKHttp's native cache; my problem is that I don't have control over the server side data, and the response header Cache-Control is coming back with a "no-cache" value.

Is there anyway to intercept the request to add in a header to cache the data that's coming back using OkHttp? (I'd also like to cache specific requests if possible).

Thank you!
Best Regards,
Christopher Steven

Answer Source

OkHttp doesn't currently offer a mechanism to defeat Cache-Control: no-cache. OkHttp will end up validating the response with the server, but if the server says the stored response is still good then the response body won't need to be retransmitted.

We've got a feature request outstanding that wants something like this, though it's difficult because it may mean a single request yields multiple responses.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download