Response.body()在Android中使用Retrofit 2.0.2时返回一个具有null属性的对象 [英] Response.body() returns an object with null properties while using Retrofit 2.0.2 in Android

查看:1759
本文介绍了Response.body()在Android中使用Retrofit 2.0.2时返回一个具有null属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


com.example.senolb.project.Downsized@e4bbc81


这是我的请求接口:

  public interface ApiInterface {
@GET(search)
呼叫<缩小> getDownsized(@Query(api_key)字符串键,
@Query(fmt)字符串格式,
@Query(q)字符串类型,
@Query(limit )字符串限制);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(http://api.giphy.com/v1/gifs/)
.addConverterFactory(GsonConverterFactory.create())
.build();
}

这是我的缩小班级:

  @Generated(org.jsonschema2pojo)
公共类缩编{

@SerializedName(url)
@Expose
private String url;
@SerializedName(width)
@Expose
private String width;
@SerializedName(height)
@Expose
私人字符串高度;
@SerializedName(size)
@Expose
私人字符串大小;
// getter和setter方法低于

这是我在主页面的请求函数这会在我按下按钮时触发:

  public void request(View view)throws IOException {

ApiInterface服务= ApiInterface.retrofit.create(ApiInterface.class);
呼叫<缩小> myDownsized = service.getDownsized(dc6zaTOxFJmzC,json,funny,1);
$ b myDownsized.enqueue(新回调<缩小>(){
@Override
public void onResponse(Call<缩小>调用,Response<缩小>响应){
if(response.isSuccessful()){

TextView text2 =(TextView)findViewById(R.id.first_text);
缩小尺寸dw = response.body();
text2.setText(dw.getHeight());

} else {
//不成功的响应
}
}
@覆盖
public void onFailure(Call< Downsized> call,Throwable t){
// failed response
}
});

我该怎么办?

解决方案

你的问题是你没有正确解析返回的json api 。首先创建三个类来映射你的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<>();

公共列表< Data> getDataList(){
返回dataList;


$ / code $ / pre

第二类:

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

公共类数据{
@SerializedName(图片)
@Expose
私人图片图片;

public Image getImages(){
return images;
}
}

第三课:

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

public class Image {

@SerializedName(缩小尺寸)
@Expose
私人缩小尺寸;

公开缩编getDownsized(){
返回缩小;






$ p

之后,改变你的界面使用 JsonResponse class not 缩小

  import retrofit2.Call; 
进口retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
导入retrofit2.http.GET;
导入retrofit2.http.Query;

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

@GET(搜索)
呼叫< JsonResponse> getDownsized(@Query(api_key)字符串键,
@Query(fmt)字符串格式,
@Query(q)字符串类型,
@Query(limit )字符串限制);

}

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

  ApiInterface服务= ApiInterface.retrofit .create(ApiInterface.class); 
呼叫< JsonResponse> myDownsized = service.getDownsized(dc6zaTOxFJmzC,json,funny,1);
$ b $ myDownsized.enqueue(new Callback< JsonResponse>(){
@Override
public void onResponse(Call< JsonResponse> call,Response< JsonResponse> response){$ b $如果(response.isSuccessful()){
for(Data data:response.body()。getDataList()){
System.out.println(data.getImages()。getDownsized()。 getUrl());
// TextView text2 =(TextView)findViewById(R.id.first_text);
//缩小尺寸dw = response.body();
//text2.setText (dw.getHeight());
}

} else {
//不成功的响应
}
}

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

运行结果为:辛普森一家


response.body() does not return content of the API but it returns this:

com.example.senolb.project.Downsized@e4bbc81

This is my interface for the request:

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

This is my Downsized class:

@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

And this is my request function at the main page which triggers when I push a button:

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

What should I do?

解决方案

Your problem is that you're not parsing correctly the returned json from api. First of all create three more classes to map your json data:

First class:

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

second class:

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

third class:

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

After that change your interface to use JsonResponse class not 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);

}

and finally change the code in your request() method:

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

After running the result is: simpsons

这篇关于Response.body()在Android中使用Retrofit 2.0.2时返回一个具有null属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆