所以,我的问题很简单。
这是我通过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()
如果成功与否。
快乐编码!