Alireza Noorali Alireza Noorali - 5 months ago 15
JSON Question

How to initialize a JSONArray and use in other methods

I'm going to initialize a JSONArray and set it as input of a method which copies that JSONArray into an ArrayList.

This is my method:

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;
}


and this is my Code:

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);


but when I use jsonArray as the method input It says that jsonArray is null !
so I debugged the code and I understood that the sequence of execution is like this:

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));


and in Line 4 the jsonArray is null !

Any Idea !?

Answer

Your JSONArray isn't being instantiated until you get the callback in onResponse(String), so until your StringRequest is finished the jsonArray will be null. instead move the inititlization into the callback like so:

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);