繁体   English   中英

android中的Retrofit响应问题

[英]Issue with Retrofit response in android

当我从我的 ResponseBody pojo 类中添加 List jsonobject 时,由于响应为空,textview 不显示任何文本。 但是当我从我的 pojo 中删除 List jsonobject 时,textview 会正确显示所需的城市和国家/地区名称。

应用程序接口

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
    }
}

响应体.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;
        }

    }

城市.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;
        }

    }

列表.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;
        }

    }

接口接口.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;
        }
    }

活动_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>

当我不从 ResponseBody.class 中删除 List pojo 对象时,我无法弄清楚 textview 显示为空的原因

从您的评论中可以清楚地看出问题出在 ResponseBody 类上。

将类 Main.Java 中变量 temp_kf 的数据类型更改为 double。

变量 temp_kf 的当前数据类型很长,因此当 Gson 尝试转换包含小数部分的值时,它会引发异常。

请使用ArrayList代替java.util.List

 @SerializedName("list")
 @Expose
 private ArrayList<CityList> list = null;

或更改com.example.weatherapp.List名称,例如CityList

1.在你的ApiInterface 上@GET查询中删除多余的“/”

使其“data/2.5/forecast”作为“/”已添加到您的基本网址

@GET("data/2.5/forecast")
Call<ResponseBody> RESPONSE_BODY_CALL(@Query("id") long id, @Query("APPID") String api_key);

2.从列表中删除空分配。

示例:您将列表初始化为 null

@SerializedName("list")
private List<ListItem> list;

项目清单 :

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 + '\'' + 
            "}";
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM