Search code examples
androidretrofitretrofit2urlencode

How to avoid URL encoding in Retrofit?


I'm using retrofit for my network layer I'm facing a strange issue I've the parameter and it's value look like this

store://0TxIGQMQbObzvU4Apia0V0&callback=

and after encoding it changes to this

store://0TxIGQMQbObzvU4Apia0V0%26callback%3D

and for some reason server is not liking this encoding and I'm getting "HTTP 400 Bad Request".

If I hit without encoding it runs fine so I was wondering is there is any way i can disable the endoing.

I've tried other ways like this

@Query(value "env" encoded = false) String env

but no luck.

This is my network interface class

public interface NetworkService
{
    @GET("v1/public/yql")
    Observable<SeriesEntity> getOnGoingSeries(@Query("q") String query,
                                              @Query("format") String format,
                                              @Query("diagnostics") boolean flag,
                                              @Query("env") String env);


    class Factory {
        public  static NetworkService create()
        {
            OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            clientBuilder.addInterceptor(loggingInterceptor);

            Gson gson = new GsonBuilder().disableHtmlEscaping().create();

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://query.yahooapis.com")
                    .client(clientBuilder.build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();

            return retrofit.create(NetworkService.class);
        }
    }
}

Edit :

I also notice that the issue is only the encoded value of '&' because if i replace it value (whic is %26) it works fine


Solution

  • the issue I believe is that you're including callback param as part of env one. Try adding @Query("callback") boolean callback to your retrofit interface (and using just store://0TxIGQMQbObzvU4Apia0V0 for env)