Reputation: 1
When I use OkHttp to GET JSON from a URL like this :
Request request = new Request.Builder()
.url(url).build();
I usually get a same response (sometimes I can get a new response).
If I use like this:
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noCache().noStore().build())
.url(url).build();
I will get a new response everytime.
I want to know why I get the same reponse by the first method?
Upvotes: 0
Views: 332
Reputation: 2323
Caching in HTTP
HTTP is typically used for distributed information systems, where performance can be improved by the use of response caches. The HTTP/1.1 protocol includes a number of elements intended to make caching work as well as possible. Because these elements are inextricable from other aspects of the protocol, and because they interact with each other, it is useful to describe the basic caching design of HTTP separately from the detailed descriptions of methods, headers, response codes, etc.
Caching would be useless if it did not significantly improve performance. The goal of caching in HTTP/1.1 is to eliminate the need to send requests in many cases, and to eliminate the need to send full responses in many other cases. The former reduces the number of network round-trips required for many operations; we use an "expiration" mechanism for this purpose. The latter reduces network bandwidth requirements.
For more information on this, go through Caching in HTTP. Also for help on coding aspect, go through this documentation on Class Cache.
Upvotes: 1