Update an Android app GridView when new item added to the JSON - android

In my application, I am loading the content for a GridView using a remote JSON file. But that JSON update once in a month. So the user no need to connect Internet every time he/she loads the GridView. So what I have done is, stored the data in to a database for the first time GridView load and next time I'm filling the GridView using the database.
Now I have a problem. I want to send a notification or something like that, when a new item added to the JSON file. With out using C2DM, is there any other way I can achieve my target?
I thought something like a notification that we get when a new version of an application is available. If it is applicable, how can I do that?

If you don't want to push data onto your app the other obvious way is to poll at regular time intervals. A service can do that in the background. Once the server replies positively you can display a notification.

I have found a different approach for the same question. Here we don't need to use either Push Notification or C2DM. By using HttpUrlConnection class and getLastModified() method, we can achieve our target.
Logic is, we save the last updated time of the file in our SharedPreferences and next time we load the app, the SharedPreferences value and the last update date of the value is same, we can load the cache data or the data we have saved in our database. If those values different, then only we download the data from the server.

Related

Which is the best way to synchronize Android application data from server?

I have three informative screens like Facebook blogs in my application. Application works Online and Offline. User can see the data offline.the data is Cached in Database.
When data is added/updated/deleted to server from active users.
The problem is, I want to update all other active application users with new data without affecting their works and in short time. In short, want to update it in background. How I can achieve this?
I have tried below solutions but had some issues:
1. After registration fetching all data from server and store in Database for Offline use.
Problem with this solution: Always showing progress bar for caching data impact bad user experience. user will get irritated.
2. Implemented background processing GCM push notification.(Means without displaying push notification to user process it in background)
Problem with this solution: If user blocks notification then the application never get synchronized with new data added/updated/deleted on server.
Well, if the data is in Json, you can use Retrofit for data fetching, its amazingly fast.
After registration fetching all data from server and store in Database
for Offline use.
Solution: After the user is registered show the Progress for the first time while your getting the data and updating the database, after that display the data from database and keep updating the data in the background.
Or you can simply make a ProgressBar of height 2dp near ActionBar to show the user while data updates in the background and user can interact with the current data available from database.
You can use MaterialProgressBar or make your own.

How to enable offline reading in my application?

Context
I have a news activity which contains a ListView . Each item in the list contains some text and one image. Now every time the user opens the activity, the application makes a request to the server for getting data. Unfortunately if the user doesn't have an active internet connection, I simply put a Toast saying "Please check your internet connection"
Requirements
I would like to show news article from the last session if the user doesn't have an active internet connection. I define session as the time when user launches the activity and exits it by pressing back button.
Libraries used
I am using Retrofit Library for making Http Requests and Picasso for loading images.
What I have tried so far
While exploring Retrofit, I found out that retrofit by default caches the response for some time. So without any internet connection also I am able to get the data. In Picasso also, I found that we can save the image to any location on the device.
How to go for its Implementation ?
Since it will be a big change in my application, I wanted to know what is the best way to enable offline reading using the above two libraries? How do other applications manage to do so? Also if I can get some references or some blogs regarding this implementation, then it would be great.
Use the internal storage of the application.
Parse the image to a byteArray and the text to a single String so you can easily convert this also to a byteArray.
Name the files so you can easily retrieve them and link them back together.
You can storage the last updated data on local, with SharedPreferences or json file.
Instead of show toast, you can load the last updated data from local.
Picasso is a good choice for images, it saves images locally and reuse it automatically later.
For your items, I suggest to use a small local DataBase : in Android, we use SQLiteDataBase. here is a small tutorial : http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
So, I suggest you this pattern
user opens activity
system retrieves data from database
in parallel, system starts to download the new items.
When new items have been downloaded, you should notice user like 9gag or Facebook apps do.

Android get data if data is one hour old from the Web Services

I have the android application where the webservices data is not frequently updated say once in a day.
I have to save data in DB for offline viewing as well, right now I am thinking that, when user clicks an button to get data it checks if the last data entered in DB is one hour old, if no then no downloading from web service if yes then download.
So, at the time I am saving data I will save current time with it , and calculate the difference and get latest data.
Is this a correct approach.
Asmi
Having a field in database table just for this purpose isn't an ideal approach. Instead, you should save the current time in SharedPreferences and compare the time to see if data needs to be refreshed.

How to speed up synchronizing json data from the server using json parser in android?

Sync data from the server using json parser
I have developed android application for offline concept.When internet connected more than 4000 records synchronized from the server. If i add only one data in the server. when i do second time synchronizing, that particular data to be synchronize from the server.My problem is when i do second time synchronizing data it took same time as first time synchronizing. how to avoid this,please advice me.
Synchronizing working fine. But if i press the sync button second time. whole data can be synchronizing again. Please explain in detail. I have little bit idea i don't know whether it is correct or not.
My suggestion:
Flag to be set in the server side and client side. If data synchronized flag to be set as 0. if data newly synchronized it should be set as 1.
if data loss. unfortunatly data cache cleared in application. how to do this?. please explain me.
Check this video
Here a googler explain how to build REST applications - there is a lot of usefull thing about sync.
If you build server and client by youw own - you can store timestamp of the last connection and each time you synchronize you pass this time to the server and depend on it you can pass only modified columns (you have to store timeStamp of modification for data or list of modified columns from key timestamps)

Store data for few minutes for further access

I have a done a project in which data like name, url, desc are coming from server using web-service. Every time I need to parse it and then display. I want to use store parsed data in a particular are in which parsed data will be removed after five minutes. Is this possible to do that, if yes then how? I have searched lot and found one thing "mamcache" but not clear how can be used mamcache in Android
Edit
Actually in my app I have different category and clicking on every category, sending request to the server and then parsing it and then displaying. I don't want to send request every time, so I think we should use some technique by which we download data when app runs first time and then store parsed data in a temporary location (this location will store data only for five minutes). I will check often if temporary location has data then fetch from there otherwise send request download, parse and then display.
Your description of what you need is a little vague, but if you need to persist parsed data you can build a model for your data to store it temporarily, or you can save the data to the application preferences which will be available across app instances.
If you can better describe exactly how you want to use the parsed data, I can give you explicit direction?
Edit
If you only need to persist data for a short period of time within the same activity, then use variables to store your parsed data. If the variables are populated, then don't hit the server. If the variables have not been initialized, then hit the server, parse and put the data into the respective variables. As for holding the data for 5 minutes, use a datetime variable to check if the other variable data is stale.
Do you have control over the server? You could implement caching.
See blog post here:
http://android-developers.blogspot.com/2011/09/androids-http-clients.html
HTTP Caching has been back ported here:
http://code.google.com/p/httpclientandroidlib/
You could also implement a cache yourself using DiskLruCache.

Categories

Resources