Api (Interface)
package com.example.openweathermap;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface Api {
String BASE_URL="https://samples.openweathermap.org/data/2.5/";
@GET("weather/")
Call<WeatherResponse> getWeatherDetails(@Query("api_key")String api_key);
}
My Model Class
Here is my model class which consists of getters and setters
package com.example.openweathermap;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Weather {
@SerializedName("coord")
private String coord;
@SerializedName("lon")
private String lon;
@SerializedName("lat")
private String lat;
@SerializedName("weather")
private List<Integer> weather = new ArrayList<Integer>();
@SerializedName("description")
private String description;
@SerializedName("base")
private String base;
@SerializedName("main")
private String main;
@SerializedName("temp")
private String temp;
@SerializedName("pressure")
private String pressure;
@SerializedName("humidity")
private Integer humidity;
@SerializedName("temp_min")
private String temp_min;
@SerializedName("temp_max")
private String temp_max;
@SerializedName("visibility")
private String visibility;
@SerializedName("wind")
private String wind;
@SerializedName("speed")
private String speed;
@SerializedName("deg")
private String deg;
@SerializedName("clouds")
private String clouds;
@SerializedName("all")
private String all;
@SerializedName("dt")
private String dt;
@SerializedName("sys")
private String sys;
@SerializedName("type")
private String type;
@SerializedName("id")
private String id;
@SerializedName("message")
private String message;
@SerializedName("country")
private String country;
@SerializedName("sunrise")
private String sunrise;
@SerializedName("sunset")
private String sunset;
public Weather(String description) {
this.description = description;
}
public Weather(String coord, String lon, String lat, List<Integer> weather, String base, String main, String temp, String pressure, Integer humidity, String temp_min, String temp_max, String visibility, String wind, String speed, String deg, String clouds, String all, String dt, String sys, String type, String id, String message, String country, String sunrise, String sunset) {
this.coord = coord;
this.lon = lon;
this.lat = lat;
this.weather = weather;
this.base = base;
this.main = main;
this.temp = temp;
this.pressure = pressure;
this.humidity = humidity;
this.temp_min = temp_min;
this.temp_max = temp_max;
this.visibility = visibility;
this.wind = wind;
this.speed = speed;
this.deg = deg;
this.clouds = clouds;
this.all = all;
this.dt = dt;
this.sys = sys;
this.type = type;
this.id = id;
this.message = message;
this.country = country;
this.sunrise = sunrise;
this.sunset = sunset;
}
public String getCoord() {
return coord;
}
public void setCoord(String coord) {
this.coord = coord;
}
public String getLon() {
return lon;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setLon(String lon) {
this.lon = lon;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public List<Integer> getWeather() {
return weather;
}
public void setWeather(List<Integer> weather) {
this.weather = weather;
}
public String getBase() {
return base;
}
public void setBase(String base) {
this.base = base;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getPressure() {
return pressure;
}
public void setPressure(String pressure) {
this.pressure = pressure;
}
public Integer getHumidity() {
return humidity;
}
public void setHumidity(Integer humidity) {
this.humidity = humidity;
}
public String getTemp_min() {
return temp_min;
}
public void setTemp_min(String temp_min) {
this.temp_min = temp_min;
}
public String getTemp_max() {
return temp_max;
}
public void setTemp_max(String temp_max) {
this.temp_max = temp_max;
}
public String getVisibility() {
return visibility;
}
public void setVisibility(String visibility) {
this.visibility = visibility;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
public String getDeg() {
return deg;
}
public void setDeg(String deg) {
this.deg = deg;
}
public String getClouds() {
return clouds;
}
public void setClouds(String clouds) {
this.clouds = clouds;
}
public String getAll() {
return all;
}
public void setAll(String all) {
this.all = all;
}
public String getDt() {
return dt;
}
public void setDt(String dt) {
this.dt = dt;
}
public String getSys() {
return sys;
}
public void setSys(String sys) {
this.sys = sys;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getSunrise() {
return sunrise;
}
public void setSunrise(String sunrise) {
this.sunrise = sunrise;
}
public String getSunset() {
return sunset;
}
public void setSunset(String sunset) {
this.sunset = sunset;
}
}
MyJSONResponse I am trying to get this data in adapter which is a RecyclerView Adapter
package com.example.openweathermap;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class WeatherResponse {
@SerializedName("weather")
private List<Weather> weather;
@SerializedName("id")
private String id;
@SerializedName("main")
private String main;
@SerializedName("description")
private String description;
@SerializedName("temp")
private String temp;
public WeatherResponse(List<Weather> weather) {
this.weather = weather;
}
public WeatherResponse(String weather, String id, String main, String description, String temp) {
this.id = id;
this.main = main;
this.description = description;
this.temp = temp;
}
public List<Weather> getWeather() {
return weather;
}
public void setWeather(List<Weather> weather) {
this.weather = weather;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
}
----------
**MyAdapter
This is the recyclerview adapter**
----------
package com.example.openweathermap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<Weather> weathers;
public Context context;
public MyAdapter(ArrayList<Weather> weathers, Context context) {
this.weathers = weathers;
this.context = context;
}
public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
Weather weather=weathers.get(position);
holder.temp.setText(weather.getTemp());
/*holder.name.setText(weather.getCountry());*/
holder.description.setText(weather.getDescription());
weathers=new ArrayList<>();
}
@Override
public int getItemCount() {
return weathers.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView temp;
public TextView name;
public TextView description;
public ViewHolder(@NonNull View itemView) {
super(itemView);
temp=(TextView)itemView.findViewById(R.id.temp);
name=(TextView)itemView.findViewById(R.id.name);
description=(TextView)itemView.findViewById(R.id.description);
}
}
}
Main Activity
package com.example.openweathermap;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private ArrayList<Weather> weathersList;
// TODO - insert your themoviedb.org API KEY here
private final static String API_KEY = "ae47e0f7eb5fbbce0c9cfbdf1373a1b3";
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
weathersList=new ArrayList<>();
if (API_KEY.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please obtain your API KEY first ", Toast.LENGTH_LONG).show();
return;
}
getWeather();
}
private void getWeather() {
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api=retrofit.create(Api.class);
Call<WeatherResponse> call=api.getWeatherDetails(API_KEY);
call.enqueue(new Callback<WeatherResponse>() {
@Override
public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {
ArrayList<Weather> weathersList = (ArrayList<Weather>) response.body().getWeather();
adapter=new MyAdapter(weathersList,getApplicationContext());
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<WeatherResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
I am getting an error as " Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference RecyclerView Adapter Error" in MyAdapter