Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONArray response with Volley for Android

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.

like image 261
Theo Avatar asked May 22 '15 19:05

Theo


People also ask

What is a JsonArray?

JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.

What is JSON array in Android?

JSON (Javascript Object Notation) is a programming language . It is minimal, textual, and a subset of JavaScript. It is an alternative to XML. Android provides support to parse the JSON object and array.

How JSON array looks like?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.


2 Answers

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.

like image 185
Kushal Sharma Avatar answered Sep 23 '22 02:09

Kushal Sharma


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!!!

like image 25
Theo Avatar answered Sep 24 '22 02:09

Theo