如果在Retrofit中“response.isSuccessful()”为真,如何读取响应

时间:2018-01-31 18:21:19

标签: java android json retrofit

来自API的JSON响应:

{
    "type": "loginResponseMessage",
    "statusCode": 200,
    "statusMessage": "Login Successful",
    "resource_id": 3
}

我想阅读JSON响应。当'if(response.isSuccessful())'正在执行时,我得到空指针异常。在执行'else'块的情况下,我得到了正确的响应。以下是代码:

@Override
            public void onResponse(Call<Login> call, Response<Login> response) {
            if (response.isSuccessful()) {
                LoginResponseMessage loginResponseMessage;

                loginResponseMessage = new APIResponse().getResponse2(retrofit,response);

                String toastMessage = "StatusMessage: "+loginResponseMessage.getStatusMessage()
                        +"Status Code: "+loginResponseMessage.getStatusCode()
                        +"ResourceId: "+loginResponseMessage.getResource_id();
                Toast.makeText(LoginActivity.this,toastMessage, Toast.LENGTH_SHORT).show();
            } else {

                switch(response.code()){
                    case 401:
                        LoginResponseMessage loginResponseMessage;

                        loginResponseMessage = new APIResponse().getResponse2(retrofit,response);

                        String toastMessage = "StatusMessage: "+loginResponseMessage.getStatusMessage()
                                +"Status Code: "+loginResponseMessage.getStatusCode()
                                +"ResourceId: "+loginResponseMessage.getResource_id();
                        Toast.makeText(LoginActivity.this,toastMessage, Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }

Class APIResponse:

public class APIResponse {
    private String statusMessage;

    public LoginResponseMessage getResponse2(Retrofit retrofit, Response response){         //This will only be called in LoginActivity
        LoginResponseMessage responseMessage = new LoginResponseMessage();

        Converter<ResponseBody, LoginResponseMessage> converter
                = retrofit.responseBodyConverter(LoginResponseMessage.class, new Annotation[0]);
        try {
            responseMessage = converter.convert(response.errorBody());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responseMessage;
    }
}

相同的逻辑适用于'if'&amp; '其他'阻止。但是在'if(response.isSuccessful())'块

中获取空指针异常
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Reader okhttp3.ResponseBody.charStream()' on a null object reference
                                                                      at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:35)
                                                                      at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
                                                                      at pk.qareeb.qareeb.LoginActivity$5.onResponse(LoginActivity.java:206)
                                                                      at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
                                                                      at android.os.Handler.handleCallback(Handler.java:761)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                      at android.os.Looper.loop(Looper.java:156)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6523)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
t

第二种方法: 我也尝试过以下代码。

@Override
     public void onResponse(Call<Login> call, Response<Login> response) {
         Toast.makeText(LoginActivity.this,response.body().toString(),Toast.LENGTH_LONG).show();
}

通过添加第二个方法代码,而不是烘烤JSON响应,它正在烘烤“com.qareeb.qareeb.modal.Login@119c611”。为什么?

1 个答案:

答案 0 :(得分:0)

第二种方法中,您正在打印instance of Login class。这不是错误或问题。如果您想查看响应,请覆盖登录类中的 toString()方法,或者还可以检查登录类中的属性一个接一个。

修改 - 据我所知,你在这里使用Login(for request)类而不是Response(获取响应)类。因此,使用您的Login类发送请求和LoginResponse类以获取登录响应。

创建一个单独的班级名称 LoginResponse ,并在网络电话中使用它而不是登录

<强> LoginResponse.java

public class LoginResponse
{
   private String statusCode;

   private String resource_id;

   private String type;

    private String statusMessage;
    // getter and setters.
    .
    .
    @Override
    public String toString()
    {
        return "LoginResponse [statusCode = "+statusCode+", resource_id = "+resource_id+", type = "+type+", statusMessage = "+statusMessage+"]";
    }
}

现在就像 -

@Override
 public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
     Toast.makeText(context, LoginResponse.toString(), Toast.LENGTH_LONG).show();
 }

@Override
 public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
     LoginResponse loginResponse = response.body();
     Toast.makeText(context, loginResponse.getStatusMessage (), Toast.LENGTH_LONG).show();
     Toast.makeText(context, loginResponse.getType (), Toast.LENGTH_LONG).show();
 }