Provide Offline data in web service based application android
Today I am going to share my one of best practice module which is very help full to provide data while your web service call fails or No network situations.
I made a simple class that store web service response (JSON String ) into shared preferences
and when there is any exception in web service call. that time we used our last saved web service response and show offline data .
here is my class Offline.java
and when ever i get updated data from web service I just call the following line
as
so every time it will store the my last successful updated data in shared preferences
and whenever i got exception in webservice call I used the following code snippet
:)
thats end Now in offlineResponse string I have my last saved response.
like that my app will always have data either it is online ot it is offline :)
I hope this will help full for you guys :)
Happy Codddddddding :)
Today I am going to share my one of best practice module which is very help full to provide data while your web service call fails or No network situations.
I made a simple class that store web service response (JSON String ) into shared preferences
and when there is any exception in web service call. that time we used our last saved web service response and show offline data .
here is my class Offline.java
package
com.gaincircle.classes;
import
android.content.Context;
import
android.content.SharedPreferences;
import
android.preference.PreferenceManager;
import android.util.Log;
public class Offline {
private static SharedPreferences prefs;
private static
SharedPreferences.Editor prefsEditor;
/**
*
* @param preferenceName
* name prference
whose value you want to retrive
* @param context
*
Context
* @return
*/
public static String
retriveOfflineResponse(String preferenceName,
Context
context) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
String
jsonString = prefs.getString(preferenceName, null);
Log.e("offline", "Data retrive
for "
+ preferenceName + ".");
return jsonString;
}
/**
* @param jsonString
*
Return prviously store Json String
* @param preferenceName
* @param context
*/
public static void
saveOfflineResponse(String preferenceName,
String
jsonString, Context context) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefsEditor = prefs.edit();
prefsEditor.putString(preferenceName,
jsonString);
prefsEditor.commit();
Log.e("offline", "Data stored
for "
+ preferenceName + ".");
}
}
as
Offline.saveOfflineResponse("DowloadedList",WebServiceResponse, context);
so every time it will store the my last successful updated data in shared preferences
and whenever i got exception in webservice call I used the following code snippet
String offlineResponse = Offline.retriveOfflineResponse("DowloadedList", context);
:)
thats end Now in offlineResponse string I have my last saved response.
like that my app will always have data either it is online ot it is offline :)
I hope this will help full for you guys :)
Happy Codddddddding :)
No comments:
Post a Comment