Retrofit 2 - 响应体返回null(响应代码为200)

时间:2017-12-25 12:24:39

标签: java json gson retrofit

我有一个像这样的json文件:

{  
   "response":{  
      "ApplicationList":[  
         {  
            "Id":1,
            "Name":"SomeApp"
         }
      ],
      "Token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIyMyIsIm5iZiI6MTUxNDIwMDkyMiwiZXhwIjoxNTQ1NzM2OTIyLCJpYXQiOjE1MTQyMDA5MjJ9.9nyaBfbxPlg8T8WkbBBi34II9NZMtyRpeEJ1s1XCJlo"
   },
   "errorMessageId":0,
   "errorMessage":null
}

我正在使用Retrofit库,我已经使用拦截器级别BODY检查了响应,这表明响应很好(如上所述)。

我使用http://www.jsonschema2pojo.org/创建了模型,但出于某种原因,解析为java模型并不是应该的。我在模型中获取属性的所有空值。

以下是我的模型类:

public class SignInUsersResponse implements Parcelable{

public Response response;
public Integer errorMessageId;
public String errorMessage;

@Override
public String toString() {
    return "SignInUsersResponse{" +
            "response=" + response +
            ", errorMessageId=" + errorMessageId +
            ", errorMessage='" + errorMessage + '\'' +
            '}';
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(this.response, flags);
    dest.writeValue(this.errorMessageId);
    dest.writeString(this.errorMessage);
}

public SignInUsersResponse() {
}

protected SignInUsersResponse(Parcel in) {
    this.response = in.readParcelable(Response.class.getClassLoader());
    this.errorMessageId = (Integer) in.readValue(Integer.class.getClassLoader());
    this.errorMessage = in.readString();
}

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

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


public class Response implements Parcelable{

public List<ApplicationList> applicationList = null;
public String token;


@Override
public String toString() {
    return "Response{" +
            "applicationList=" + applicationList +
            ", token='" + token + '\'' +
            '}';
}


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedList(this.applicationList);
    dest.writeString(this.token);
}

public Response() {
}

protected Response(Parcel in) {
    this.applicationList = in.createTypedArrayList(ApplicationList.CREATOR);
    this.token = in.readString();
}

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

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


public class ApplicationList implements Parcelable{

public Integer id;
public String name;



@Override
public String toString() {
    return "ApplicationList{" +
            "id=" + id +
            ", name='" + name + '\'' +
            '}';
}


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(this.id);
    dest.writeString(this.name);
}

public ApplicationList() {
}

protected ApplicationList(Parcel in) {
    this.id = (Integer) in.readValue(Integer.class.getClassLoader());
    this.name = in.readString();
}

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

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

这三个都在不同的类文件中。

这就是我打电话给服务器的方式:

 RHDRService rhdrService = ApiUtilsUser.rhdrService(Constants.RHUSER_BASE_URL);
    Call<SignInUsersResponse> call = rhdrService.signInUser(signInUserPost);

    call.enqueue(new Callback<SignInUsersResponse>() {
        @Override
        public void onResponse(Call<SignInUsersResponse> call, Response<SignInUsersResponse> response) {
            Log.d("test", response.body().toString());
        }

        @Override
        public void onFailure(Call<SignInUsersResponse> call, Throwable t) {

        }
    });

这是记录的内容:

  

D / test:SignInUsersResponse {response = Response {applicationList = null,token =&#39; null&#39;},errorMessageId = 0,errorMessage =&#39; null&#39;}

这是声明用于Retrofit调用的其他文件的方式:

public class ApiUtilsUser {

    public static RHDRService rhdrService(String base_url) {
        return RetroFitClientUser.getClient(base_url).create(RHDRService.class);
    }
}


public class RetroFitClientUser {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        // add your other interceptors …
        // add logging as last interceptor
        httpClient.addInterceptor(logging);  // <-- this is the important line!

        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())             // Ako nećemo logovanje onda ova linija ne treba
                .build();

        return retrofit;
    }
}




public interface RHDRService {

    @Headers( "Content-Type: application/json" )
    @POST("api/user/signin")
    Call<SignInUsersResponse> signInUser (@Body SignInUserPost signInUserPost);
}

1 个答案:

答案 0 :(得分:0)

您的JSON和POJO不同。

默认情况下,Gson使用FieldNamingPolicy.IDENTITY as field naming policy。此字段命名策略用于将JSON映射到您的POJO,并且区分大小写。

您可以使用其他字段命名策略,也可以使用@SerializedName指示应使用给定名称对字段进行反序列化。

class MyPojo {
     @SerializedName("ApplicationList")
     List<Application> applicationList;
}