【问题标题】:Not able to get Response using Retrofit无法使用改造获得响应
【发布时间】:2017-07-03 05:59:08
【问题描述】:

我试图从下面的 json 中获取 temp_max 的值,但我得到了 null。此外,我还能够获取 cod、message 和 cnt 的值。

我使用 ""response.body().getTempMax()"" 来获取值。如果这不是正确的方法,请告诉我。

模型类

public class Main {


    @SerializedName("temp_min")
    @Expose
    private Double tempMin;
    @SerializedName("temp_max")
    @Expose
    private Double tempMax;

    public Double getTempMin() {
        return tempMin;
    }

    public void setTempMin(Double tempMin) {
        this.tempMin = tempMin;
    }

    public Double getTempMax() {
        return tempMax;
    }

    public void setTempMax(Double tempMax) {
        this.tempMax = tempMax;
    }
    }
}

主活动

ApiInterface apiInterface= ApiClient.getClient().create(ApiInterface.class);

    Call<Main> call = apiInterface.getWeather();
    call.enqueue(new Callback<Main>()
    {
        @Override
        public void onResponse(Call<Main> call, Response<Main> response)
        {
            response.body();
            Toast.makeText(MainActivity.this, "The cnt is " + response.body().getTempMax(), Toast.LENGTH_LONG).show();
        }

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

【问题讨论】:

  • 你能把解析类的代码也粘贴一下吗

标签: java android retrofit2


【解决方案1】:

您需要更新您为解析提供的模型类

说

    class JSONResponse{
    int cod;
    String message;
    int cnt;
    ArrayList<Data> list;
    }


   class Data{
    long dt;
    Main main;

   }

   class Main{
   //All properties related to main class
   }

这就是您需要的结构。是不是很简单。。现在,当您需要访问 Main 类的成员时,您需要从 JsonResponse 类一直遍历到 Data 类并访问 Main 对象并访问属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2021-08-19
    • 2016-11-05
    相关资源
    最近更新 更多