如何将 json 数据存储到共享首选项?



我需要将userid存储在共享首选项中并在第二个活动中使用它,但我不知道该怎么做。如何仅存储id并在第二个活动中调用它?

这是我的代码:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Boolean esito = response.getBoolean("Esito");
if (esito) {
JSONArray jsonArray = response.getJSONArray("Dati");
Log.d("JSON", String.valueOf(esito));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dato = jsonArray.getJSONObject(i);
String id = dato.getString("id");
String fullname = dato.getString("fullname");
String username = dato.getString("username");
String password = dato.getString("password");

//mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "nn");
startActivity(new Intent(getApplicationContext(),LoggedActivity.class));
}
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}

如果您只想在第二个活动中使用数据,则只需使用 Intent 传递数据,并且 Intent 具有putExtra()方法,通过使用此方法,您可以在活动之间传递数据。

看到这里,

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Boolean esito = response.getBoolean("Esito");
if (esito) {
JSONArray jsonArray = response.getJSONArray("Dati");
Log.d("JSON", String.valueOf(esito));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dato = jsonArray.getJSONObject(i);
String id = dato.getString("id");
String fullname = dato.getString("fullname");
String username = dato.getString("username");
String password = dato.getString("password");

//mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "nn");
Intent intent = new Intent(getApplicationContext(),LoggedActivity.class)
intent.putExtra("id",id);
startActivity(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}

但是,如果您真的想将数据存储在SharedPreference中,请使用它,

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Boolean esito = response.getBoolean("Esito");
if (esito) {
JSONArray jsonArray = response.getJSONArray("Dati");
Log.d("JSON", String.valueOf(esito));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dato = jsonArray.getJSONObject(i);
String id = dato.getString("id");
String fullname = dato.getString("fullname");
String username = dato.getString("username");
String password = dato.getString("password");
SharedPreferences.Editor editor = getSharedPreferences(YOUR_SHARED_PEREFENCE_NAME, MODE_PRIVATE).edit();
editor.putString("id", id);
editor.commit();
//mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "nn");
startActivity(new Intent(getApplicationContext(),LoggedActivity.class));
}
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Boolean esito = response.getBoolean("Esito");
if (esito) {
JSONArray jsonArray = response.getJSONArray("Dati");
Log.d("JSON", String.valueOf(esito));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dato = jsonArray.getJSONObject(i);
String id = dato.getString("id");
String fullname = dato.getString("fullname");
String username = dato.getString("username");
String password = dato.getString("password");
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("id", id);
editor.putString("fullname", fullname);
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
//mTextViewResult.append(id + ", " + fullname +  ", " + username +  ", " + password + "nn");
startActivity(new Intent(getApplicationContext(),LoggedActivity.class));
}
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}

最新更新