How to use caching mechanism in android for httprequest - android

I am working on an android application in which i want to use caching mechanism for httpurlrequest. I want to cache the response and want to use again for next request.
So, in android how to cache the response and how to use it next time when we do the same request.
Any working example would be great for me.
How to check whether response is from cache or from server?
How to check whether cache is available for the particular request.
PS: there is no any support of cache from server side. i.e sever doesn't send any 'Cache-Control' header field in response.
Thanks,

Related

Which is the best way to save data on a mobile device?

If not want to use SQLITE DB, what is the ways can use to save data?
example: Now i'm use RETROFIT for REST API to fetch Movies of a server when I turn-on Internet can show movies,
but when turn-off internet and restart app will show activity without movies.
I want to make app when turn-off internet show the movies which is loaded before. How can i do that?
Use Volley Android Volley library has a very elaborate caching mechanism. This is one of the best features of volley. When a request is made through volley first it is checked in the cache. If an appropriate response is present in cache then it is parsed and returned directly to main thread, else a network request is made.
You can try to save your json 'as-is' in file or SharedPreferences and get it when your device doesn't have any network.
Also you can try to work with OkHttp cache or interceptors https://github.com/square/okhttp/wiki/Recipes

How to simulate browser login in Android using API

I want to make an android app which will login to my web application using rest API. In browsers we have a concept of cookie which servers use to identify/maintain session with the users.
In Android how would we accomplish it ? I heard that there is a concept of token which is sent by server in response(first time when credentials are validated) and Android app have to send it to server every time it tries to access a resource(protected). So, what is the better way of doing it ?
Do we need to validate the token again and again when the client requests for a resource ?
Honestly, I can't think of a better way of doing this. Token based authentication seems to be pretty standard when dealing with RESTful APIs. Is there any reason you can't do that?
If you don't want to change the server code, then this could be simulated by adding a cookie header to every request you send. But this is basically the same thing that you mentioned above, just not as clean.
And the browser is already sending a token to be validated again and again. Every request has a cookie header that gets validated through your web application on every request, so this isn't a big deal at all.
And, you don't need anything Android specific to accomplish this. In whatever http library you're using I'm sure there is a method you can called or something you can override in order to set custom headers. Use that to set either your cookie header or token header on every request that you need to make.

Android OkHttp how to handle ETag

I'm try to do Android offline caching method using OkHttp. The flow is like this:
I send an HTTP request with an ETag empty string at header field if-none-catched to the server, and the server returns a proper ETag.
Then I store the ETag and cache the response.
Next time when I fire the same service call I get the same ETag and set it to the HTTP request header and the server will return HTTP 304 if the ETag is the same.
My issue now is how can I store and retrieve the ETag? Does OkHttp default will handle or I need store at SQLite? I keep looking on Google about OkHttp implement ETag sample code but all I get is just normal caching method. Link I refer:
I'm trying to use Java's HttpURLConnection to do a "conditional get", but I never get a 304 status code
https://github.com/square/okhttp/wiki/Interceptors
Correct way of setting up cache for OkHttp in Android
https://gist.github.com/polbins/1c7f9303d2b7d169a3b1#file-restcontroller-java-L45
so far what I achieved is only cache for 1st time service call, but never get latest data from server anymore. Appreciate if anyone can provide some guidance or found any good example of handle ETag and if-none-catch dynamically for OkHttp to share with. Any clarification feel free to ask.
You just need to enable OkHttp’s response cache. It’ll use the ETag if your webserver returns one.

as3 URLLoader Cache suggestions for mobile app

I have been looking around for a class to manage the caching of the data from a URLLoader call but been unsuccessful. Does URLLoader cache by default?
I am building an app that fetches a bunch of information on the user (profile details, friend lists, profile image etc) and I would prefer not to call URLLoader each time. I am caching their profile image on first load and hope I can do the same with the rest of the data without having to create a clone of the DB locally.
Cheers
Yes, the URLLoader caches requests, and there are various solutions to break that cache request (generally by adding a random element to the end of the web request). You can see the documentation here for the URLRequest object that's returned, and it's various cache options.
However, I recommend persisting the data locally upon receiving the request and using the platform's database / data storage pattern that you're on. Then, check for internet prior to making the request: if you can make a connection, make the request to retrieve and update the local data. If there's no internet / connection, just load the data you have saved locally. Using the "cached" version of the request isn't a trustworthy pattern.

Android: Download File only if changed

I want to write a android application which needs data from the web. This information is stored in a json-file. The data from the json-file is saved on the device. To keep it up to date, I need to check for changes in the file every hour.
As the remote File can get quite large I want to download it only if it is different from the version which was previously downloaded. I thought about using the Last-Modified-Header of HTTP for this.
I came up with the following workflow (pseudo-code):
data = null; data_timestamp = null;
Every hour repeat:
Issue a HTTP Head-Request to the URL and option new_timestamp from Last-Modified Header.
If either data==null or new_timestamp > data_timestamp then
Issue a normal HTTP-Request to the URL
Save to data and set data_timestamp = new_timestamp
Do you think this is a reasonable approach? I could use the if-modified-since HTTP Header to get the data only if it has changed since the last download. This would save me one request. If it has changed, a body containing the new data is provided, if it hasn't, the body is empty.
I also thought about using ETags, as I typically want to download if the file has new content (and not if the modified-date has changed), but my webserver (nginx) doesn't support the creation of etags and I don't want to involve another layer on the server-side for performance-reasons.
You should look into using ETags instead of relying on HTTP HEAD. They are supported in javax.ws.rs.core with the EntityTag class.
You can see a Java-based example using Spring to help explain some of the concepts as well.
I solved the problem as described by me above: Download the file by using the if-modified-since HTTP Header. The ngnix webserver can be configured to return the right information regarding this header.

Categories

Resources