Gangadhar Nimballi
Gangadhar Nimballi

Reputation: 1534

Android httpRequest caching

My application is using HttpRequest for many times. is there a way to not call Rest for every time?

i.e How maintain cache for Api Request call. and it should also be worked when there is no internet

Thanks in advance.

Upvotes: 1

Views: 913

Answers (1)

cooperok
cooperok

Reputation: 4257

Android have own classes to cache responses. To turn cache you should call HttpResponseCache.install method. Such as

HttpResponseCache.install(getCacheDir(), 10 * 1024 * 1024); //10MB cache size

and when you open connection call

connection.setUseCaches(true);

Somthing like this

URL url = new URL("http://site.com/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(true);

Also you can read this topic

Upvotes: 3

Related Questions