java - 如何将 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 + "\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();
}
}
}
解决方案
如果您只想在第二个活动中使用数据,那么只需使用意图传递数据,并且意图有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 + "\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 - Java中的参数/方法
- c# - 可以是 int 或 string 的可空泛型类型
- python - 密集层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 3,但接收到形状为 (None, 1) 的输入
- wireguard - Wireguard vpn - 连接后,我可以访问“服务器”网络但不能访问互联网
- google-authentication - 在角度刷新组件 - 谷歌身份验证 - 身份验证后导航到内部页面
- git - 如何解码 git-log 中的转义文件名?
- hibernate - JPA @OneTomany @ManyToOne 多个实例
- file - NestJS 中的同步队列
- godot - Godot:我怎样才能在行进方向上旋转玩家?
- asp.net-core-mvc - ASP.NET Core 5 MVC,未授权的 JSON 响应