Search code examples
androidretrofit2url-encodingokhttp

Retrofit2 Replacing query parameters


I am using Retrofit2 and sending request with input parameters as follows. But retrofit automatically converts + symbol to %2B. How to encode this and send as + itself

Relevant code

1) Interface

@POST("/registrationapi.php")
    Call<RegistrationPOJO> registrationResponse(@Query("firstname") String firstname , @Query("lastname") String lastname,
                                                @Query("email") String email, @Query("password") String password,
                                                @Query("uid") String uid, @Query("mobile") String mobile,
                                                @Query("key") String key
    );

2) RestClient

private APIInterface service;

public RestClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppConfiguration.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();
        service = retrofit.create(APIInterface.class);
    }
public void getRegistrationInfo(final Registration context, String firstname, String lastname, String email,
                                    String password, String uid, String mobile, String key
                                    ){
        Call<RegistrationPOJO> reg =service.registrationResponse(firstname,lastname,email,password,uid,mobile,key);
        reg.enqueue(
                new Callback<RegistrationPOJO>() {

                    @Override
                    public void onResponse(Call<RegistrationPOJO> call, Response<RegistrationPOJO> response) {
                        success = response.isSuccessful();

                        if(success) {

                            //Handle success flow 
                        } else {
                            //Handle error flow 
                        }
                    }
                    @Override
                    public void onFailure(Call<RegistrationPOJO> call, Throwable t) {
                        //Handle error flow 
                    }
                }
        );
    }

My mobile number is having + symbol at the beginning. From the retrofit logs, I can see this is converted like mobile=%2B11111111111 while sending the request.

I am expecting encoding and making input parameter like mobile=+11111111111

Corresponding gradle dependencies are

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

As per anurag's suggestion. I have changed parameter to

@Query(value = "mobile" , encoded=true) String mobile

and its working as expected


Solution

  • Try using encoded = true in query params.

    Call<ResponseBody> method(@Query(value = "+11111111111", encoded = true) String mobile) {
            .....
          }