URL encoding error in Retrofit 2.0

2.4k views Asked by At
 @GET("images")
 Call<Example> getimages(@Query("where=item_id") int item_id);

When I use this the equal to sign after where gets encode to %3D which my server doesn't accept.I want = symbol after where in my api call.

And my link is images?where=item_id=1

2

There are 2 answers

6
Chintan Soni On BEST ANSWER

Try this way:

@GET("images")
Call<Example> getimages(@Query("where") String item_id);

When you call this method, you have to pass this way:

Service service = retrofit.create(Service.class);
Call<Example> call = service.getimages("item_id=1");

If you can call your Api successfully, you can pass the value dynamically, using string concatenation.

Reason: When passing query parameters you just have to write query parameter in @Query("") and value to it will be assigned on runtime when you will call this method and pass value to "item_id" parameter of getimages method.

To learn more on Retrofit, refer this link: https://futurestud.io/tutorials/tag/retrofit

0
Pratik Popat On

Add encoded flag.

@GET("images")
Call<Example> getimages(@Query("where=item_id", encoded = true) String item_id);

and encode item_id before pass it to this method.