JSONArray response with Volley for Android

Theo

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.

Kushal Sharma

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parse Android Volley JSONArray response

From Dev

Android Volley POST JsonObject and get JsonArray or JsonObject or Other response

From Dev

How to GET volley JSONArray in Android

From Dev

Android Volley: gzip response

From Dev

Not getting response Volley android

From Dev

Volley Response Issue Android

From Dev

Android Volley JSON response

From Dev

Android Volley Request Response not in order

From Dev

Android Volley. JsonArray JsonException end of input at character 0

From Dev

Post JSONArray to webservice and get the JSONObject Response in android

From Dev

Volley library for Android parse xml response?

From Dev

Android Volley access http response header fields

From Dev

Android Volley access caching response data

From Dev

finding volley requests response time android

From Dev

Null Point Exception - Android Volley Response

From Dev

Android Volley: Unexpected response code 405

From Dev

Android - Wait for Volley response to complete and continue executing

From Dev

Android, Volley Request, the response is blocking main thread

From Dev

Android, Volley Request, the response is blocking main thread

From Dev

Android Volley access http response header fields

From Dev

Android Volley access caching response data

From Dev

Why Android Volley Response String is so short

From Dev

Android Volley return value from response

From Dev

Android Kotlin - Volley Unexpected response code 400

From Dev

android call function after volley response in AsyncTask

From Dev

Error Android Volley Unexpected response code 400

From Dev

Volley jsonArray Request not working

From Dev

How to extend Volley Response (to fix There is no default constructor available in 'com.android.volley.Response')

From Dev

Passing parameters in Volley JSONArray Request

Related Related

  1. 1

    Parse Android Volley JSONArray response

  2. 2

    Android Volley POST JsonObject and get JsonArray or JsonObject or Other response

  3. 3

    How to GET volley JSONArray in Android

  4. 4

    Android Volley: gzip response

  5. 5

    Not getting response Volley android

  6. 6

    Volley Response Issue Android

  7. 7

    Android Volley JSON response

  8. 8

    Android Volley Request Response not in order

  9. 9

    Android Volley. JsonArray JsonException end of input at character 0

  10. 10

    Post JSONArray to webservice and get the JSONObject Response in android

  11. 11

    Volley library for Android parse xml response?

  12. 12

    Android Volley access http response header fields

  13. 13

    Android Volley access caching response data

  14. 14

    finding volley requests response time android

  15. 15

    Null Point Exception - Android Volley Response

  16. 16

    Android Volley: Unexpected response code 405

  17. 17

    Android - Wait for Volley response to complete and continue executing

  18. 18

    Android, Volley Request, the response is blocking main thread

  19. 19

    Android, Volley Request, the response is blocking main thread

  20. 20

    Android Volley access http response header fields

  21. 21

    Android Volley access caching response data

  22. 22

    Why Android Volley Response String is so short

  23. 23

    Android Volley return value from response

  24. 24

    Android Kotlin - Volley Unexpected response code 400

  25. 25

    android call function after volley response in AsyncTask

  26. 26

    Error Android Volley Unexpected response code 400

  27. 27

    Volley jsonArray Request not working

  28. 28

    How to extend Volley Response (to fix There is no default constructor available in 'com.android.volley.Response')

  29. 29

    Passing parameters in Volley JSONArray Request

HotTag

Archive