使用Retrofit如何处理这个json响应表单?

时间:2016-09-11 07:56:02

标签: android retrofit

当我将我的id和密码发布(POST方法)到我的REST api中时,它会重新启动

{
  "key": "ef91707f0434a1a2a7581dd3f4f48d3bdad717"
}  

我希望在我的共享偏好中保存此ef91707f0434a1a2a7581dd3f4f48d3bdad717

ReferSharedPreference.java(这是为了方便使用共享偏好对象)

public class ReferSharedPreference {

    public final static String REF_LOGIN_KEY = "REF_LOGIN_KEY";

    static Context mContext;

    public ReferSharedPreference(Context c) {
        mContext = c;
    }

    public void put(String key, String value) {
        SharedPreferences pref = mContext.getSharedPreferences(REF_PREF_NAME,
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, value);
        editor.commit();
    public String getValue(String key, String dftValue) {
        SharedPreferences pref = mContext.getSharedPreferences(REF_PREF_NAME,
                Activity.MODE_PRIVATE);
        try {
            return pref.getString(key, dftValue);
        } catch (Exception e) {
            return dftValue;
        }
    }

LoginActivity.java

        Call<ResponseBody> getkey = loginApiService.getkey(loginData);
        getkey.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.code() == 200) {
                    ReferSharedPreference ref_key = new ReferSharedPreference(getApplicationContext());
                    ref_key.put(ReferSharedPreference.REF_LOGIN_KEY, response.body().toString());
                    Toast.makeText(getApplicationContext(), response.body().toString(), Toast.LENGTH_LONG);
                    Toast.makeText(getApplicationContext(), ref_key.getValue(ReferSharedPreference.REF_LOGIN_KEY, "default").toString(), Toast.LENGTH_SHORT);
                    }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }

在上面的代码(LoginActivity.java)中,

                Toast.makeText(getApplicationContext(), response.body().toString(), Toast.LENGTH_LONG);
                Toast.makeText(getApplicationContext(), ref_key.getValue(ReferSharedPreference.REF_LOGIN_KEY, "default").toString(), Toast.LENGTH_SHORT);

不要工作。即使我的腻子说HTTP/1.1" 200 50。我了解到这意味着请求进展顺利。

问题 如何在共享偏好中保存ef91707f0434a1a2a7581dd3f4f48d3bdad717? 我想要保存它像Token ef91707f0434a1a2a7581dd3f4f48d3bdad717

1 个答案:

答案 0 :(得分:2)

您可以实现在SharedPreferences中保存和加载字符串值的方法,如下所示:

String key = "Token";
String value = "ef91707f0434a1a2a7581dd3f4f48d3bdad717";
//saving your key
sSavePreferences(getApplicationContext(), key, value);

//accessing your key
String mKey = sLoadSavedPreferencesString(getApplicationContext, key);

方法sSavePreferences(Context context, String key, String value)

public static void sSavePreferences(Context context, String key, String value) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

方法String sLoadSavedPreferencesString(Context context, String key)

public static String sLoadSavedPreferencesString(Context context, String key) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);

        String value = sharedPreferences.getString(key, "No Name Found");
        return value;
    }

<强>更新 您可以在获得JSON Oject作为响应时获取该值,如此

String value; try { value = response.getString("key"); } catch (JSONException e) { e.printStackTrace(); }

class MyKey {
        String key;
    }
...

Call<ResponseBody> getkey = loginApiService.getkey(loginData);
    getkey.enqueue(new Callback<ResponseBody>()
    {
        @Override
        public void onResponse (Call < ResponseBody > call, Response < ResponseBody > response){
            if (response.code() == 200) {
                ReferSharedPreference ref_key = new ReferSharedPreference(getApplicationContext());
                ref_key.put(ReferSharedPreference.REF_LOGIN_KEY, response.body().toString());

                String jsonString = response.body().toString();
                Gson gson = new Gson();
                Key result = gson.fromJson(jsonString, MyKey.class);
                Toast.makeText(getApplicationContext(), result.key, Toast.LENGTH_LONG);

                Toast.makeText(getApplicationContext(), ref_key.getValue(ReferSharedPreference.REF_LOGIN_KEY, "default").toString(), Toast.LENGTH_SHORT);
            }
        }

        @Override
        public void onFailure (Call < ResponseBody > call, Throwable t){
        }

    }

在上面的代码中使用了GSON。 GSON可用于将Java对象序列化和反序列化为(和)JSON。使用类MyKey,GSON将对象序列化和反序列化,这样就可以解决问题

String jsonString = response.body().toString();
            Gson gson = new Gson();
            Key result = gson.fromJson(jsonString, MyKey.class);
            Toast.makeText(getApplicationContext(), result.key, Toast.LENGTH_LONG);