Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit and GET using parameters

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

public interface FooService {         @GET("/maps/api/geocode/json?address={zipcode}&sensor=false")     void getPositionByZip(@Path("zipcode") int zipcode, Callback<String> cb); } 

When I call the service:

OkHttpClient okHttpClient = new OkHttpClient();  RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Constants.GOOGLE_GEOCODE_URL).setClient(new OkClient(okHttpClient)).build();  FooService service = restAdapter.create(FooService.class);  service.getPositionByZip(zipCode, new Callback<String>() {     @Override public void success(String jsonResponse, Response response) {        ...     } @Override public void failure(RetrofitError retrofitError) {     } }); 

I receive the following stacktrace:

06-07 13:18:55.337: E/AndroidRuntime(3756): FATAL EXCEPTION: Retrofit-Idle 06-07 13:18:55.337: E/AndroidRuntime(3756): Process: com.marketplacehomes, PID: 3756 06-07 13:18:55.337: E/AndroidRuntime(3756): java.lang.IllegalArgumentException: FooService.getPositionByZip: URL query string "address={zipcode}&sensor=false" must not have replace block. 06-07 13:18:55.337: E/AndroidRuntime(3756):     at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:120) 06-07 13:18:55.337: E/AndroidRuntime(3756):     at retrofit.RestMethodInfo.parsePath(RestMethodInfo.java:216) 06-07 13:18:55.337: E/AndroidRuntime(3756):     at retrofit.RestMethodInfo.parseMethodAnnotations(RestMethodInfo.java:162) 06-07 13:18:55.337: E/AndroidRuntime(3756):     at  

I took a look at the StackOverflow question: Retrofit: multiple query parameters in @GET command? but it did not seem applicable.

I took the code pretty much verbatim from here: http://square.github.io/retrofit/ so I am a bit of a loss to understand the issue.

Thoughts?

like image 381
Perry Hoekstra Avatar asked Jun 07 '14 18:06

Perry Hoekstra


People also ask

How do you send parameters in retrofit kotlin get request?

Create a function in the viewmodel class and pass the parameter in the function itself, then make the service call from that funciton.

How do I add parameters to a retrofit request?

You can do that by adding a new request interceptor to the OkHttpClient . Intercept the actual request and get the HttpUrl . The http url is required to add query parameters since it will change the previously generated request url by appending the query parameter name and its value.


2 Answers

AFAIK, {...} can only be used as a path, not inside a query-param. Try this instead:

public interface FooService {          @GET("/maps/api/geocode/json?sensor=false")     void getPositionByZip(@Query("address") String address, Callback<String> cb); } 

If you have an unknown amount of parameters to pass, you can use do something like this:

public interface FooService {          @GET("/maps/api/geocode/json")     @FormUrlEncoded     void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb); } 
like image 139
Bart Kiers Avatar answered Sep 19 '22 19:09

Bart Kiers


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

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

like image 35
madhu527 Avatar answered Sep 18 '22 19:09

madhu527