gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:33:34 24 4
gpt4 key购买 nike

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

这是我的代码:

    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 + "\n\n");

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();
}

}
}

最佳答案

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

看这个,

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 + "\n\n");
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 + "\n\n");

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();
}

}
}

关于java - 如何将 json 数据存储到共享首选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com