Java 如何将json数据存储到共享首选项?
我需要将Java 如何将json数据存储到共享首选项?,java,android,json,android-studio,sharedpreferences,Java,Android,Json,Android Studio,Sharedpreferences,我需要将userid存储在共享首选项中,并在第二个活动中使用它,但我不知道如何执行。如何仅存储id并在第二个活动中调用它 这是我的密码: JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override
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();
}
}
}
JsonObjectRequest-request=newjsonobjectrequest(request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
Boolean-esito=response.getBoolean(“esito”);
如果(esito){
JSONArray JSONArray=response.getJSONArray(“Dati”);
Log.d(“JSON”,String.valueOf(esito));
for(int i=0;i
如果您只想在第二个活动中使用数据,那么只需使用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 + "\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();
}
}
}
JsonObjectRequest-request=newjsonobjectrequest(request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
Boolean-esito=response.getBoolean(“esito”);
如果(esito){
JSONArray JSONArray=response.getJSONArray(“Dati”);
Log.d(“JSON”,String.valueOf(esito));
for(int i=0;i
使用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");
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();
}
}
}
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 + "\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();
}
}
}