How To Cache Parsed Json For Offline Usage
Solution 1:
Why not just save it to cache folder of your app using something like this:
Stringpath= Environment.getExternalStorageDirectory() + File.separator + "cache" + File.separator;
Filedir=newFile(path);
if (!dir.exists()) {
dir.mkdirs();
}
path += "data";
Filedata=newFile(path);
if (!data.createNewFile()) {
data.delete();
data.createNewFile();
}
ObjectOutputStreamobjectOutputStream=newObjectOutputStream(newFileOutputStream(data));
objectOutputStream.writeObject(actorsList);
objectOutputStream.close();
And after, you can read it in any time using:
List<?> list = null;
Filedata=newFile(path);
try {
if(data.exists()) {
ObjectInputStreamobjectInputStream=newObjectInputStream(newFileInputStream(data));
list = (List<Object>) objectInputStream.readObject();
objectInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
UPDATE: Okay, make class named ObjectToFileUtil, paste this code to created class
package <yourpackagehere>;
import android.os.Environment;
import java.io.*;
publicclassObjectToFileUtil {
publicstatic String objectToFile(Object object)throws IOException {
Stringpath= Environment.getExternalStorageDirectory() + File.separator + "cache" + File.separator;
Filedir=newFile(path);
if (!dir.exists()) {
dir.mkdirs();
}
path += "data";
Filedata=newFile(path);
if (!data.createNewFile()) {
data.delete();
data.createNewFile();
}
ObjectOutputStreamobjectOutputStream=newObjectOutputStream(newFileOutputStream(data));
objectOutputStream.writeObject(object);
objectOutputStream.close();
return path;
}
publicstatic Object objectFromFile(String path)throws IOException, ClassNotFoundException {
Objectobject=null;
Filedata=newFile(path);
if(data.exists()) {
ObjectInputStreamobjectInputStream=newObjectInputStream(newFileInputStream(data));
object = objectInputStream.readObject();
objectInputStream.close();
}
return object;
}
}
Change < yourpackagehere > to your package name and don't forget to add WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml. In your MainActivity add field
privateString dataPath;
and replace your onPostExecute method of JSONAsyncTask class to
protectedvoidonPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if(result) {
try {
dataPath = objectToFile(arrayList);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
Now you can access get actorsList from File anytime when you want, by using
try {
actorsList = (ArrayList<Actors>)objectFromFile(dataPath);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
If you want to save path of file after closing application you must save dataPath string (and load on application start), for example, using SharedPreferences.
Solution 2:
And what would be the best optionto cache data ? SharedPreferences or SQLite database
Which is purely based on the data you received.
- If the data is Small,Unstructured data then use Shared Pref.
- If the data is Large,Structured data then use SQLite.
But for store the full data better you can use file concept. Store the string data in your code String data = EntityUtils.toString(entity);
the data
you have to save to the file.If any changes in the data from the server add that to file.And retrieve the data if internet not present. Get the example code for file operations from the above link.
Post a Comment for "How To Cache Parsed Json For Offline Usage"