Lending Square
Lending Square

Reputation: 201

Retrofit GET request with a parameter

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.

This is what the API expects.

Upvotes: 0

Views: 2280

Answers (2)

Recep Yesil
Recep Yesil

Reputation: 148

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

Upvotes: 2

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12963

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

Upvotes: 0

Related Questions