Perry Hoekstra Perry Hoekstra - 1 year ago 179
Android Question

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 {
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(
06-07 13:18:55.337: E/AndroidRuntime(3756): at retrofit.RestMethodInfo.parsePath(
06-07 13:18:55.337: E/AndroidRuntime(3756): at retrofit.RestMethodInfo.parseMethodAnnotations(
06-07 13:18:55.337: E/AndroidRuntime(3756): at

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

I took the code pretty much verbatim from here: so I am a bit of a loss to understand the issue.


Answer Source

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

public interface FooService {    

    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 {    

    void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download