Retrofit error URL query string must not have replace block

非 Y 不嫁゛ 提交于 2019-12-20 12:10:32

问题


I have this function

      @GET("/users?filters[0][field]={param}&filters[0][operator]=equals&filters[0][value]={value}")
UserDto retrieveUsersByFilters(@Path("param") String nameFilter, @Path("value") String value);

I try to call it like this :

   UserDto currentUser = interfaceUser.retrieveUsersByFilters(User.LOGIN, login);

But i have error :

retrofit.RetrofitError: InterfaceUser.retrieveUsersByFilters: URL query string "filters[0][field]={param}&filters[0][operator]=equals&filters[0][value]={value}" must not have replace block.

I already test url on firefox and it work fine.

Thank's for your responses

Edit

Solution:

          @GET("/users?filters[0][operator]=equals")
          UserDto retrieveUsersByFilters(
          @Query("filters[0][field]") String nameFilter,
          @Query("filters[0][value]") String value);

回答1:


Query params have their own annotation which automatically appends to the URL.

@GET("/users?filters[0][operator]=equals")
UserDto retrieveUsersByFilters(
    @Query("filters[0][field]") String nameFilter,
    @Query("filters[0][value]") String value);

You can read more about @Query on its Javadoc




回答2:


 URL="/api-mobile_prateek2.php?method=getProductById&pid="


 @GET("/api-mobile_prateek2.php?method=getProductById")
    Call<Product> responseproduct(@Query("pid") String pid);

dont put the pid in the @GET,, Retrofit automatically fix the url, using @Query




回答3:


Don't put your values directly in the path, but prefer in the method signature. Not completely sure, but try something like this :

@GET("/users?filters[0][operator]=equals")
UserDto retrieveUsersByFilters(@Path("filters[0][field]") String nameFilter, @Path("filters[0][value]") String value);



回答4:


From the JavaDoc:

Example 1:

@GET("/friends")
 Call<ResponseBody> friends(@Query("page") int page);

Calling with foo.friends(1) yields /friends?page=1. Example with null:


Example 2:

@GET("/friends")
 Call<ResponseBody> friends(@Query("group") String group);

Calling with foo.friends(null) yields /friends. Array/Varargs Example:


Example 3:

@GET("/friends")
 Call<ResponseBody> friends(@Query("group") String... groups);

Calling with foo.friends("coworker", "bowling") yields /friends?group=coworker&group=bowling. Parameter names and values are URL encoded by default. Specify encoded=true to change this behavior.


Example 4:

@GET("/friends")
 Call<ResponseBody> friends(@Query(value="group", encoded=true) String group);

Calling with foo.friends("foo+bar")) yields /friends?group=foo+bar.



来源:https://stackoverflow.com/questions/24610243/retrofit-error-url-query-string-must-not-have-replace-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!