与邮递员相比,我在响应列表器中得到Empty Json Object。我如何从JsonObject获取JsonArray作为响应?

时间:2019-04-16 10:08:42

标签: java android json api

在Java JsonObjectRequest响应列表代码中,我得到了错误的响应,即我得到了响应,但这与从邮递员那里得到的是不同的。

我尝试使用截击尝试简单的json对象请求调用,并在响应列表器中登录了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());
                }

            }

实际输出:

  

{“输出”:[]}

预期输出:

  

{       “输出”:[           “ 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”       ]   }

1 个答案:

答案 0 :(得分:1)

您有一个来自JSON对象的数组。您可以尝试这样的事情。

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();
                }
            }
        }

最后,在需要的地方使用ArrayList。

这样的事情,

Url url1 = urls.get(0);

希望这会有所帮助。