如何在volley中获取json对象数据

uelo1irk  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(205)

json数据使用
当前,我希望从对象条件“text”值中获取数据,以便与“icons”图像一起显示在应用程序上。如何从json数组循环获取数据并使用volley库实现到应用程序中?
私有数据(字符串国家){

// Create a String request
    // using Volley Library
    String url = "https://api.weatherapi.com/v1/current.json?key=20cc9a9b0a4243b4be970612211704&q="+countries+"&aqi=no";

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response)
                {

                    // Handle the JSON object and
                    // handle it inside try and catch
                    try {

                        // Creating object of JSONObject
                        JSONObject jsonObject = new JSONObject(response);
                        country.setText("Region: "+jsonObject.getJSONObject("location").getString("name"));
                        temperature.setText("Temperature °C: "+jsonObject.getJSONObject("current").getString("temp_c"));
                        temperatureF.setText("Temperature °F: "+jsonObject.getJSONObject("current").getString("temp_f"));
                        time.setText("Current Time: "+jsonObject.getJSONObject("location").getString("localtime"));
                        countryZone.setText("Current Zone: "+jsonObject.getJSONObject("location").getString("tz_id"));

                        JSONObject object = new JSONObject("current");
                        JSONArray jsonArray = object.getJSONArray("condition");
                        for(int i = 0;i<jsonArray.length();i++){
                            object = (JSONObject) jsonArray.get(i);
                             uri = object.getString("icon");
                        }
                        Picasso.get().load(uri).into(setImage);

                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    Toast.makeText(
                            MainActivity.this,
                            error.getMessage(),
                            Toast.LENGTH_SHORT)
                            .show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(request);
    requestQueue.getCache().clear();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题