Java 改造错误 URL 查询字符串不能有替换块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24610243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrofit error URL query string must not have replace block
提问by tamtoum1987
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.
retrofit.RetrofitError: InterfaceUser.retrieveUsersByFilters: URL 查询字符串“filters[0][field]={param}&filters[0][operator]=equals&filters[0][value]={value}”不能有替换块。
I already test url on firefox and it work fine.
我已经在 Firefox 上测试了 url,它工作正常。
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);
采纳答案by Jake Wharton
Query params have their own annotation which automatically appends to the URL.
查询参数有自己的注释,可以自动附加到 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
您可以@Query
在其 Javadoc上阅读更多信息
回答by Tr4X
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);
回答by SATHISH RA
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 pidin the @GET,, Retrofit automatically fix the url, using @Query
不要把pid放在@GET中,Retrofit会自动修复url,使用@Query
回答by Barakuda
From the JavaDoc:
从JavaDoc:
Example 1:
示例 1:
@GET("/friends")
Call<ResponseBody> friends(@Query("page") int page);
Calling with foo.friends(1) yields /friends?page=1. Example with null:
使用 foo.friends(1) 调用会产生 /friends?page=1。空值示例:
Example 2:
示例 2:
@GET("/friends")
Call<ResponseBody> friends(@Query("group") String group);
Calling with foo.friends(null) yields /friends. Array/Varargs Example:
使用 foo.friends(null) 调用会产生 /friends。数组/可变参数示例:
Example 3:
示例 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.
使用 foo.friends("coworker", "bowling") 调用会产生 /friends?group=coworker&group=bowling。默认情况下,参数名称和值是 URL 编码的。指定 encoding=true 以更改此行为。
Example 4:
示例 4:
@GET("/friends")
Call<ResponseBody> friends(@Query(value="group", encoded=true) String group);
Calling with foo.friends("foo+bar")) yields /friends?group=foo+bar.
使用 foo.friends("foo+bar")) 调用会产生 /friends?group=foo+bar。