在onResponse中改装2 NULL响应,即使数据不为NULL



所以,我的问题相当简单。

这是我通过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)

如果我正在获取一些数据,我的响应怎么可能为null?

密码重置.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_接口

@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");
}

在理论上,当我们得到一些响应时,称为onResponse的retrofit2方法,当建立和接收响应的过程不满足时,则称为onFailure。我忽略了这个简单的事实。所以,如果有人仍然来看和阅读,我建议你也检查一下你的response.body()是否成功。快乐编码!

从改装的javadoc for Response中,您可以看到body()成功的响应返回反序列化的响应。不幸的是,你的回复似乎不成功,因为你似乎收到了500分。

errorBody()是您想要使用的。但是,这会返回原始响应,因此您必须自己对其进行反序列化。

有很多方法可以做到这一点。一种可能是使用gson来反序列化主体:

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

附言:仅仅因为你最终上了onResponse,并不意味着你有一个成功的回应。然而,从您的代码中,您似乎已经知道了这一点,并且正在检查http状态200

最新更新