Retrofit: How to specify comma-separated parameters in request?
If you are using kotlin and you have var items: List<String>
that should go as a query param.
Use this method to form up string:
fun itemsString(items: List<String>) = items.joinToString(separator = ",")
Your url query should be like :
@Query(value = "items", encoded = true) String items
So, as we already found out the problem was in ?
-> &
typo. But one more thing to be mentioned is that Retrofit can accept complex objects as call parameters. Then String.valueOf(object)
will be called to convert object into Query/Path param.
In your case you could define custom class like that:
class LatLng {
private double lat;
private double lng;
...
@Override public String toString() {
return String.format("%.1f,%.1f", lat, lng);
}
}
And refactor your endpoint method like that:
@GET("/explore?limit=50&radius=25000&v=20140905&venuePhotos=1&oauth_token=xxyyxx")
void getVenues(@Query("ll") LatLng ll, Callback<String> cb);
About parsing the answer:
- Never create
Gson
objects right in callback. It's simply too heavyweight. Use the one you provide toRestAdapter
. - Why do you mix
JsonParser
&Gson
? They are different tools for basically the same problem. - Make use of Retrofit's built-in converter mechanism ;)
From words to code:
public class FoursquareResponse<T> {
private MetaResponse meta;
private T response;
// getters
}
Altogether:
@GET("/explore?limit=50&radius=25000&v=20140905&venuePhotos=1&oauth_token=xxyyxx")
void getVenues(@Query("ll") LatLng ll, Callback<FoursquareResponse<VenuesExploreResponse>> cb);
...
foursquare.getVenues(LatLng.valueOf(30.26, -97.74), new Callback<FoursquareResponse<VenuesExploreResponse>>() {
@Override
public void success(FoursquareResponse<VenuesExploreResponse> r, Response response) {
MetaResponse metaResponse = r.getMeta;
VenuesExploreResponse myResponse = r.getResponse();
// Store results from myResponse in List
}
@Override
public void failure(RetrofitError error) {
Log.d(TAG, "Failures!");
}
});