改进:@GET命令中的多个查询参数? [英] Retrofit: multiple query parameters in @GET command?

查看:399
本文介绍了改进:@GET命令中的多个查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Retrofit和Robospice在我的android应用程序中进行API调用。所有@POST方法效果很好,在URL中没有任何参数的@GET命令也是如此,但我无法获得任何@GET调用来处理末尾的参数!例如,如果我的API路径是my / api / call /,并且我想在URL中使用2个参数param1和param2,则get call看起来像:



http://www.example.com/my/api/call?param1=value1&param2=value2



因此我已设置我的@GET界面如下所示:

  @GET(/ my / api / call?param1 = {p1}& param2 = {p2})
响应getMyThing(@Path(p1)
字符串param1,@Path(p2)
字符串param2);

但是我得到一个错误,提示请求网络执行期间发生异常:URL查询字符串 / my / api / call?param1 = {p1}& param2 = {p2} 方法getMyThing可能没有替换块。



我做错了什么?

解决方案

您应该使用以下语法:

  @GET(/ my / API / call)
响应getMyThing(
@Query(param1 )String param1,
@Query(param2)String param2);

在URL中指定查询参数仅用于知道键和值并且它们是固定的。


I am using Retrofit and Robospice to make API calls in my android application. All @POST methods work great, and so do @GET commands without any parameters in the URL, but I can't get any @GET calls to work with parameters on the end!

For example, if my API path was "my/api/call/" and I wanted 2 parameters "param1" and "param2" in the URL, the get call would look like:

http://www.example.com/my/api/call?param1=value1&param2=value2

so I have setup my @GET interface like so:

@GET("/my/api/call?param1={p1}&param2={p2}")
Response getMyThing(@Path("p1")
String param1, @Path("p2")
String param2);

but I get an error saying
"An exception occurred during request network execution : URL query string "/my/api/call?param1={p1}&param2={p2}" on method getMyThing may not have replaced block."

What am I doing wrong?

解决方案

You should be using this syntax:

@GET("/my/API/call")
Response getMyThing(
    @Query("param1") String param1,
    @Query("param2") String param2);

Specifying query parameters in the URL is only for when you know both the key and value and they are fixed.

这篇关于改进:@GET命令中的多个查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆