Corneliu Corneliu - 3 months ago 14
Android Question

Retrofit ws error using @Query

I have try this interface :

public interface InterfaceWs
{
@GET("/?extract-mode=bestdeals&api-key={apikey}") public Observable<List<ModelBestDeals>> getBestDeals(@Query("apikey") String apikey);
}


Before using
@Query
I was using
@Path
. I change it
and I receive this error :

URL query string "extract-mode=bestdeals&api-key={apikey}" must not have replace block. For dynamic query parameters use @Query.


What is wrong ?

Answer
 @GET("/?extract-mode=bestdeals&api-key={apikey}") public Observable<List<ModelBestDeals>> getBestDeals(@Query("apikey") String apikey);

should be

@GET("/?extract-mode=bestdeals") public Observable<List<ModelBestDeals>> getBestDeals(@Query("api-key") String apikey);

retrofit will take care of completing your url with api-key=value, where value is the value of apikey. You could also use a QueryMap to provide the other pair extract-mode=bestdeals. E.g.

 Map<String, String> map = new HashMap<>();
 map.put("extract-mode", "bestdeals");
 map.put("api-key", apikey);

and your method

  @GET("/") public Observable<List<ModelBestDeals>>  
  getBestDeals(@QueryMap Map<String, String> values);

which is, in my opinion, way more readable