在Android中使用Retrofit 2.0.2时,Response.body()返回一个具有null属性的对象

时间:2016-07-22 14:34:01

标签: android api null gson retrofit

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

我该怎么办?

2 个答案:

答案 0 :(得分:4)

您的问题是您没有正确解析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

答案 1 :(得分:0)

您必须先将身体投射到缩小对象,如下所示:

if (response.isSuccessful()) {

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

     }