【问题标题】:Retrofit: multiple query parameters in @GET command?改造:@GET 命令中的多个查询参数?
【发布时间】:2013-11-27 07:25:42
【问题描述】:

我正在使用 Retrofit 和 Robospice 在我的 Android 应用程序中进行 API 调用。所有@POST 方法都工作得很好,在URL 中没有任何参数的@GET 命令也是如此,但是我不能让任何@GET 调用最终使用参数!

例如,如果我的 API 路径是“my/api/call/”并且我希望 URL 中有 2 个参数“param1”和“param2”,那么 get 调用将如下所示:

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

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

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

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

我做错了什么?

【问题讨论】:

    标签: android api get retrofit robospice


    【解决方案1】:

    您应该使用以下语法:

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

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

    【讨论】:

    • 我现在收到以下错误:11-14 10:29:48.626: E//RequestRunner.java:134(31776): 10:29:48.630 Thread-11782 期间发生异常请求网络执行:null 我肯定在我的 spicemanager 中设置接口,不确定什么可能是 null...
    • 我不确定,抱歉。我从未使用过 RoboSpice。我只是来改造的 :)
    • 嗨,Jake,在您必须自己构建查询参数的情况下,我们是否会完美,但是如果它们是预先给您的呢?想象一个模板响应,它告诉您从哪里获取信息并有这些参数供您设置。所以 /my/api/call?param1={p1}&param2={p2} 实际上并不是你手动创建的,而是在另一个调用的响应中提供给你的。
    • @Javier Tarazaga 抱歉,这已经晚了,但在改造 1 和 2 中,您可以使用 QueryMap - 请参阅 this example here
    【解决方案2】:

    如果您有一堆 GET 参数,另一种将它们传递到您的 url 的方法是 HashMap。

    class YourActivity extends Activity {
    
        private static final String BASEPATH = "http://www.example.com";
    
        private interface API {
            @GET("/thing")
            void getMyThing(@QueryMap Map<String, String>, new Callback<String> callback);
        }
    
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.your_layout);
    
           RestAdapter rest = new RestAdapter.Builder().setEndpoint(BASEPATH).build();
           API service      = rest.create(API.class);
    
           Map<String, String> params = new HashMap<String, String>();
           params.put("foo", "bar");
           params.put("baz", "qux");
           // ... as much as you need.
    
           service.getMyThing(params, new Callback<String>() {
               // ... do some stuff here.
           });
        }
    }
    

    调用的 URL 将是 http://www.example.com/thing/?foo=bar&amp;baz=qux

    【讨论】:

    • 这就是我要找的,我们可以使用它发送多个查询参数。
    【解决方案3】:

    您可以创建一个参数映射并将其发送如下:

    Map<String, String> paramsMap = new HashMap<String, String>();
    paramsMap.put("p1", param1);
    paramsMap.put("p2", param2);
    
    // Inside call
    @GET("/my/api/call")
    Response getMyThing(@QueryMap Map<String, String> paramsMap);
    

    【讨论】:

    • 这是一个更好的解决方案我不知道为什么它没有被选为最佳答案
    【解决方案4】:

    不要在 GET-URL 中编写查询参数。这样做:

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

    【讨论】:

      【解决方案5】:

      使用 Java

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

      使用 Kotlin 协程

       @GET("/my/api/call")
       suspend fun getSearchProduct(@Query("p1") p1: String , @Query("p2") p2: String )
      

      【讨论】:

        【解决方案6】:

        这是您可以使用的最佳方式:

        @GET("api_link_put_here")
            apiResponse getMyData(
                    @Query("first_param") String firstParam,
                    @Query("second_param") String secondParam
            );
        

        这将自动像这样工作: http://baseurl.com/api_link_put_here?first_param=firstParam&second_param=secondParam

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-28
          • 2019-05-15
          • 1970-01-01
          • 2016-01-18
          相关资源
          最近更新 更多