Я хотел бы сохранить некоторые пользовательские настройки в 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();