4

First I know my title is bad as I didn't come up with better, I'm opened to suggestion.

I'm using retrofit to get data from an api of this kind : @GET("users/{userid}")

It works fine and I'm happy with it, the problem is when I call the same api with @POST("users/widget") with a list of ids. I have the following answer :

{
  "long_hash_id": {
    "_id": "long_hash_id"
    .......
  },
  "long_hash_id": {
    "_id": "long_hash_id",
    .....
  },
  ........
}

the "long_hash_id" is typicaly "525558cf8ecd651095af7954" it correspond to the id of the user attached to it.

When I didn't use retrofit, I used Gson in stream mode to get each user one by one. But I don't know how to tell retrofit.

Hope I'm clear and Thank you in advance.

----------- Solution :

I made my interface this way :

@FormUrlEncoded
@POST(AppConstants.ROUTE_USER_GROUP)
Call<Map<String,User>> getUsers( @Field("ids") List<String> param, @QueryMap Map<String,String> options);

and I simply gave my ArrayList of ids. Thank you very much

2
  • Did you want to parse it ? or whet is the problem Commented Nov 12, 2015 at 14:58
  • yes I want to parse it with retrofit, for now I don't use retrofit. Commented Nov 12, 2015 at 15:08

1 Answer 1

7

Gson is able to deal with JSON objects with variable keys like the one you posted. What you have to do, in this case, is to declare a Map<String, ModelClass>, where ModelClass is the content of the JSONObject you want to represent

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you ! I declare it to who ? I create a new Model containing juste this map ? maybe you have a link or key words for me to find it ?
post the code you are using. Your Retrofit interface declaration, and the Model class. I'll try to guide you trough that
ok I did it, "jsonObjectToUrlEncodedString" take in this case the json array and convert it to urlencoded,
for now I use generics because it's a method used for a lot of routes that works the same way
Ok, thank you very much it worked. I update my title and my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.