之前我从未使用sharedprefrences
,现在我想在sharedprefrences
中存储身份验证令牌。这是我的代码,我在hawkerauthToken
获取用户令牌,这是一个字符串,我想将此令牌存储在sharedprefrences
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, LOGIN_URL, object,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("v_response", response.toString(4));
Log.d("resopnse", response.toString());
hawkerauthToken = String.valueOf(response.get("token"));
Log.d("tokeennnn" , hawkerauthToken);
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:2)
<强> SharedPreferences 强>
保存强>
SharedPreferences preferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
preferences.edit().putString("token", hawkerauthToken).commit();
<强>检索:强>
String token = preferences.getString("token","");
答案 1 :(得分:0)
尝试以下代码
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(inboxFragment.context);
sp.edit().putString("keyname", "keyValue").apply();