0

I'm start with Retrofit.

I thought of Jackson but has given me problems and I guess there will Retrofit thought about this

I have GET endpoints.

I need convert this:

public class BaseRequest {
    private String param1;
    private String param2;
    private String param3;
    //Getter & Setters ...

}

on a Map <String, String>

i'm using dagger + retrofit.

How do I can do this?

Thanks

1 Answers1

0

You'll need to provide more information on this, do the parameters have the same query param name or different.

If they are different you can just pass them in as a @QueryMap Map<String, String> params being a Map of key/value pairs. Such that the output would be something like

Map<String, String> values = new HashMap<>();
values.put("name1", "value1");
values.put("name2", "value2");
values.put("name3", "value3");

?name1=value1&name2=value2&name3=value3

If they are the same then you need @Query("name") String... values), the output of this would be something like:

List<String> values = new ArrayList<>();
values.add("value1");
values.add("value2");
values.add("value3");

?name=value1&name=value2&name=value3
Phil H
  • 897
  • 4
  • 10
  • 1
    yeah i know that i can add in all my entities a method named toMap and and on this any like: `Map map = new HashMap<>(); map.put("param1","value1"); map.put("param2","value2"); //one for member`But do this for all entities? for all members of all entities? :( not good idea i think. – Alberto Marina Dec 18 '14 at 22:31
  • @AlbertoMarina I think you need to clarify the question please. Not understanding what it is you are trying to achieve. This answer relates to dynamically handling query params in a request. Is this what you wanted to know or is it something else you are after? – Phil H Dec 19 '14 at 07:44
  • Ok, sorry. I need convert a POJO on a Map for retrofit. – Alberto Marina Dec 19 '14 at 17:14
  • @AlbertoMarina have you thought of using reflection on the POJO fields and go that route? Iterate over the fields to get the field name and the value, put those into a map. The only thing with that is the field names have to match the query param name you want to send in the url. – Phil H Dec 19 '14 at 18:02