HashMap值在JsonArrayRequest之外为null

时间:2019-04-12 16:21:44

标签: android

我想访问 JsonArrayRequest 外部的HashMap值。它们可以在JsonArrayRequest中访问,但是在JsonArrayRequest之外它们为空。我该如何解决?

        final List<HashMap<String, String>> list = new ArrayList<>();
        final HashMap<String, String> map = new HashMap();

        JsonArrayRequest request1 = new JsonArrayRequest(Request.Method.GET,
                url,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject obj = response.getJSONObject(i);
                                map.put("email", obj.getString("email"));
                                map.put("phone", obj.getString("phone"));
                                list.add(map);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        Toast.makeText(HomeActivity.this, "inside "+map.get("email")+"-"+map.get("phone"), Toast.LENGTH_SHORT).show();

                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(HomeActivity.this, "Error"+error, Toast.LENGTH_SHORT).show();

                    }
                });

Toast.makeText(HomeActivity.this, "outside "+map.get("email")+"-"+map.get("phone"), Toast.LENGTH_SHORT).show();

1 个答案:

答案 0 :(得分:1)

您必须等待响应者才能获取数据:

final HashMap<String, String> map = new HashMap();
    final List<HashMap<String, String>> list = new ArrayList<>();
    JsonArrayRequest request1 = new JsonArrayRequest(Request.Method.GET,
            "",
            null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);

                            map.put("phone", obj.getString("phone"));
                            list.add(map);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    printToast(map.get("email"));
                    Toast.makeText(HomeActivity.this, "inside "+map.get("email")+"-"+map.get("phone"), Toast.LENGTH_SHORT).show();

                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(HomeActivity.this, "Error"+error, Toast.LENGTH_SHORT).show();

                }
            });


private void printToast(String email){
    Toast.makeText(HomeActivity.this, "outside "+email,Toast.LENGTH_SHORT).show();

}