【问题标题】:Response.body() returns an object with null properties while using Retrofit 2.0.2 in AndroidResponse.body() 在 Android 中使用 Retrofit 2.0.2 时返回一个具有空属性的对象
【发布时间】:2016-11-26 12:19:57
【问题描述】:

response.body() 不会返回 API 的内容,但会返回:

com.example.senolb.project.Downsized@e4bbc81

这是我的请求界面:

public interface ApiInterface {
@GET("search")
Call<Downsized> getDownsized(@Query("api_key") String key,
                             @Query("fmt") String format,
                             @Query("q") String type,
                             @Query("limit") String limit);
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.giphy.com/v1/gifs/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
}

这是我的缩小班:

@Generated("org.jsonschema2pojo")
public class Downsized {

@SerializedName("url")
@Expose
private String url;
@SerializedName("width")
@Expose
private String width;
@SerializedName("height")
@Expose
private String height;
@SerializedName("size")
@Expose
private String size;
// getter and setter methods below

这是我在主页上的请求功能,当我按下按钮时触发:

public void request(View view) throws IOException {

ApiInterface service = ApiInterface.retrofit.create(ApiInterface.class);
Call<Downsized> myDownsized = service.getDownsized("dc6zaTOxFJmzC","json","funny","1");

 myDownsized.enqueue(new Callback<Downsized>() {
     @Override
     public void onResponse(Call<Downsized> call, Response<Downsized> response) {
         if (response.isSuccessful()) {

             TextView text2 = (TextView) findViewById(R.id.first_text);
             Downsized dw = response.body();
             text2.setText(dw.getHeight());

         } else {
             //unsuccessful response
         }
     }
     @Override
     public void onFailure(Call<Downsized> call, Throwable t) {
         //failed response
     }
 });

我该怎么办?

【问题讨论】:

  • 您能否确认您的 api 确实有效,并且它使用浏览器 HTTP 客户端扩展返回实际数据?

标签: android api null gson retrofit


【解决方案1】:

您的问题是您没有正确解析从api 返回的 json。首先再创建三个类来映射您的 json 数据:

头等舱:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;

public class JsonResponse {

@SerializedName("data")
@Expose
private List<Data> dataList = new ArrayList<>();

public List<Data> getDataList() {
    return dataList;
}
}

二等:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {
@SerializedName("images")
@Expose
private Image images;

public Image getImages() {
    return images;
}
}

三等:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Image {

@SerializedName("downsized")
@Expose
private Downsized downsized;

public Downsized getDownsized() {
    return downsized;
}
}

之后更改您的界面以使用JsonResponse 类而不是Downsized

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ApiInterface {
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.giphy.com/v1/gifs/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

@GET("search")
Call<JsonResponse> getDownsized(@Query("api_key") String key,
                                @Query("fmt") String format,
                                @Query("q") String type,
                                @Query("limit") String limit);

}

最后更改 request() 方法中的代码:

ApiInterface service = ApiInterface.retrofit.create(ApiInterface.class);
    Call<JsonResponse> myDownsized = service.getDownsized("dc6zaTOxFJmzC", "json", "funny", "1");

    myDownsized.enqueue(new Callback<JsonResponse>() {
        @Override
        public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
            if (response.isSuccessful()) {
                for (Data data : response.body().getDataList()) {
                    System.out.println(data.getImages().getDownsized().getUrl());
                    //TextView text2 = (TextView) findViewById(R.id.first_text);
                    //Downsized dw = response.body();
                    //text2.setText(dw.getHeight());
                }

            } else {
                //unsuccessful response
            }
        }

        @Override
        public void onFailure(Call<JsonResponse> call, Throwable t) {
            //failed response
        }
    });

运行后结果为:simpsons

【讨论】:

  • 谢谢,这解决了我的问题。这是否意味着无论我得到什么 Json,我都需要将 JsonResponse 类与 ArrayList 一起使用?
  • 不,如果您的 json 响应包含一个数组(包含 [ ] 括号),您可以在 java 代码中使用 ArrayList。实际上,您可以在界面中直接使用Call&lt;List&lt;Data&gt;&gt;,而不是创建JsonResponse 类和其余的改造代码。您总是开始分析您的 json 响应,然后通过 java 提取数据。
【解决方案2】:

您必须先将 body 转换为 Downsized 对象,如下所示:

if (response.isSuccessful()) {

         Downsized downsized = response.body();
         String url=downsized.getUrl();

     }

【讨论】:

  • 是的,我做到了,但它给出了 null。
猜你喜欢
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
相关资源
最近更新 更多