【发布时间】:2014-09-22 13:44:00
【问题描述】:
我想在 SharedPreferences 中保存一些用户设置,但不知何故,当我读回来时,保存的字符串总是空的。
正在保存..
SharedPreferences prefs = getApplicationContext().getSharedPreferences("AAAAPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor edits = prefs.edit();
edits.putString("request_id", requestId);
edits.putInt("active", active);
edits.commit();
阅读..
SharedPreferences prefs = getApplicationContext().getSharedPreferences("AAAAPrefs", Activity.MODE_PRIVATE);
String request_id = prefs.getString("request_id", "");
我做错了什么? 感谢您的帮助!
编辑:
new HttpHandler() {
@Override
public HttpUriRequest getHttpRequestMethod() {
HttpPost httppost = new HttpPost("***********");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("name", textName.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return httppost;
}
@Override
public void onResponse(String result) {
try {
JSONObject obj = new JSONObject(result);
JSONObject response = obj.getJSONObject("response");
String requestId = response.getString("request_id");
int active = response.getInt("active");
SharedPreferences prefs = getApplicationContext().getSharedPreferences("AAAAPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor edits = prefs.edit();
edits.putString("request_id", requestId);
edits.putInt("active", active);
edits.commit();
/* MORE... */
} catch (Throwable t) {
// Error.
}
}
}.execute();
【问题讨论】:
-
它会抛出任何异常还是只是一个空字符串?
-
遗憾的是我没有得到任何异常或任何东西。只是一个空字符串。
-
您确定 requestId 不为空吗?上面的代码似乎应该可以工作。你能多展示一点你的代码吗,你到底在哪里设置和获取首选项?
-
我通过它进行了调试,我设置的 requestId 不为空。我把它放在一个del中。在 HTTP 请求之后。将使用更多代码更新答案。感谢您的帮助!
-
胡思乱想.. json 解析过程中抛出异常,你永远不会设置字符串。