【问题标题】:How to store json data to shared preferences?如何将 json 数据存储到共享首选项?
【发布时间】:2019-09-01 11:24:22
【问题描述】:

我需要将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();
                    }

                }
            }

【问题讨论】:

标签: java android json android-studio sharedpreferences


【解决方案1】:

如果你只想在第二个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();
                    }

                }
            }

【讨论】:

    【解决方案2】:
    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();
                        }
    
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 2018-06-10
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多