Response.body() returns an object with null proper

2019-04-13 10:23发布

问题:

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?

回答1:

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



回答2:

You must cast the body to Downsized object before, like this:

if (response.isSuccessful()) {

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

     }