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

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

问题描述

我正在使用 Retrofit 和 Robospice 在我的 android 应用程序中进行 API 调用.所有@POST 方法都工作得很好,URL 中没有任何参数的@GET 命令也是如此,但我无法获得任何@GET 调用来处理参数!

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!

例如,如果我的 API 路径是my/api/call/"并且我想要 URL 中的 2 个参数param1"和param2",则 get 调用将如下所示:

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

所以我像这样设置了我的@GET 接口:

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);

但我收到一条错误消息,说
请求网络执行期间发生异常:URL 查询字符串"/my/api/call?param1={p1}&param2={p2}" 方法 getMyThing 可能没有替换块."

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."

我做错了什么?

推荐答案

您应该使用以下语法:

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

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

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天全站免登陆