Reputation: 2718
When I add List jsonobject from my ResponseBody pojo class,the textview doesnt display any text as the response is empty. But when I remove the List jsonobject from my pojo , the textview correctly displays the required city and country name.
API
http://api.openweathermap.org/data/2.5/forecast?id=6324621&APPID=3e29a05ea5759f3ca2ffe14924df07e8
JSON
{
"cod": "200",
"message": 0,
"cnt": 40,
"list": [
{
"dt": 1574694000,
"main": {
"temp": 300.83,
"temp_min": 300.24,
"temp_max": 300.83,
"pressure": 1013,
"sea_level": 1013,
"grnd_level": 1009,
"humidity": 66,
"temp_kf": 0.59
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 1.61,
"deg": 290
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-11-25 15:00:00"
},
{},
{},{},
],
"city": {
"id": 6324621,
"name": "Powai",
"coord": {
"lat": 19.1164,
"lon": 72.9047
},
"country": "IN",
"timezone": 19800,
"sunrise": 1574644897,
"sunset": 1574684949
}
}
ResponseBody.java:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ResponseBody {
@SerializedName("cod")
@Expose
private String cod;
@SerializedName("message")
@Expose
private long message;
@SerializedName("cnt")
@Expose
private long cnt;
@SerializedName("list")
@Expose
private java.util.List<com.example.weatherapp.List> list = null;
@SerializedName("city")
@Expose
private City city;
public String getCod() {
return cod;
}
public void setCod(String cod) {
this.cod = cod;
}
public long getMessage() {
return message;
}
public void setMessage(long message) {
this.message = message;
}
public long getCnt() {
return cnt;
}
public void setCnt(long cnt) {
this.cnt = cnt;
}
public java.util.List<com.example.weatherapp.List> getList() {
return list;
}
public void setList(java.util.List<com.example.weatherapp.List> list) {
this.list = list;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
City.java:
package com.example.weatherapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class City {
@SerializedName("id")
@Expose
private long id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("country")
@Expose
private String country;
@SerializedName("timezone")
@Expose
private long timezone;
@SerializedName("sunrise")
@Expose
private long sunrise;
@SerializedName("sunset")
@Expose
private long sunset;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public long getTimezone() {
return timezone;
}
public void setTimezone(long timezone) {
this.timezone = timezone;
}
public long getSunrise() {
return sunrise;
}
public void setSunrise(long sunrise) {
this.sunrise = sunrise;
}
public long getSunset() {
return sunset;
}
public void setSunset(long sunset) {
this.sunset = sunset;
}
}
List.java:
package com.example.weatherapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class List {
@SerializedName("dt")
@Expose
private long dt;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("dt_txt")
@Expose
private String dtTxt;
public long getDt() {
return dt;
}
public void setDt(long dt) {
this.dt = dt;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
public String getDtTxt() {
return dtTxt;
}
public void setDtTxt(String dtTxt) {
this.dtTxt = dtTxt;
}
}
ApiInterface.java:
package com.example.weatherapp.network;
import com.example.weatherapp.City;
import com.example.weatherapp.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface ApiInterface {
@GET("/data/2.5/forecast")
Call<ResponseBody> RESPONSE_BODY_CALL(@Query("id") long id, @Query("APPID") String api_key);
}
ApiClient.java:
package com.example.weatherapp.network;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
static Retrofit retrofit = null;
static OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
public static Retrofit getRetrofit()
{
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); //logs set
okHttpClient.addInterceptor(httpLoggingInterceptor); // logs added
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://api.openweathermap.org/")
.client(okHttpClient.build())
.build();
return retrofit;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="40dp"
android:layout_marginTop="20dp"
android:padding="20dp"
android:background="#bebeee"
android:text="City, Country"/>
<TextView
android:id="@+id/humidity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textStyle="bold"
android:layout_marginVertical="20dp"
android:layout_below="@+id/city"
android:textSize="40dp"
android:background="#ddbdbb"
android:padding="20dp"
android:text="Humidity"/>
</RelativeLayout>
I cant figure out the reason by the textview is showing empty when I do not remove the List pojo object from ResponseBody.class
Upvotes: 0
Views: 706
Reputation: 1594
From your comments its clear that the issue is with the ResponseBody class.
Change the data type of variable temp_kf from the class Main.Java to double.
The current data type for the variable temp_kf is long, so when Gson tries to convert the value containing fractional parts it throws exception.
Upvotes: 2
Reputation: 146
1.On you ApiInterface Remove extra "/" from @GET query
Make it "data/2.5/forecast" as a "/" already added to your base url
@GET("data/2.5/forecast")
Call<ResponseBody> RESPONSE_BODY_CALL(@Query("id") long id, @Query("APPID") String api_key);
2.Remove null asignment from list.
Example : You initialized list as null
@SerializedName("list")
private List<ListItem> list;
ListItem :
public class ListItem{
@SerializedName("dt")
private int dt;
@SerializedName("dt_txt")
private String dtTxt;
@SerializedName("weather")
private List<WeatherItem> weather;
@SerializedName("main")
private Main main;
@SerializedName("clouds")
private Clouds clouds;
@SerializedName("sys")
private Sys sys;
@SerializedName("wind")
private Wind wind;
public void setDt(int dt){
this.dt = dt;
}
public int getDt(){
return dt;
}
public void setDtTxt(String dtTxt){
this.dtTxt = dtTxt;
}
public String getDtTxt(){
return dtTxt;
}
public void setWeather(List<WeatherItem> weather){
this.weather = weather;
}
public List<WeatherItem> getWeather(){
return weather;
}
public void setMain(Main main){
this.main = main;
}
public Main getMain(){
return main;
}
public void setClouds(Clouds clouds){
this.clouds = clouds;
}
public Clouds getClouds(){
return clouds;
}
public void setSys(Sys sys){
this.sys = sys;
}
public Sys getSys(){
return sys;
}
public void setWind(Wind wind){
this.wind = wind;
}
public Wind getWind(){
return wind;
}
@Override
public String toString(){
return
"ListItem{" +
"dt = '" + dt + '\'' +
",dt_txt = '" + dtTxt + '\'' +
",weather = '" + weather + '\'' +
",main = '" + main + '\'' +
",clouds = '" + clouds + '\'' +
",sys = '" + sys + '\'' +
",wind = '" + wind + '\'' +
"}";
}
}
Upvotes: 0
Reputation: 462
Please use ArrayList
in place of java.util.List
@SerializedName("list")
@Expose
private ArrayList<CityList> list = null;
or change com.example.weatherapp.List
name such as CityList
Upvotes: 0