Retrofit and GET using parameters

前端 未结 4 1302
时光取名叫无心
时光取名叫无心 2020-12-02 16:07

I am trying to send a request to the Google GeoCode API using Retrofit. The service interface looks like this:

public interface FooService {    
    @GET(\"         


        
4条回答
  •  臣服心动
    2020-12-02 16:59

    @QueryMap worked for me instead of FieldMap

    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 params, 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("key1", "val1");
       params.put("key2", "val2");
       // ... as much as you need.
    
       service.getMyThing(params, new Callback() {
           // ... do some stuff here.
       });
    }
    }
    

    The URL called will be http://www.example.com/thing/?key1=val1&key2=val2

提交回复
热议问题