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
Related
I'm creating an app that connects via internet to a rest API which is connect to a postgres database.
In this API I am exporting some JSON data that I wish to consume using the app. I pretend on using okhttp3 to do this.
The thing is, I wan't this JSON data to be available locally, and be downloaded to the persons phone since the app is mostly going to be used underground, inside subway stations, where there is no internet connection.
My question is how can I get the data that return from the REST API, and persisti it to a JSON file and then deserialize it as needed to show the data to the user.
I pretend on using GSON to deserialize, but I don't know how to save the data to a file and, after that, acess that data.
Also I pretend on implementing a automatic update system where, after any change on the data, the app will be able to download and overwrite the old data with the updated version.
Also I'm using Java, not Kotlin, for this app.
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,
I use Picasso for downloading and caching images in my app.
Its work great then i decide to implement this for other requests from web.
my question is how can i cache data (Like json and xml) received from web in android ?
I mean at first request to web if request is successful response save in user device memory till he/she click update data to get fresh data and cache the new data.
You can write you own CacheManger ,below is what Cache Manager should do :
Hit http request and get json data
Add data to local database first[cache the url and json data both]
Provide Data model to your api
When the next reuest comes to cache manager check for url first if
present in db, provide that data since it is been already cached
if the url as key in db is not present then only hit the http apis.
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.
i want to keep my app in sync with the Server. The communication between client (android app) and server is handled through JSON Objects / HTTP. What is the best strategy if the connection is not available anymore? It is important that the user can continue his work with the app. Does there even exist frameworks for such sync-problems?
i thought about a queue of transactions?! Any reccomendations or experiences?
Thanks
For fetching... I once wrote a caching URL manager that would load read the JSON from reply from the server and write it to the SD Card. When if the user did another request for the same URL a short time later, the URL manager would simply return the cached json from the filesystem as the JSON reply. This made the communication code somewhat transparent, in that I was always dealing with JSON replies, whether or not they were cached or real time.
For sending information to the server, you could write all information to the database, and use a background service that pushes the data to the server. That way the UI will always succeed in writing the information and your sync service would push data if there was a network connection. Using a Service you can simply pass the data in the Service intent and it can worry about the writing to db and syncing, etc.
http://developer.android.com/training/cloudsync/aesync.html
i think this is what i will use in my next project :)