androidretrofitretrofit2urlencodecfurl-encoding

URL encoding error in Retrofit 2.0


 @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


Solution

  • 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