代码之家  ›  专栏  ›  技术社区  ›  MegaRoks

不显示带有响应的传入数据

  •  0
  • MegaRoks  · 技术社区  · 7 年前

    TextResult . 怎么做?

    private void jsonParse() {
            String url = "https://api.apixu.com/v1/current.json?key=...&q=Paris";
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                JSONArray jsonArray = response.getJSONArray("location");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject location = jsonArray.getJSONObject(i);
                                    String name = location.getString("name");
                                    String region = location.getString("region");
                                    String country = location.getString("country");
                                    TextResult.append(name + ", " + region + ", " + country + "\n\n");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
            mQueue.add(request);
        }
    

    {"location":{"name":"Paris","region":"Ile-de-France","country":"France"}}
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Jawad Malik    7 年前

    @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject jsonObject = response.getJSONObject("location");
    
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONArray location = jsonObject.getJSONArray(i);
                    String name = location.getString("name");
                    String region = location.getString("region");
                    String country = location.getString("country");
                    TextResult.append(name + ", " + region + ", " + country + "\n\n");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
        2
  •  0
  •   Vishal Sharma    7 年前
    {"location":{"name":"Paris","region":"Ile-de-France","country":"France"}}
    
    Its not a JSONArray its a JSONObject.
    
    First get the location from JSONObject.
    
    String location_value=response.get("location");
    
    JSONObject location=new JSONObject(location_value);
    
    String name = location.getString("name");
    String region = location.getString("region");
    String country = location.getString("country");
    
    TextResult.append(name + ", " + region + ", " + country + "\n\n");