Sei sulla pagina 1di 7

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

How to Cache Json data to be available offline?


3

java

android

json

offline

I have parsed the JSON Data in a listview and now I want to make it available offline. Is there a way to save the JSON data at the phone so that
you can see the data if your phone is offline?
Does someone knows an example?
EDIT works now:

public class MainActivity extends ListActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, JSONArray> {
InputStream is = null;
String result = "";
JSONArray jArray = null;
ProgressDialog pd;
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
pd.dismiss();
ArrayList<String> list= new ArrayList<String>();

share

improve this question


user3241084
38 1 6

Asked
Jan 30 '14 at 15:22

Brian Tompsett -
2,769 10 21 58

Edited
Dec 16 '15 at 15:54

You can save it to phone database. Search for it. Gokhan Arik Jan 30 '14 at 15:24

add a comment

5 Answers

order by votes

You have two ways. Either you create a database and save all of the data there and retrieve it back when you want to. Or if the data
you have is not that much and you don't want to deal with databases, then you write the json string to a text file in the memory card

and read it later when you are offline.


And for the second case, every time you go online, you can retrieve the same json from your web service and over write it to the old
one. This way you can be sure that you have the latest json saved to the device.

share

improve this answer


osayilgan
3,770 5

27 54

Answered
Jan 30 '14 at 15:25

Depending on the JSON (if it is couple of entries), @user3241084 can use SharedPreferences. Yordan Lyubenov Jan 30 '14 at 15:40

@YordanLyubenov considering that user wants to store JSON object programatically when it's offline, I don't think it's just a couple of entries. osayilgan
Jan 30 '14 at 16:11

how can I add this to my code? I just tried but nothin worked? user3241084 Jan 30 '14 at 16:11

@user3241084 post your tries here Raghunandan Jan 30 '14 at 16:16

just deleted the wrong code that my app runs ;) user3241084 Jan 30 '14 at 16:21

show 10 more comments

public class CacheHelper {


static int cacheLifeHour = 7 * 24;
public static void save(String key, String value) {
try {
key = URLEncoder.encode(key, "UTF-8");
File cache = new File(FileManager.getCacheDirectory(G.context) + "/" + key + ".srl");
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(cache));
out.writeUTF(value);
out.close();
} catch (Exception e) {

e.printStackTrace();

share

improve this answer


Ashkan Ghodrat
731 7 18

share

Once you download the data you could persist the data on the mobile, using a database or a system of your preference.
You can check the different options here: data-storage

improve this answer

Answered
Jan 4 at 6:42

Javier Salinas
391 1 12

Answered
Jan 30 '14 at 15:26

you can use those two methods two store you JSON file as a string in your SharedPreferences and retrieve it back:

public String getStringProperty(String key) {


sharedPreferences = context.getSharedPreferences("preferences", Activity.MODE_PRIVATE);
String res = null;
if (sharedPreferences != null) {
res = sharedPreferences.getString(key, null);
}
return res;

public void setStringProperty(String key, String value) {


sharedPreferences = context.getSharedPreferences("preferences", Activity.MODE_PRIVATE);
if (sharedPreferences != null) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
CupsLog.i(TAG, "Set " + key + " property = " + value);
}
}

just use setStringProperty("json", "yourJsonString") to save and getStringProperty("json") to retrieve.

share

improve this answer


Emil Adz
19k 20

68 106

Answered
Jan 30 '14 at 15:31

Edited
Jan 30 '14 at 15:39

using SharedPreferences should be prepared to sqlite (unless of course you have a database structure). For caching and storing
data pulled from the internet, I recommend robospice: https://github.com/octo-online/robospice. It's a very well done library, easy to

use, and should be used any time you download data from the internet or have a long-running task.

share

improve this answer


Mohamed ALOUANE
157 13

Your Answer

log in

or
Name

Email

Answered
Apr 23 '14 at 16:49

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Post Your Answer

Potrebbero piacerti anche