Retrofit: multiple query parameters in @GET command?

后端 未结 4 1098
误落风尘
误落风尘 2020-12-23 09:30

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

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 10:02

    If you have a bunch of GET params, another way to pass them into your url is a HashMap.

    class YourActivity extends Activity {
    
        private static final String BASEPATH = "http://www.example.com";
    
        private interface API {
            @GET("/thing")
            void getMyThing(@QueryMap Map, new Callback 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 params = new HashMap();
           params.put("foo", "bar");
           params.put("baz", "qux");
           // ... as much as you need.
    
           service.getMyThing(params, new Callback() {
               // ... do some stuff here.
           });
        }
    }
    

    The URL called will be http://www.example.com/thing/?foo=bar&baz=qux

提交回复
热议问题