How to send Hashmap with value with Retrofit
HashMap with retrofit:
@FormUrlEncoded
@POST("register.php")
Call<ResponseBody> RegisterUser(@FieldMap Map<String, String> params);
Map<String, String> params = new HashMap<>();
params.put("userName", edt_uname.getText().toString());
params.put("userEmail", edt_email.getText().toString());
ApiInterface apiService = ApiClient.getLogin().create(ApiInterface.class);
apiService.RegisterUser(params))
public class ApiClient {
private static Retrofit retrofit = null;
public static Retrofit getLogin() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(GlobalConfig.Main_Url)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
WinterChilly
I'm an Android Developer professionally for about 4 years, well I started 8 years ago. Check my Linkedin or my webpage if you want to know more about me: https://www.linkedin.com/in/primo%C5%BE-pesjak-201405112/
Updated on June 05, 2022Comments
-
WinterChilly almost 2 years
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.
-
WinterChilly almost 8 yearsThis is with Volley and is not answering the question.
-
WinterChilly almost 8 yearsWhat if i have to give an Api key with the fieldmap?
-
ViramP almost 8 yearsparams.put("apikey", yourkey);
-
WinterChilly almost 8 yearsDoesn't work because there has to be something like this, because if not i get an error that apikey isn't set:
(@Query("apikey") String api_key, @FieldMap Map<String, String> meta);
-
WinterChilly almost 8 yearsYeah, but the project is pretty big, I can only send you the pieces of code if that is okey? I can send you the php code of api.
-
WinterChilly almost 8 yearsLet us continue this discussion in chat.