【发布时间】:2018-02-12 11:10:54
【问题描述】:
我使用 Retrofit GET 请求调用 api。此 GET 请求需要一个参数。当我使用 POSTMAN 进行测试时,API 可以完美运行,但是当我尝试使用下面的 API 调用时,它会返回
对象引用未设置为对象的实例。
@GET("/api/account/*******")
Call<ResetPassword> requestPasswordResetToken(@Query("phoneNumber") String phoneNumber);
以及我如何在我的活动中提出该请求的代码。
public void requestPasswordResetToken(String phoneNumber) {
Retrofit retrofit = RetrofitClient.getClient("");
APIService mAPIService = retrofit.create(APIService.class);
final ProgressDialog loading = ProgressDialog.show(this, "Please Wait", "Loading your information...", false, false);
loading.setCancelable(true);
loading.show();
mAPIService.requestPasswordResetToken(phoneNumber).enqueue(new Callback<ResetPassword>() {
@Override
public void onResponse(Response<ResetPassword> response, Retrofit retrofit) {
if(response.isSuccess()) {
String loginSuccess = response.body().getSuccess();
String message = response.body().getMessage();
if (loginSuccess.equals("true")) {
loading.dismiss();
showSnackMessage(message);
}else {
Log.e("loginError", message);
Toast.makeText(RequestPasswordResetActivity.this, message, Toast.LENGTH_LONG).show();
loading.dismiss();
}
}
}
@Override
public void onFailure(Throwable throwable) {
Log.e("ResetPasswordError", throwable.getMessage());
Toast.makeText(RequestPasswordResetActivity.this, "Unable to Login, Please Try Again", Toast.LENGTH_LONG).show();
loading.dismiss();
}
});
}
API 预期的屏幕截图。字段名称正确。
【问题讨论】:
-
使用
com.squareup.okhttp3:logging-interceptor记录您的 API 调用并检查 -
@SivakumarS 它已经被记录,这是我的回应 D/OkHttp: { D/OkHttp: "total": 0, D/OkHttp: "data": null, D/OkHttp: "message ": "对象引用未设置为对象的实例。", D/OkHttp: "success": false D/OkHttp: }
标签: java android rest retrofit2