在android设备中保存服务器对本地的响应

时间:2017-03-20 10:14:59

标签: java android json

我将此作为我的服务器响应。我想将其全部存储在设备中,以便必须创建用户的会话并将其存储在设备上,以便不需要进一步登录该应用程序。如果用户退出应用程序,则必须删除此数据。 我被困在这里....请告诉我解决这个问题的方法。

    03-20 13:18:56.486 20338-20537/? D/Null: Params :{data[User][password]=pfrt1fKLkoZhAT6hoMJFiA==
                                         , data[User][email]=RyUMRBg7UyeIlFBBtNemZFuG46PJtAIdiZWXnlJ4zNI=
                                         }
03-20 13:18:56.609 20338-20338/? D/Null: Reponse Check :{"code":200,"user":{"User":{"id":"ui1bJkK19jxbaquTboA2oQ==","email":"RyUMRBg7UyeIlFBBtNemZFuG46PJtAIdiZWXnlJ4zNI=","status":"1","verified":"1","created":"2016-03-07 11:41:59","modified":"2016-04-07 15:43:43","token":"6b987332b77d7c69d76bf7be80a85177fb7fa08d"},"Profile":{"id":"1","first_name":"Pradeep","last_name":"Gupta","bio":"sfafaf","address":"82, Debinibash Road\r\nDum Dum, P.O. - Motijheel","phone":"+913325505055","profile_pic":"\/img\/356a192b7913b04c54574d18c28d46e6395428ab\/license.jpg","user_id":"1","Contributor":{"id":"31","profile_id":"1","status":"1","vs_cdn_id":"261961777","secret_token":"s-7Va5z","uploaded_on":null,"statement":"AOK KJDHKJDH bkgkg kkhkjh kjhkj kjh kjhkjh","time":"7 hours per month","created":"2016-05-02 18:40:11","modified":"2016-05-02 18:41:29"},"Moderator":[]},"redirect":"\/"}}
03-20 13:18:56.695 20338-20338/? D/Null: Object Check :ModelObject{code='200', id='null', email='null', first_name='null', last_name='null'}
03-20 13:18:56.695 20338-20338/? D/Null: Object Check :class com.example.user.myapplication.ModelObject

2 个答案:

答案 0 :(得分:0)

您可以使用SharedPreferences来解决此问题。

保存数据:

SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);  
SharedPreferences.Editor data = preferences.edit();
data.putString("loginInfo", yourLoginObjectString);
data.commit();

阅读数据:

String loginObjectString = preferences.getString("loginInfo", "defaultValue");

答案 1 :(得分:0)

您可以考虑回复中的任何唯一字段,例如每个用户的电子邮件或ID或名称将不同,并以下列方式将其保存在SharedPreferences(本地设备存储)中。

在偏好设置中设置值:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Pradeep");
 editor.putInt("idName", 12);
 editor.commit();

从偏好中检索数据:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

当用户退出应用程序时,将该字段设置为null。 每次启动应用程序时,检查是否存在已保存的字段(空检查)。 多数民众赞成。