Can you please explain how to post data using hashmap in retrofit2 ?
Asked
Active
Viewed 1.1k times
2 Answers
14
This is what I post
@FormUrlEncoded
@POST("getProfile")
Call<YourResponseObject> getProfile(@FieldMap HashMap<String, String> data);
And the HashMap
HashMap<String, String> map = new HashMap<>();
map.put("token", "yourtoken");
map.put("yourvariable", "yourvariable");
Truong Hieu
- 3,309
- 2
- 20
- 37
-
Can you please explain how total process go till i get the response using any other example ! – Akash Ratanpara Feb 24 '17 at 11:51
-
@AkashRatanpara instead of sending parts of request, you can use HashMap, using HashMap can dynamically what params you want to send. The response of API will automatically parsing using GSON to `YourResponseObject`. – Truong Hieu Feb 24 '17 at 11:58
-
More useful details and examples, you can visit this site. I learned from this. https://futurestud.io/tutorials/retrofit-getting-started-and-android-client – Truong Hieu Feb 24 '17 at 11:58
-
If i don't want to make model then what is other way to call service? – Akash Ratanpara Feb 24 '17 at 12:06
-
@AkashRatanpara you can parse it to `String`. Not by using `GSON` use `Scalar` converter. – Truong Hieu Feb 25 '17 at 16:26
1
From Retrofit2 documentation check FieldMap for more details You need to create your interface
public interface YourPostService {
@FormUrlEncoded
@POST("/myEndpoint")
Call<YourResponseClass> postData(@FieldMap Map<String, String> fields);
}
and after this is easy to call and use it
dario.budimir
- 161
- 2
- 9