Как получить и отобразить объекты в массиве JSON в Android Studio - PullRequest
0 голосов
/ 12 декабря 2018

Я работаю над созданием приложения погоды в Android Studio, и я застрял.Я разработал приложение для получения прогноза погоды на один день, но я хочу добавить второе действие для отображения пятидневного прогноза.Строка API похожа и возвращает массив JSON, единственное отличие состоит в том, что она возвращает 5 массивов JSON вместо одного.

I believe I can adapt the current code I'm using to retrieve one day for 5 days, but I'm not sure how to do that. 

Here is the current method I developed for the single day of weather. 

Here is the string for the 5 day forecast https://api.openweathermap.org/data/2.5/forecast?q=Indianapolis,us&mode=xml&appid=api-key

Любая помощь очень ценится.

public void getWeather(String city, String units) {


        String url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=" + units + "&appid=api-key";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject responseObject) {
                        //tempTextView.setText("Response: " + response.toString());
                        Log.v("Weather", "Response: " + responseObject.toString());

                        try {
                            JSONObject mainJSONObject = responseObject.getJSONObject("main");
                            JSONArray weatherArray = responseObject.getJSONArray("weather");
                            JSONObject firstWeatherObject = weatherArray.getJSONObject(0);

                            String temp = Integer.toString((int) Math.round(mainJSONObject.getDouble("temp")));
                            String weatherDescription = firstWeatherObject.getString("description");
                            String city = responseObject.getString("name");

                            tempTextView.setText(temp);
                            weatherDescTextView.setText(weatherDescription);
                            cityTextView.setText(city);

                            int iconResourceId = getResources().getIdentifier("icon_" + weatherDescription.replace(" ", ""), "drawable", getPackageName());
                            weatherImageView.setImageResource(iconResourceId);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO: Handle error

                    }
                });

// Access the RequestQueue through your singleton class.
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(jsonObjectRequest);

    }
...