从改型调用API时出现未知错误

时间:2019-08-23 08:46:26

标签: android http retrofit

我在翻新电话方面遇到问题。 我正在调用一个简单的忘记密码api。代码中的一切看起来都很好,但是我在response.errorBody()

中遇到了类似okhttp3.ResponseBody$1@somerandomnumber的错误。

这是我的json响应

{
    "error": "False",
    "message": "An email has been sent to your registered email address: . To reset password please check your inbox."
}

这是我的调用api参考  //忘记密码

@FormUrlEncoded
@POST("api/v1//users/password/reset")
Call<NewBasicModel> forgotPassword(
                              @Field("email")String email);

这是我在活动中的代码

LoginApi loginApi = APIClient.getApiClient().create(LoginApi.class);
    Call<NewBasicModel> call = loginApi.forgotPassword(userEmail);

Log.i("url", call.request().url().toString());


call.enqueue(new Callback<NewBasicModel>() {
    @Override
    public void onResponse(@NonNull Call<NewBasicModel> call, @NonNull Response<NewBasicModel> response) {
        if (response.isSuccessful()){

            if ("False".equals(response.body().getError())){
                svProgressHUD.dismiss();
                updateUI(response.body().getMessage());

            }
            else {
                svProgressHUD.dismiss();
                AlertPopup.alertDialogShow(ForgotPassword.this, "Something went wrong, try again", "");
            }

        }

        else {
            svProgressHUD.dismiss();
            AlertPopup.alertDialogShow(ForgotPassword.this, response.errorBody().toString(), "");
        }
    }

    @Override
    public void onFailure(Call<NewBasicModel> call, Throwable t) {

    }
});

这是我的api客户端

    public static Retrofit retrofit=null;
        private static Gson gson = new GsonBuilder().setLenient().create();

        private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(7, TimeUnit.MINUTES)
                .readTimeout(7, TimeUnit.MINUTES)
                .writeTimeout(7, TimeUnit.MINUTES)
                .build();

        public static Retrofit getApiClient()
        {
            if(retrofit==null)
            {
                //scalar is for text and gson for json obect and arrays
                retrofit=new Retrofit.Builder().baseUrl(base_url)
                        .client(okHttpClient)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
            }
            return  retrofit;
        }


Here is my response model

public class NewBasicModel {
    @SerializedName("error")
    @Expose
    private String error;
    @SerializedName("message")
    @Expose
    private String message;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

0 个答案:

没有答案