I want to save a JSON file from url locally in my app and also since JSON file is getting updated every minutes so i want to update my local file with updated JSON on url after fixed interval of time in backend(without affecting frontend)
Asked
Active
Viewed 1,129 times
1
-
1Have you tried anything specific and are hitting a blocking point? [Gson](https://sites.google.com/site/gson/gson-user-guide) can help you deserialize the json to a POJO, theb you can [schedule a task](http://www.sitepoint.com/scheduling-background-tasks-android/) to run again. Id try to give more help, but unless you're stuck on something specific, it's hard to know where to direct you. – nerdwaller Jul 16 '14 at 04:37
-
i am able to save JSON file in my app but not able to update the file.I am saving the JSON in .txt format.Please provide solution for saving the file if you have any other way to perform this operation – Durgesh Jul 16 '14 at 04:42
-
1I think you need to consider a better [storage solution](http://developer.android.com/guide/topics/data/data-storage.html) for one. Are you wanting to keep all results, or only the most recent at any given time? – nerdwaller Jul 16 '14 at 04:44
-
I want to save full JSON after fixed interval of time say 5 min. – Durgesh Jul 16 '14 at 04:47
-
try to add your json file data in database and after your interval update new data in database. – Haresh Chhelana Jul 16 '14 at 04:49
-
So you want the full history and every 5 mins you will add to it? If so, you probably want to use a local database. Check out [this tutorial](http://www.vogella.com/tutorials/AndroidSQLite/article.html) – nerdwaller Jul 16 '14 at 04:49
-
But the JSON file is very large.So i think using database will not be efficient.Please suggest if you have any other opinion. – Durgesh Jul 16 '14 at 04:52
-
If you are doing lots of updates and wanting to keep the entire sequence, then a database is **way more efficient** than you manually reading/parsing/writing text files. However, if you are just replacing the old data with new, then you can just overwrite the old text file and probably be fine. I asked for clarity for a reason, the approach is different depending on what you want. If you only want the most recent, use @Paritosh's answer, otherwise - you'll want an sqlite database I bet. – nerdwaller Jul 16 '14 at 12:10
1 Answers
2
One way is to use shared preferences to store JSON string and update that as and when required.
To store data:
SharedPreferences settings = getApplicationContext().getSharedPreferences("PREF_NAME", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("strJSON", "" + strJSONfromServer);
editor.commit();
To retrieve data :
SharedPreferences settings = getApplicationContext().getSharedPreferences("PREF_NAME", MODE_PRIVATE);
String strData = settings.getString("strJSON", "");
To clear data :
SharedPreferences settings = getApplicationContext().getSharedPreferences("PREF_NAME",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.remove("strJSON");
editor.commit();
-
-
1. You can clear contents of file and update it.
or
2. You can delete old file and save new file. – Paritosh Jul 16 '14 at 05:20