使用翻新解析Json给出null

Toni0123:

我正在尝试使用Retrofit2从Json获取数据,但我得到的是null。我不确定应该在接口GET方法中输入什么?

Json Datum回应:

public class DatumResponse {

@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("total")
@Expose
private Integer total;

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

public Integer getTotal() {
    return total;
}

public void setTotal(Integer total) {
    this.total = total;
}

改装类

public class JsonApi {

public static final String BASE_URL = "https://api.deezer.com";
private static Retrofit retrofit = null;


public static Retrofit getClient() {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

和我的界面:

public interface IDatum {

@GET("chart")
Call<DatumResponse> getDatum();

非常感谢。

阿里·艾哈迈德(Ali Ahmed):

将完整的json添加到JsonScheme2pojo,您将获得这些类

图片

在这里,TrackDetailsModel是主模型类。使用它可以获取其他数据。

现在改变

@GET("chart")
Call<DatumResponse> getDatum();

@GET("chart")
Call<TrackDetailsModel> getDatum();

使用此获取数据

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

Call<TrackDetailsModel> call = apiInterface.getDatum();

    call.enqueue(new Callback<TrackDetailsModel>() {
    @Override
    public void onResponse(Call<TrackDetailsModel> call, Response<TrackDetailsModel> response) {
            if (response.isSuccessful()) {
                 TrackDetailsModel model = response.body();
                 String trackTitle = model.getTracks().getData().getTitle();

                    }
                }

    @Override
    public void onFailure (Call <TrackDetailsModel> call, Throwable t){
          call.cancel();
          Toast.makeText(Main.this,"Error: " + t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
                }
            });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章