首页 > 解决方案 > 如何将 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();
                    }

                }
            }

标签: javaandroidjsonandroid-studiosharedpreferences

解决方案


如果您只想在第二个活动中使用数据,那么只需使用意图传递数据,并且意图有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();
                    }

                }
            }

推荐阅读