android – How to send Hashmap with value with Retrofit

I’m trying to send to API through POST an HashMap and an API KEY but I can’t, because I need to send it like I do a @Field(“something”).
So what i want is @FieldMap(“meta”)

Example how i use Field:

@FormUrlEncoded
@POST("/api")
Call<CallBackMethod> save(@Query("apikey") String api_key,
                          @Field("something") String test);

How I currently use FieldMap, but the field map doesn’t get send because the API is expecting an Array with a name “meta”, so I get an error response from server Metadata does not exist in POST. THE API WORKS.
But I need to send it an Array with keys and values, that is a Hashmap in Java.

I’m calling this API something like this:

  Map<String, String> meta = new HashMap<>();
        meta.put("user_id", user_id);
        final Api apiService = ApiClient.getAPI().create(Api.class);
        Call<OtherMethod> call = apiService.getOtherMethod(API_KEY, meta);
  call.enqueue(new Callback<OtherMethod>() {
        @Override
        public void onResponse(Call<OtherMethod> call, Response<OtherMethod> response) {
            Log.d("Response Raw", response.raw() + "");
            Log.d("Response Raw", response.isSuccessful() + "");

        }

        @Override
        public void onFailure(Call<OtherMethod> call, Throwable t) {
            Log.e("E: t.toString());
        }
    });

Other class where the methods are:

   @FromUrlEncoded
        @POST("/api")
        Call<OtherMethod> getOtherMethod(@Query("apikey") String api_key,
                                              @FieldMap Map<String, String> meta);

EDIT 1:

After some messing around, I managed to get meta to be accepted, but now i have problems with encoding.
Current API:

   @FormUrlEncoded
    @POST("/api")
    Call<CallBackMethod> getSomething(@Query("apikey") String api_key,
                                  @FieldMap HashMap<String,HashMap<String,String>> meta);

This is what it sends:

D/OkHttp: meta=%7Buser_id%3D431%7D

I tried with encoding=true and false both the same result.
This is what it should send

meta={user_id=431}
Does anybody have any idea how to disable encoding?

EDIT 2:
I changed the API so i send the @BODY and then json and it works. If anyone has solution for top problem i’ll give credit too you.

Loading...

(Visited 1 times, 1 visits today)