JSONArray response with Volley for Android

39,487

Solution 1

I the problem might be - you are already getting response as a JSONArray.

So, you can Call

JSONObject jresponse = response.getJSONObject(0);

and if you have more than 1 object in response, then

for(int i = 0; i < response.length(); i++){
    JSONObject jresponse = response.getJSONObject(i);
    String nickname = jresponse.getString("nickname");
    Log.d("nickname", nickname);
}

Remove this :

               try {
                    JSONArray jsonArray = new JSONArray(response);

                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jresponse = 
            jsonArray.getJSONObject(i);
            String nickname =                                 
       jresponse.getString("nickname");
                        Log.d("nickname",nickname);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

and add :

try {
    JSONObject jresponse = response.getJSONObject(0);
    String nickname = jresponse.getString("nickname");
    Log.d("nickname",nickname);
}catch (JSONException e) {
    e.printStackTrace();
}

The Code looks good, however i think you might be missing a call to add jsonObjReq1 in the request queue. I would suggest to use Singleton Pattern.

Solution 2

Fixed!!!

                @Override
                public void onResponse(JSONArray response) {
                    Log.d("TAG", response.toString());

                    try {

                        Log.d("JsonArray",response.toString());
                        for(int i=0;i<response.length();i++){
                            JSONObject jresponse = response.getJSONObject(i);
                            String nickname = jresponse.getString("nickname");
                            Log.d("nickname",nickname);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //pDialog.dismiss();

                }

There was no need to create a new JSONArray. It was created inside the onResponse() method. The next project I am assigned to do is going to have more complicate webservices.omg!!!

Share:
39,487
Theo
Author by

Theo

Beng Honours Degree in Electical in Electronic Engineering,University of Sussex UK MsC in Electrical Power,University of Newcastle Upon Tyne UK. OCJP SE 6 My github account. Android Projects published on google play Easy News. Easy Movies.

Updated on July 23, 2022

Comments

  • Theo
    Theo almost 2 years

    I am posting some data into the database using Volley and I get the following jsonarray response.

    [
      {
      "nickname":"panikos",
      "username":"[email protected]",
      "user_type":"LEADER",
      "latest_steps":"0"
      }
    ]
    

    This is a sample of my code that unfortunately doesn't log out or debug the variable of "nickname" object:(.

    final JsonArrayRequest jsonObjReq1 = new 
    JsonArrayRequest(AppConfig.URL_GET_TEAM, jsonObject,
                new com.android.volley.Response.Listener<JSONArray>() {
    
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d("TAG", response.toString());
    
                        try {
                            JSONArray jsonArray = new JSONArray(response);
    
                            for(int i=0;i<jsonArray.length();i++){
                                JSONObject jresponse = 
                    jsonArray.getJSONObject(i);
                    String nickname =                                 
               jresponse.getString("nickname");
                                Log.d("nickname",nickname);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //pDialog.dismiss();
    
                    }
                }, new com.android.volley.Response.ErrorListener() {
    
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("TAG", "Error: " + error.getMessage());
                //pDialog.dismiss();
    
            }
        }) {
    
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
    
    
        };
    

    Any ideas? Am I missing something?

    Thanks.

  • Theo
    Theo almost 9 years
    Thanks for the reply. Yes jsonObjReq1 is in the queue.
  • Theo
    Theo almost 9 years
    AppController.getInstance().addToRequestQueue(jsonObjReq1, tag_json_obj);
  • Theo
    Theo almost 9 years
    But I don't whats wrong and can't read the nickname object. It's so annoying!
  • Theo
    Theo almost 9 years
    nope. I don't get any errors. I just put a breakpoint inside the onErrorResponse method and so nothing.
  • Theo
    Theo almost 9 years
    The breakpoint in JSONArray jsonArray = new JSONArray(response); gives the JSON Array response. So it gets there. After that things are getting messy.
  • Kushal Sharma
    Kushal Sharma almost 9 years
    I think if the app is not crashing, but must be leaving a volley log. can you show the log
  • Theo
    Theo almost 9 years
    org.json.JSONException: Not a primitive array: class org.json.JSONArray
  • Theo
    Theo almost 9 years
    at app.victory.walking.thewalkingviktory.InvitationBoard$4.onRe‌​sponse(InvitationBoa‌​rd.java:189)
  • Kushal Sharma
    Kushal Sharma almost 9 years
    You don't have to and your own question .. Please accept my and if it was helpful
  • impossible
    impossible almost 8 years
    Hey, I am getting can not resolve getJSONObject(int). Any guesses?