Retrofit GET request with a parameter

久未见 提交于 2019-12-24 21:12:30

问题


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();
        }
    });
}

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


回答1:


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).

And also try this format request

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



回答2:


Make sure that you are calling correct method, you seem to call requestPasswordResetToken but you are showing resendVerification inside your interface.



来源:https://stackoverflow.com/questions/48745066/retrofit-get-request-with-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!