代码之家  ›  专栏  ›  技术社区  ›  johnny68

即使数据不为空,onResponse中的reformation2空响应

  •  1
  • johnny68  · 技术社区  · 6 年前

    所以,我的问题很简单。

    这是我通过api调用得到的响应。

    D/OkHttp: {"status":"error","status_code":"500","error_msg":"There was an error trying to send the password reset email."}
    <-- END HTTP (220-byte body)
    

    这是我处理电话的代码

     Call<PasswordReset> forgotPasswordCall = apiInterface.Reset(email);
        forgotPasswordCall.enqueue(new Callback<PasswordReset>() {
            @Override
            public void onResponse(Call<PasswordReset> call, Response<PasswordReset> response) {
                PasswordReset passwordReset = response.body();
                try {
                    if (passwordReset.getStatusCode() != null && passwordReset.getStatusCode().equals("200")) {   //line 133
                        Log.d("Success", "Password Reset Email Sent");
                        new CustomToast().showToast(getContext(), view, "Password Email sent");
                        new LoginActivity().replaceFragment("left");
                    } else {
                        String test = passwordReset.getStatusCode();
                        Log.d("Failure", test);
                        hideDialog();
                    }
                }
                catch (Exception e){
                    e.printStackTrace();
                    hideDialog();
                }
            }
    
            @Override
            public void onFailure(Call<PasswordReset> call, Throwable t) {
                Log.d("Failure", "Password Reset Email Not Sent");
                new CustomToast().showToast(getContext(), view, "Email Not Sent");
                new LoginActivity().replaceFragment("left");
                hideDialog();
            }
        });
    

    这是我抓到的例外

    W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String in.siddhant.anetpays_customer.POJO.PasswordReset.getStatusCode()' on a null object reference
        at in.siddhant.anetpays_customer.Login.Fragments.ForgotPassword$1.onResponse(ForgotPassword.java:133)
    

    如果我得到一些数据,我的响应怎么可能是空的?

    passwordreset.class类

        public class PasswordReset {
    
    
        @SerializedName("data")
        private String data;
        @SerializedName("status_code")
        private String statusCode;
        @SerializedName("status")
        private String status;
    
        public String getData() {
            return data;
        }
    
        public void setData(String data) {
            this.data = data;
        }
    
        public String getStatusCode() {
            return statusCode;
        }
    
        public void setStatusCode(String statusCode) {
            this.statusCode = statusCode;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }
    

    改造客户

    public static Retrofit getClient() {
    
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
    
    
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://xsdf/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();
    
    
    
            return retrofit;
    

    API U接口

    @FormUrlEncoded
        @POST("/ddd/pwdresetrequest")
        Call<PasswordReset>Reset(@Field("email")String email);
    

    p.s-api本身有一些问题,并且总是返回 "status":"error" 但这不应该影响应用程序,对吧?我也很乐意分享更多的代码。 提前谢谢。

    解决方案 我正在张贴的解决方案建议,根据接受的答案,希望它有助于谁来寻找。

    forgotPasswordCall.enqueue(new Callback<PasswordReset>() {
                @Override
                public void onResponse(Call<PasswordReset> call, Response<PasswordReset> response) {
                         if (!response.isSuccessful()){
                            Gson gson = new Gson();
                            PasswordReset passwordReset1 = gson.fromJson(response.errorBody().charStream(), PasswordReset.class);
                            if (passwordReset1.getStatusCode().equals("500")){
                                new CustomToast().showToast(getContext(), view, "Password Email not sent");
                                hideDialog();
                            }
                            else {
                                Thread.dumpStack();
                                hideDialog();
                            }
    
                        }
                         else if (response.isSuccessful()) {
                             Log.d("Success", "Password Reset Email Sent");
                             new CustomToast().showToast(getContext(), view, "Password Email sent");
                             new LoginActivity().replaceFragment("left");
                         }
    

    从理论上讲,改造2的onresponse方法是在得到响应时调用的,onfailure方法是在不满足建立和接收响应的过程中调用的。我忽略了这个简单的事实。 所以,如果有人来看看书,我建议你也检查一下 response.body() 如果成功与否。 快乐编码!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Fred    6 年前

    从改造的javadoc Response 你可以看看 body() 返回来自 成功的 回应。不幸的是,似乎你有一个不成功的回应,鉴于你似乎收到500。

    errorBody() 是你想用的。但是,这将返回原始响应,因此您必须自己反序列化它。

    有很多方法可以做到这一点可能有人在用 gson 要反序列化主体,请执行以下操作:

    new Gson().fromJson(response.errorBody().body().string(), YourModel.class);
    

    PS:就因为你最终 onResponse 这并不意味着你有一个成功的回应。但是,从您的代码看来,您已经知道了这一点,并且正在检查http状态 200 .