如何初始化JSONArray并在其他方法中使用

时间:2016-06-09 09:15:21

标签: java android arrays json android-volley

我将初始化一个JSONArray并将其设置为将JSONArray复制到ArrayList的方法的输入。

这是我的方法:

    public ArrayList<String> copyJSONArrayToArrayList(JSONArray jArray){
    ArrayList<String> arrayList = new ArrayList<> ();
    for (int i = 0; i < jArray.length(); i++) {
        try {
            arrayList.add(jArray.getJSONObject(i).getString("city"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return arrayList;
}

这是我的代码:

    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, WS_GET_CITY, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressbar.setVisibility(View.GONE);

            try {
                jsonObject = new JSONObject(response);
                jsonArray = jsonObject.getJSONArray("getCity");

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
            progressbar.setVisibility(View.GONE);
        }
    });

    queue.add(stringRequest);


    ac_srccity = (AutoCompleteTextView) findViewById(R.id.ac_srccity);
    ArrayAdapter<String> adapterSrc = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, copyJSONArrayToArrayList(jsonArray));
    ac_srccity.setAdapter(adapterSrc);

但是当我使用jsonArray作为方法输入它说jsonArray为null! 所以我调试了代码,我明白执行的顺序是这样的:

1

    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

2。

    queue.add(stringRequest);

3

 ac_srccity = (AutoCompleteTextView) findViewById(R.id.ac_srccity);

4

ArrayAdapter<String> adapterSrc = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, copyJSONArrayToArrayList(jsonArray));

并且在第4行中,jsonArray为空!

任何想法!?

2 个答案:

答案 0 :(得分:2)

JSONArray收到回调之前,您的onResponse(String)尚未实例化,因此在StringRequest完成之前,jsonArray将为null。 而是将inititlization移动到回调中,如下所示:

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, WS_GET_CITY, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        progressbar.setVisibility(View.GONE);

        try {
            jsonObject = new JSONObject(response);
            jsonArray = jsonObject.getJSONArray("getCity");
            ac_srccity = (AutoCompleteTextView) findViewById(R.id.ac_srccity);
            ArrayAdapter<String> adapterSrc = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, copyJSONArrayToArrayList(jsonArray));
            ac_srccity.setAdapter(adapterSrc);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
        progressbar.setVisibility(View.GONE);
    }
});

queue.add(stringRequest);

答案 1 :(得分:0)

以下代码就是答案:

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, WS_GET_CITY, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressbar.setVisibility(View.GONE);

            try {
                jsonObject = new JSONObject(response);
                jsonArray = jsonObject.getJSONArray("getCity");
                ArrayAdapter<String> adapterSrc = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, copyJSONArrayToArrayList(jsonArray));
                ac_srccity.setAdapter(adapterSrc);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
            progressbar.setVisibility(View.GONE);
        }
    });

    queue.add(stringRequest);