Android SharedPreferences未按预期工作

时间:2014-09-22 13:44:01

标签: android sharedpreferences

我想在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();

2 个答案:

答案 0 :(得分:0)

当我使用以下代码实例化SharedPreferences时似乎有效:

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

答案 1 :(得分:-1)

试试这个:

public static void saveOrEdit(Context context ,String name,
            String key , String value,Class clz){
        if(null == context || StringUtil.isEmpty(name)){
            return;
        }
        SharedPreferences preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        if(null != clz){
            if(clz.getSimpleName().equals("Boolean")){
                editor.putBoolean(key, Boolean.valueOf(value));
            }else if(clz.getSimpleName().equals("Float")){
                editor.putFloat(key, Float.valueOf(value));
            }else if(clz.getSimpleName().equals("Integer")){
                editor.putInt(key, Integer.valueOf(value));
            }else if(clz.getSimpleName().equals("Long")){
                editor.putLong(key, Long.valueOf(value));
            }else{
                editor.putString(key, value);
            }
        }else{          
            editor.putString(key, value);
        }
        //It should be submitted
        editor.commit();
    }