0

fellow developers.

I am a junior Android Developer who recently came out of school. I need to develop an application that displays a ListView with some content downloaded from the Internet. The thing is, the JSON is a little bit "big", therefore, it would be good that users could download the JSON with WiFi, and in case that their network connection is bad, they could use a "local copy" of the data instead of having to download it again with slow Internet.

Is there any way I can download JSON-encoded information with AsyncTask or anything and store it locally so I can open it later and load the ListView content again?

Thank you :)

uryftw
  • 33
  • 1
  • 5
  • Many ways.. store the data on a database, write the data into file using FileInputOutputStream, sharedpreferences and etc – Sheychan Jul 28 '15 at 07:38

2 Answers2

1

First things first: Read this to know what kind of options you have.

Now to answer your question on how to save data on locally some code from here:

String FILENAME = "hello_file";
String yourJSON = downloadedJSON.toString() // depends what kind of lib you are using

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(yourJSON.getBytes());
fos.close();

Store the FILENAME in the SharedPreferences so that you can read it later in case it changes or just to learn how to store data in SharedPreferences:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // PREFS_NAME should be a static final String property
SharedPreferences.Editor editor = settings.edit();
editor.putString("myFileName", FILENAME).commit();

Then when you reopen the app later you can do it like this:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String FILENAME = setting.getString("myFileName", "");
if (!FILENAME.isEmpty()) {
   readFile(FILENAME);
}

And finally read the file back as String from here:

FileInputStream fis;
fis = openFileInput("test.txt");
StringBuffer fileContent = new StringBuffer("");

byte[] buffer = new byte[1024];

while ((n = fis.read(buffer)) != -1) { 
  fileContent.append(new String(buffer, 0, n)); 
}

Try to understand each block of code before you use this code. Go through the Android Documentation and use Google if you are stuck. Plenty of tutorials out there. Data Storage is fundamental and not just for Android!

Community
  • 1
  • 1
ezcoding
  • 2,974
  • 3
  • 23
  • 32
  • Thank you very much, ezCoding. I'll check everything out today when I come out from work :) – uryftw Jul 28 '15 at 08:08
  • Arf, I still have not been able to test this, but I will accept the answer as soon as I can test it and see if it works (which I hope it will). Sorry for the delayed answer! – uryftw Aug 05 '15 at 09:06
0

you can use this library :

http://loopj.com/android-async-http/

this library support GET/POST method to request and recieve respone from your service.

After that you can save your data by SQLite or android preferences :

http://www.tutorialspoint.com/android/android_sqlite_database.htm http://www.tutorialspoint.com/android/android_shared_preferences.htm

Nguyen Thanh An
  • 269
  • 1
  • 4
  • 12