Nguyen Duy Minh
Nguyen Duy Minh

Reputation: 23

Android JSONArray length value 0

I'm trying to get my JSONArray length value which I used for my spinner but it always returns 0 even when the spinner has more value than 0. Here is my code

Class ConfigJSON

public class ConfigJSON {
      public static int value;
      public static final String JSON_ARRAY = "result";
}

Main Activity

protected void onCreate{
    getData();
    new UpdateUser.GetUserInfo(UpdateUser.this).execute();
}

public void getData() {
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            JSONObject j = null;
            try {
                j = new JSONObject(response);
                result = j.getJSONArray(ConfigJSON.JSON_ARRAY);
                ConfigJSON.value = result.length();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private class GetUserInfo extends AsyncTask<Void, Void, Void> {

    //Do Stuff before PostExecute

    @Override
    protected void onPostExecute(Void result) {

        //The textview will output the JSONArray length
        TextView msg = (TextView) findViewById(R.id.txtmsg);
        msg.setText(Integer.toString(ConfigJSON.value));

        int index;
        for (int i=0;i<ConfigJSON.value;i++){
            if (spinner.getItemAtPosition(i).toString().equals("stuff")){
                index = i;
                spinner.setSelection(index);
            }
        }
    }
}

The whole point of this code is to get the JSONArray value so I can have a loop to setSelection in spinner but the value is always 0 so the spinner will always select the first value. I change the value in the loop like this to test if the setSelection is broken

for (int i=0;i<3;i++){
         if (spinner.getItemAtPosition(i).toString().equals("stuff")){
           index = i;
             spinner.setSelection(index);
         }
}

The spinner works fine so I remove

new UpdateUser.GetUserInfo(UpdateUser.this).execute();

in onCreate and put it in getData

public void getData() {
    //Same as the getData above but only this line at the end
    new UpdateUser.GetUserInfo(UpdateUser.this).execute();
}

because I want to make sure the GetUserInfo will run after the value from getData is set. But it still returns 0 even when the getData have 3 values in it. So how do I get the result.length() value and use it in the GetUserInfo?

Upvotes: 2

Views: 393

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

I want to make sure the GetUserInfo will run after the value from getData is set

Volley network calls are asynchronous so execute the task after the network call completion, as

public void getData() {
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            JSONObject j = null;
            try {
                j = new JSONObject(response);
                result = j.getJSONArray(ConfigJSON.JSON_ARRAY);
                ConfigJSON.value = result.length();
                new UpdateUser.GetUserInfo(UpdateUser.this).execute();
                //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

Upvotes: 3

Related Questions