last day (15 days later) » 

1:54 PM
1
A: How can implement offline caching of json in Android?

SourAppleThis probably isn't the best way to do it, but it worked for me. You might find this helpful: http://www.vogella.com/tutorials/JavaSerialization/article.html I had to do the same in some project. This is what I did: public final class cacheThis { private cacheThis() {} public static void wri...

 
Where to write to file? I mean after the json parsing of data? can you please guide me with an example ?
 
I usually cache right after parsing the whole response. Like you could write to file at the end of public void onResponse(JSONArray response) like this: public void onResponse(JSONArray response) { Log.d(TAG, response.toString()); hidePDialog(); // Parsing json for (int i = 0; i < response.length(); i++) { . . . } cacheThis.writeObject(YourActivity.this, fileName, movieList); }
 
thnxs the filename i have to create a local variable ??
 
Yes, it's just a string that represents the name of the file you're saving to. You have to make sure that you're using the same fileName when reading from file and writing to file.
 
ok thnxs when where to use that read code which you tell please help me a little more
 
1:54 PM
Well that depends on where you want to use it. For example, you could check if the device is connected to the internet; if not, read the cached items. Or you could read the file anyway, but check for duplicates occurrences when parsing the JSON response.
 
can i check that fill in my phone some where ?
 
No, it's creating a private file (Context.MODE_PRIVATE). You can access it via DDMS if you're using the emulator.
 
I am placing that write code as you describe! but is force closing my application
 
What's the error log?
 
null pointer...
 
1:54 PM
More details? Either your list is null or the fileName you're using is null. Did you define a fileName? String fileName = "TheNameYouWant";
 
Ok thnxs!! please can you tell me how to read cache when internet is off
 
hey
i had done this successfully
hey sourapple after testing my app i found a bug which is
once data is cached in local file it replace the local when internet is active again. example let say when internet is active i load 40 articles it get cached in a local file but when after connecting to internet again let say now i load 20 articles it cached and replace the 20 articles by 40 .what i expect when i come again without internet i will get maximum articles i have loaded previously. thanks!
 
2:22 PM
Hi,
in the CacheThis class, change the following line:
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);

to:
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE | Context.MODE_APPEND);
it should work
 
@SourApple thanks, i have replace the code as you suggested but its not working,now every time it shows only 10 articles without internet.
 
oh, I'm not home right now so I can't really test it. There's another way to solve the issue (but isn't really the best way).
you can load the cached list anyway (internet or no internet) and then append the new loaded items to it
of course you'll have to remove the Context.MODE_APPEND then
I'll send you whatever works with me once I get home.
 
2:41 PM
Thanks a lot for your humbleness and helping.I'm waiting for your reply till the time i'm trying to crack this.I will update you....
 
 
6 hours later…
8:32 PM
Hi again
I edited the cacheThis class: http://pastebin.com/raw.php?i=2Sd62Ji3

also now to read cache you need to do this:

List<Object> templist = (List<Object>) cacheThis.readObject(YourActivity.this, fileName);
for (Object object:templist){
llist.addAll((List<Movie>) object);
}
I actually found the answer here: stackoverflow.com/questions/1194656/…
The answers explain why it wasn't working.
 

  last day (15 days later) »