0

I am trying to send a String[] array as a name value pair in a HashMap to the backend but the backend receives the payload as follows:

Parameters: {"gallery_file_ids"=>"[Ljava.lang.String;@4acac2e"}

The array values are not sent and also an additional "" is surrounding the array value. Here's my code:

Setting up the array:

String[] gallery_array = new String[2];
gallery_array[0] = "2134564";

HashMap<String, Object> params = new HashMap<>();
params.put("gallery_file_ids", gallery_array);

Retrofit API

@FormUrlEncoded
@PATCH("api/profile.json")
Call<Profile> updateProfile(@FieldMap HashMap<String, Object> params);

From what I understand, a string representation of the string array is bring formed by retrofit. How can I resolve this?

Jay
  • 4,873
  • 7
  • 72
  • 137
  • `gallery_array` is just a reference to the memory address that contains the contents of the array. You have to say what you want to do with the array (i.e. turn it into a comma separated list). Maybe for Retrofit it is enough to declare the `HashMap` as `` – nbokmans May 16 '17 at 06:55
  • @nbokmans No difference mate. Still the same response is logged in backend – Jay May 16 '17 at 07:00

4 Answers4

1

I managed to fix it by creating a custom java class containing an ArrayList instead of a string array:

Custom class - NewGalleryUpload.java

public class NewGalleryUpload {
    public ArrayList<String> gallery_file_ids = new ArrayList<>();
}

The Retrofit API was changed to the following:

//An object is sent in the body instead of a HashMap
@PATCH("api/profile.json")
Call<BusinessProfile> updateProfile(@Body NewGalleryUpload newGalleryUpload);

So basically, a new object of NewGalleryUpload is initialised and the values are added to the gallery_file_ids ArrayList and passed in the body of the request. Hope this helps someone!

Jay
  • 4,873
  • 7
  • 72
  • 137
0

@FieldMap accept real type is Map<String, String>, whatever your values is String[] or other, values are converted to strings using String.valueOf(Object), it is the same as @Field.

if you want to use arrays as params, you can try following code:

@FormUrlEncoded
@PATCH("api/profile.json")
Call<Profile> updateProfile(@Field("gallery_file_ids")  ArrayList<String> ids);

or you can see the question How to post array in retrofit android

Community
  • 1
  • 1
0

if you want to add values in Hashmap<> then you have to enter one index value at a time like this: params.put("gallery_file_ids", gallery_array[0]);

0
@Headers("Content-Type: application/json")
@POST("api/profile.json")
Call<Profile> updateProfile(@body List<YuorClassName> data);

You need to pass your class type list to post jsonArrayList.

and to use custom with custom header it will be like this -

@Headers("Content-Type: application/json")
@POST("api/profile.json")
Call<Profile> updateProfile(@HeaderMap Map<String, String> headers, @body List<YuorClassName> data);

and from your activity -

    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("SESSION_ID",getSeassionDataNew().getData().getSESSIONID());
    headers.put("SESSION_TOKEN", getSeassionDataNew().getData().getSESSIONTOKEN());

    .....

    List<YuorClassName> data = new ArrayList<>();

    ......

    Call call = apiInterface.updateProfile(headers , data);
Atiar Talukdar
  • 668
  • 11
  • 22