Retrofit返回null响应体

时间:2016-02-07 12:44:12

标签: android retrofit retrofit2 gson

我正在尝试使用JSON的Android教程。我想从服务器下载一些JSON objectAnimal class的模型为null。当我运行此代码时,我得到OnActivityCreated()响应正文,错误代码为406.

Fragment类的Retrofit retrofit=new Retrofit.Builder().baseUrl("baseurl").addConverterFactory(GsonConverterFactory.create()).build(); AnimalApiInterface animalApiInterface=retrofit.create(AnimalApiInterface.class); Call<List<Animal>> call=animalApiInterface.getStream(); call.enqueue(new Callback<List<Animal>>() { @Override public void onResponse(Call<List<Animal>> call, Response<List<Animal>> response) { if (response.body() == null){ Log.e("Error",response.code()+""); return; } for (Animal animal : response.body()) { Log.e("Zoo", animal.getName()); } } @Override public void onFailure(Call<List<Animal>> call, Throwable t) { Log.e("Zoo", "Retrofit error " + t.getMessage()); } }); 方法内部:

public class Animal implements Parcelable {

    private String name;
    private String species;
    private String description;
    private String thumbnail;
    private String image;

    public Animal(){}
    public Animal(String description, String image, String name, String species, String thumbnail) {
        this.description = description;
        this.image = image;
        this.name = name;
        this.species = species;
        this.thumbnail = thumbnail;
    }
    public Animal(Parcel source){
        name=source.readString();
        species=source.readString();
        description=source.readString();
        thumbnail=source.readString();
        image=source.readString();
    }
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    //other getters and setters

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        applyDefaultValues();
        dest.writeString(name);
        dest.writeString(species);
        dest.writeString(description);
        dest.writeString(thumbnail);
        dest.writeString(image);
    }

    private void applyDefaultValues() {
        if(name==null)name="";
        if(species==null)species="";
        if(description==null)description="";
        if(thumbnail==null)thumbnail="";
        if(image==null)image="";

    }

    public static Creator<Animal> CREATOR = new Creator<Animal>() {
        @Override
        public Animal createFromParcel(Parcel source) {
            return new Animal(source);
        }

        @Override
        public Animal[] newArray(int size) {
            return new Animal[size];
        }
    };
}

模特课:

public interface AnimalApiInterface {
    @GET("/exhibits.json")
    Call<List<Animal>> getStream() ;
}

界面

empty String

我已经包含了所有必需的依赖项。错误正文也是{{1}}。何我可以解决这个问题吗?

0 个答案:

没有答案