It would be very handy if something like this could be done:
@GET(value = "search")
Observable<OutputSearch> search(@QueryObject InputSearch input);
Where InputSearch would be something like
public class InputSearch {
@SerializedName("q")
private String query;
private String type;
public InputSearchArtist(String type,String query) {
this.query = query;
this.type = type;
}
}
The @QueryObject parameter would be converted to query parameters automatically.
I would do it myself, but if I use the notation above I always get the error:
No Retrofit annotation found
Thanks in advance!
@NightlyNexus but if I understand correctly that simply converts an object to JSON and puts it all in a single Query Parameter called "value", correct?
Sorry for not being more clear, but in the example above I wanted a query like this:
/search?q=some_query&type=some_type
So, the object would be converted to multiple query parameters and not just one with the whole object.
Hope it's clearer now and thanks again!
Oh, gotcha.
You want your Java object as a map to pass to @QueryMap.
This looks like it'd require
@QueryObject and probably @HeaderObjectConverter<?, Map<String, String>> objectConverter(Type type, Annotation[] annotations, Retrofit retrofit) on Converter.Factory which would be tricky when you have nesting in your object.Sounds rough.
Is there a use case where you can't use @QueryMap easily?
Yeah, nesting (in my opinion) wouldn't be supported, and would throw an exception if the object had it.
Using a QueryMap would be doable but requires the user to always explicitly convert their object to a map before each invocation.
This would be a nice clean shortcut to avoid that conversion and would also avoid everyone that needs this to write their own conversion code with checks for nested objects and other potential checks that need to be made :)
Thank you for your replies!
Actually you can define a class extends HashMap, that is
@GET(value = "search")
Observable<OutputSearch> search(@QueryMap InputSearch input);
and the definition of InputSearch is:
public class InputSearch extends HashMap<String, String> {
public InputSearchArtist(String type,String query) {
put("query", query);
put("type", type);
}
}
hope it's helpful to you
Thanks @ylfzq . :) That works, yes.
I was just look for a way to make it all work seamlessly without any extra code and without having to write a special class for the inputs.
But thanks, that's a good idea! I can make an InputBase class that automatically converts itself into a HashMap.
We call this "parameter handlers" and it's not currently supported (see tracking issue #626). It's a very, very powerful feature but it exposes a lot of the internals that we aren't prepared to expose just yet.
Its been 3 and a half years since the last comment. Is there really no updates about @QueryObject?
Most helpful comment
Actually you can define a class extends HashMap, that is
and the definition of InputSearch is:
hope it's helpful to you