Can you please explain how to post data using hashmap in retrofit2 ?
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");
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
Related
code is:
HashMap<String, String> map = new HashMap<>();
JSONObject jsonResponse = new JSONObject(response);
HashMap<String, String> question1 = readJsonObject(jsonResponse.getJSONObject("1"), map);
HashMap<String, String> question2 = readJsonObject(jsonResponse.getJSONObject("2"), map);
HashMap<String, String> question3 = readJsonObject(jsonResponse.getJSONObject("3"), map);
q1.setText(question1.get("question")); //q1,q2,q3 are textviews
q2.setText(question2.get("question"));
q3.setText(question3.get("question"));
this is readJsonObject method/function:
private HashMap<String,String> readJsonObject(JSONObject jsonObject,HashMap<String, String> map ) throws JSONException {
map.put("question", jsonObject.getString("question"));
map.put("optiona", jsonObject.getString("optiona"));
map.put("optionb", jsonObject.getString("optionb"));
map.put("optionc", jsonObject.getString("optionc"));
map.put("optiond", jsonObject.getString("optiond"));
map.put("correct", jsonObject.getString("correct"));
return map;
}
but i am getting same string value in q1 q2 q4 TextViews. please solve this.
response getting from the server:
{
"0":[],
"success":true,
"1":{ "question":"question3",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optiona"},
"2":{ "question":"question1",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optiond"},
"3":{ "question":"question4",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optionc"}
}
getting different value from server in response but q1,q2,q3 showing same values. why? please solve this
if i am adding map.clear() after every readJsonObject() textview becomes blank,,,,, and if i add map.clear in readJsonObject() no change still same value in q1,q2,q3
if i use different hashmap in every time calling readJsonObject getting right answer but i think it is not efficient. right?
Change the method to this
private HashMap<String,String> readJsonObject(JSONObject jsonObject ) throws JSOException {
HashMap<String, String> tempMap = new HashMap<>();
tempMap.put("question", jsonObject.getString("question"));
tempMap.put("optiona", jsonObject.getString("optiona"));
tempMap.put("optionb", jsonObject.getString("optionb"));
tempMap.put("optionc", jsonObject.getString("optionc"));
tempMap.put("optiond", jsonObject.getString("optiond"));
tempMap.put("correct", jsonObject.getString("correct"));
return tempMap;
}
I want to post a HashMap and a Object using Retrofit.
I tried this code below but received IllegalArgumentException.
#POST("update")
Call<RSP010> postApi010(#FieldMap HashMap<String, String> defaultData, #Body User user);
Logcat
java.lang.IllegalArgumentException: #FieldMap parameters can only be used with form encoding. (parameter #1)
But when I add #FormUrlEncoded. It said
java.lang.IllegalArgumentException: #Body parameters cannot be used with form or multi-part encoding. (parameter #2)
UPDATE CODE
public static HashMap<String, String> defaultData(){
HashMap<String, String> map = new HashMap<>();
map.put("last_get_time", String.valueOf(SharedPreferencesHelper.getLongValue(AppConstants.LAST_GET_UPDATE)));
map.put("duid", SharedPreferencesHelper.getStringValue(AppConstants.DUID));
return map;
My Object which I want to post
int profile_id;
private String name;
private String name_kana; // あいうえお
private int gender; // 1 nam 2 nu
private String birth_day;
private String birth_time;
private String birth_place;
private String relationship;
Explain:
I want to post multiple variables via API to server. FieldMap defaultData for default variables I want to use in every API.
https://futurestud.io/tutorials/retrofit-send-objects-in-request-body I've read this, it said instead of posting all separate variables of an object, I can post an object directly.
You can send #Body User user with #FieldMap HashMap<String, String> defaultData like
String user = new Gson().toJson(user);
HashMap<String, String> map = new HashMap<>();
map.put("last_get_time", String.valueOf(SharedPreferencesHelper.getLongValue(AppConstants.LAST_GET_UPDATE)));
map.put("duid", SharedPreferencesHelper.getStringValue(AppConstants.DUID));
map.put("duid", SharedPreferencesHelper.getStringValue(AppConstants.DUID));
map.put("user", user);
OR
Use #PartMap Map<String, RequestBody>
#Multipart
#POST("update")
Call<RSP010> postApi010(#PartMap Map<String, RequestBody> defaultData);
And create your request parameters
Map<String, RequestBody> map = new HashMap<>();
map.put("last_get_time", toRequestBody(String.valueOf(SharedPreferencesHelper.getLongValue(AppConstants.LAST_GET_UPDATE))));
map.put("duid", toRequestBody(SharedPreferencesHelper.getStringValue(AppConstants.DUID)));
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),new Gson().toJson(user));
map.put("user", body);
// This method converts String to RequestBody
public static RequestBody toRequestBody (String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body ;
}
Modify your method to:
#POST("update")
Call<RSP010> postApi010(#Query("last_get_time") String lastGetTime,
#Query("duid") String uid,
#Body User user);
I'm start with Retrofit.
I thought of Jackson but has given me problems and I guess there will Retrofit thought about this
I have GET endpoints.
I need convert this:
public class BaseRequest {
private String param1;
private String param2;
private String param3;
//Getter & Setters ...
}
on a Map <String, String>
i'm using dagger + retrofit.
How do I can do this?
Thanks
You'll need to provide more information on this, do the parameters have the same query param name or different.
If they are different you can just pass them in as a #QueryMap Map<String, String> params being a Map of key/value pairs. Such that the output would be something like
Map<String, String> values = new HashMap<>();
values.put("name1", "value1");
values.put("name2", "value2");
values.put("name3", "value3");
?name1=value1&name2=value2&name3=value3
If they are the same then you need #Query("name") String... values), the output of this would be something like:
List<String> values = new ArrayList<>();
values.add("value1");
values.add("value2");
values.add("value3");
?name=value1&name=value2&name=value3
I want to pass an array of the same element, but i want them to be passed as new fields. I want to do something like:
#POST("/api/userInfo/changeProfileData/")
void updateProfile(
#Field("userInfoProfile[languages][0]") String language1,
#Field("userInfoProfile[languages][1]") String language2,
Callback<BaseModel<StateModel>> callback);
But the problem is that i don't know the number of elements so i cannot do like above.
I tried to pass a list:
#Field("userInfoProfile[languages]") List<String> languages
but the value is not valid because i need fields with their index (userInfoProfile[languages][0]).
Use FieldMap to make field association more dynamic.
Update your interface to:
void updateProfile(
#FieldMap Map<String, String> languages,
Callback<BaseModel<StateModel>> callback);
Method that will build FieldMap out of your list
private Map<String, String> buildFieldMap(List<String> data){
Map<String, String> map = new HashMap<String, String>();
String fieldName = "userInfoProfile[languages][%d]"
for(int i = 0; i < data.size(); i++){
map.put(String.format(fieldName, i), data.get(i));
}
return map;
}
In Xamarin, I am trying this tutorial: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html
Here is the code:
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.Put(key, name);
return planet;
}
May I please have some help with the correct using statements for the following types:
Map
ArrayList
HashMap
Thanks in advance
Map and ArrayMap are in java.util. ArrayList is in System.Collections.