使用参数改造 GET 请求 [英] Retrofit GET request with a parameter

查看:58
本文介绍了使用参数改造 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Retrofit GET 请求调用 api.此 GET 请求需要一个参数.当我使用 POSTMAN 进行测试时,API 工作得很好,但是当我尝试使用下面的 API 调用时,它返回

I make a call to an api using Retrofit GET request. This GET request requires a parameter. The API works perfectly when i use POSTMAN for testing but when i try to use the API call below it returns

未将对象引用设置为对象的实例.

Object reference not set to an instance of an object.

@GET("/api/account/*******")
Call<ResetPassword> requestPasswordResetToken(@Query("phoneNumber") String phoneNumber);

以及我如何在活动中提出该请求的代码.

And the code how i make that request in my activity.

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 期望的屏幕截图.字段名称正确.

the screenshot of the what the API expects. The field names are correct.

推荐答案

你的代码看起来不错.您是否检查了您正在处理的 api 的结果格式.并且 ResetPassword 类属性(变量名称和类型)必须与 api 的响应相同.(注意大写或小写字母).

Your codes looks fine. Do you check the result format of the api you are working on. And also ResetPassword class properties (variable names and types) must be same with response of the api. (be careful about upper case or lower case letters).

也试试这个格式请求

  @GET("methodName/{PARAMETER}")
Call<Object> getData(
        @Path("telephoneNumber") String telephoneNumber
);

这篇关于使用参数改造 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆