Android Retrofit 2发布请求响应,其响应类名称为

时间:2017-11-10 14:24:18

标签: java android retrofit2

在我的应用程序中,我使用正文中的特殊代码向服务器发出请求。然后我应该在回复中获得一些信息。但是,我总是得到响应类的名称。
我的要求代码:

@POST("/accounts/login/vk-oauth2/")
Call<RegistrationProcessCodeResponse> postCode(@Body CodePostRequest code);

我的ResponseClass:

public class RegistrationProcessCodeResponse {
    private String message;
    private String partial_token;
    private String phase;

    public String getMessage() {
        return message;
    }

    public String getPartial_token() {
        return partial_token;
    }

    public String getPhase() {
        return phase;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setPartial_token(String partial_token) {
        this.partial_token = partial_token;
    }

    public void setPhase(String phase) {
        this.phase = phase;
    }
}

我的要求代码:

HseAlumniApi hseAlumniApi = HseAlumniApi.retrofit.create(HseAlumniApi.class);
Call<RegistrationProcessCodeResponse> postComment = hseAlumniApi.postCode(codePostRequest);
postComment.enqueue(new Callback<RegistrationProcessCodeResponse>() {
    @Override
    public void onResponse(Call<RegistrationProcessCodeResponse> call, Response<RegistrationProcessCodeResponse> response) {
        Log.d("myLogs", "String.valueOf(response.code())\n" + String.valueOf(response.code()));
        Log.d("myLogs", "response.body().toString()\n" + response.body().toString());
        if (response.isSuccessful()) {
           Log.d("myLogs", "Request succeeded");
        }
    }

    @Override
    public void onFailure(Call<RegistrationProcessCodeResponse> call, Throwable t) {
        Log.d("myLogs", "Request failed");
    }
});

我的日志:

D/myLogs: String.valueOf(response.code())
          200
D/myLogs: response.body().toString()
        com.example.vitaly.hsealumni.RegistrationProcessCodeResponse@498e7e7
D/myLogs: Request succeeded

回应Json:

{
"message": "email needed",
"partial_token": "231445d4fc5a4ed99dccb681942d5d7e",
"phase": 1
}

我真的不知道该怎么做,请帮忙

1 个答案:

答案 0 :(得分:3)

public class RegistrationProcessCodeResponse {
    private String message;
    private String partial_token;
    private String phase;

    public RegistrationProcessCodeResponse() {
        message = "";
        partial_token = "";
        phase = "";
    }

   // getters and setters

    @Override
    public String toString() {
        return "RegistrationProcessCodeResponse{" +
            "message='" + message + '\'' +
            ", partial_token='" + partial_token + '\'' +
            ", phase='" + phase + '\'' +
            '}';
    }
}