Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装:@GET命令中有多个查询参数?_Android_Api_Get_Retrofit_Robospice - Fatal编程技术网

Android 改装:@GET命令中有多个查询参数?

Android 改装:@GET命令中有多个查询参数?,android,api,get,retrofit,robospice,Android,Api,Get,Retrofit,Robospice,我正在使用改型和Robospice在我的android应用程序中进行API调用。所有@POST方法都很好用,URL中没有任何参数的@GET命令也很好用,但是我无法得到任何@GET调用来处理最后的参数 例如,如果我的API路径是“my/API/call/”,并且我希望URL中有两个参数“param1”和“param2”,那么get调用如下所示: 因此,我将@GET界面设置为: @GET("/my/api/call?param1={p1}&param2={p2}") Response ge

我正在使用改型和Robospice在我的android应用程序中进行API调用。所有@POST方法都很好用,URL中没有任何参数的@GET命令也很好用,但是我无法得到任何@GET调用来处理最后的参数

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

因此,我将@GET界面设置为:

@GET("/my/api/call?param1={p1}&param2={p2}")
Response getMyThing(@Path("p1")
String param1, @Path("p2")
String param2);
但我得到一个错误,它说:“请求网络执行期间发生异常:URL查询字符串”
/my/api/call?方法getMyThing上的param1={p1}¶m2={p2}
“可能没有替换块。”


我做错了什么?

您应该使用以下语法:

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

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

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

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

如果您有一组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.
       });
    }
}
class YourActivity扩展活动{
专用静态最终字符串基路径=”http://www.example.com";
专用接口API{
@获取(“/事物”)
void getMyThing(@QueryMap,新回调);
}
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_布局);
RestAdapter rest=new RestAdapter.Builder().setEndpoint(BASEPATH.build();
API服务=rest.create(API.class);
Map params=新的HashMap();
参数put(“foo”、“bar”);
参数put(“baz”、“qux”);
//…你需要多少就有多少。
getMyThing(参数,新回调(){
//…在这里做些事情。
});
}
}

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

您可以创建参数映射并按如下方式发送:

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);
Map paramsMap=newhashmap();
paramsMap.put(“p1”,param1);
paramsMap.put(“p2”,param2);
//内线电话
@获取(“/my/api/call”)
响应getMyThing(@QueryMap-paramsMap);
使用Java

@GET("/my/api/call")
Response getMyThing(@Query("p1")
String param1, @Query("p2")
String param2);
与科特林合作

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

我现在收到以下错误:11-14 10:29:48.626:E//RequestRunner.java:134(31776):10:29:48.630 Thread-11782请求网络执行期间发生异常:null我正在spicemanager中设置接口,不确定什么可以为null…我不确定,抱歉。我从未使用过机器人世界。我在这里只是为了改进:)嗨,杰克,在你必须自己构造查询参数的场景中,这将是完美的,但是如果它们是预先提供给你的呢?想象一个模板响应,它告诉您从哪里获取信息,并为您设置这些参数。所以/my/api/call?param1={p1}¶m2={p2}实际上并不是由您手动创建的,而是在另一个调用的响应中提供给您的。@Javier Tarazaga很抱歉,这太晚了,但在改型1和2中,您可以使用
QueryMap
-看到这就是我要找的,我们可以使用这个发送多个查询参数。这是一个更好的解决方案,我不知道为什么它没有被选为最佳答案