简体   繁体   中英

I am getting Empty Json Object in response listner as compared to postman. How can i get JsonArray from JsonObject in response?

In java JsonObjectRequest response listner code, I am getting wrong response ie I'm getting response but it is different from that I am getting from postman.

I have tried simple json object request call using volley and in response listner I logged response.toString() .

JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET,
            url,
            jObject,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                        Log.v(TAG, "Vultus Get Resposne Version2: " + response.toString());
                }

            }

Actual output :

{"output":[]}

Expected output:

{ "output": [ " https://s3.eu-west-2.amazonaws.com/spatial-production/5cb45c3787bfbc0f6e0d0d9d/sentinel-s2-l1c/png/2019-04-01T06:07:17.318Z_ndvi.png ", " https://s3.eu-west-2.amazonaws.com/spatial-production/5cb45c3787bfbc0f6e0d0d9d/sentinel-s2-l1c/png/2019-04-01_ndvi.png ", " https://s3.eu-west-2.amazonaws.com/spatial-production/5cb45c3787bfbc0f6e0d0d9d/sentinel-s2-l1c/png/2019-04-06T06:01:36.169Z_ndvi.png ", " https://s3.eu-west-2.amazonaws.com/spatial-production/5cb45c3787bfbc0f6e0d0d9d/sentinel-s2-l1c/png/2019-04-06_ndvi.png " ] }

You have an array coming from your JSON object. You can try something like this..

JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET,
        url,
        jObject,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.v(TAG, "Vultus Get Resposne Version2: " + response.toString());

                try{
                    //String jsonStr = response.toString();
                    JSONObject jsonObj = new JSONObject(response);

                    JSONArray jsonData = jsonObj.getJSONArray("output");
                    int length = jsonData.length();

                    ArrayList<String> urls = new ArrayList<>();
                    for(int i = 0; i < length; i++) {
                        String url = jsonData.getString(i);
                        urls.add(url);
                    }
                } catch(JSONException e) {
                    e.printStackTrace();
                }
            }
        }

Finally, use your ArrayList wherever you need it.

Something like this,

Url url1 = urls.get(0);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM