-1

Hi am trying to retrieve hourly weather data from openweatherAPI's and add them in recycleView. I have no idea what to do,i tried many ways from tutorials but i ended up getting this error.

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

My goal is to get date, icon and temperature from JSON.

enter image description here

here are my JSON data

{
    "lat": -8.9414,
    "lon": 33.4139,
    "timezone": "Africa/Dar_es_Salaam",
    "timezone_offset": 10800,
    "current": {
        "dt": 1652079957,
        "sunrise": 1652068232,
        "sunset": 1652110505,
        "temp": 19.7,
        "feels_like": 19.37,
        "pressure": 1018,
        "humidity": 63,
        "dew_point": 12.47,
        "uvi": 5.14,
        "clouds": 13,
        "visibility": 10000,
        "wind_speed": 1.92,
        "wind_deg": 131,
        "wind_gust": 2.91,
        "weather": [
            {
                "id": 801,
                "main": "Clouds",
                "description": "few clouds",
                "icon": "02d"
            }
        ]
    },
    "hourly": [
        {
            "dt": 1652079600,
            "temp": 19.7,
            "feels_like": 19.37,
            "pressure": 1018,
            "humidity": 63,
            "dew_point": 12.47,
            "uvi": 5.14,
            "clouds": 13,
            "visibility": 10000,
            "wind_speed": 1.92,
            "wind_deg": 131,
            "wind_gust": 2.91,
            "weather": [
                {
                    "id": 801,
                    "main": "Clouds",
                    "description": "few clouds",
                    "icon": "02d"
                }
            ],
            "pop": 0
        },
        {
            "dt": 1652083200,
            "temp": 20.09,
            "feels_like": 19.75,
            "pressure": 1018,
            "humidity": 61,
            "dew_point": 12.34,
            "uvi": 8.35,
            "clouds": 19,
            "visibility": 10000,
            "wind_speed": 1.53,
            "wind_deg": 140,
            "wind_gust": 2.16,
            "weather": [
                {
                    "id": 801,
                    "main": "Clouds",
                    "description": "few clouds",
                    "icon": "02d"
                }
            ],
            "pop": 0
        },
        {
            "dt": 1652086800,
            "temp": 20.96,
            "feels_like": 20.6,
            "pressure": 1017,
            "humidity": 57,
            "dew_point": 12.13,
            "uvi": 10.72,
            "clouds": 33,
            "visibility": 10000,
            "wind_speed": 1.42,
            "wind_deg": 154,
            "wind_gust": 2.04,
            "weather": [
                {
                    "id": 802,
                    "main": "Clouds",
                    "description": "scattered clouds",
                    "icon": "03d"
                }
            ],
            "pop": 0
        },
        {
            "dt": 1652090400,
            "temp": 21.93,
            "feels_like": 21.59,
            "pressure": 1016,
            "humidity": 54,
            "dew_point": 12.21,
            "uvi": 11.28,
            "clouds": 48,
            "visibility": 10000,
            "wind_speed": 1.47,
            "wind_deg": 160,
            "wind_gust": 2.21,
            "weather": [
                {
                    "id": 802,
                    "main": "Clouds",
                    "description": "scattered clouds",
                    "icon": "03d"
                }
            ],
            "pop": 0
        },
        {
            "dt": 1652094000,
            "temp": 22.8,
            "feels_like": 22.44,
            "pressure": 1015,
            "humidity": 50,
            "dew_point": 11.84,
            "uvi": 9.89,
            "clouds": 63,
            "visibility": 10000,
            "wind_speed": 1.72,
            "wind_deg": 159,
            "wind_gust": 2.34,
            "weather": [
                {
                    "id": 803,
                    "main": "Clouds",
                    "description": "broken clouds",
                    "icon": "04d"
                }
            ],
            "pop": 0
        },
        {
            "dt": 1652097600,
            "temp": 23.29,
            "feels_like": 22.95,
            "pressure": 1014,
            "humidity": 49,
            "dew_point": 11.53,
            "uvi": 7.06,
            "clouds": 74,
            "visibility": 10000,
            "wind_speed": 2.09,
            "wind_deg": 156,
            "wind_gust": 2.35,
            "weather": [
                {
                    "id": 803,
                    "main": "Clouds",
                    "description": "broken clouds",
                    "icon": "04d"
                }
            ],
            "pop": 0
        }]
}.............

My modeClass

public class Conditions {

    String dt,temp,icon;

    
    @Override
    public String toString() {
        return "Conditions{" +
                "dt='" + dt + '\'' +
                ", temp='" + temp + '\'' +
                '}';
    }


    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }

    public void setDt(String dt) {
        this.dt = dt;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }


    public String getDt() {
        return dt;
    }

    public String getTemp() {
        return temp;
    }


}

API class

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;

public interface WeatherUpdateApi {
    @GET
    Call<ArrayList<Conditions>> getAllUpdates(@Url String url);
}

Call Method from MainActivity

 // Weather issues
    private void addAll() {
        // Weather issues
        String url = getString(R.string.MyApi);
        String BASE_URL = "https://api.openweathermap.org";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        WeatherUpdateApi weatherApiUpdates = retrofit.create(WeatherUpdateApi.class);
        Call<ArrayList<Conditions>> call = weatherApiUpdates.getAllUpdates(url);
        call.enqueue(new Callback<ArrayList<Conditions>>() {
            @Override
            public void onResponse(Call<ArrayList<Conditions>> call, Response<ArrayList<Conditions>> response) {

                conditionsList.addAll(response.body());
                recycleAdapterWeather = new RecycleAdapterWeather(getApplicationContext(), conditionsList);
                weather_summary.setAdapter(recycleAdapterWeather);

            }

            @Override
            public void onFailure(Call<ArrayList<Conditions>> call, Throwable t) {
               Log.e("error", t.toString());
            }
        });

    }

View full code from my Github

Joshua Simon
  • 239
  • 1
  • 2
  • 8
  • https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2 – a_local_nobody May 09 '22 at 09:15

0 Answers0