im trying to mimic this structure to post data to mailchimp, im using volley
{
"email_address": "testchimp#gmail.com",
"status": "subscribed",
"merge_fields": {
"FNAME": "testmailchimp",
"LNAME": "testingit",
}
}
i have done this, but i need to add the FNAME and LNAME and i dont know how to do it right
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("email_address","testchimp#gmail.com");
params.put("status","unsubscribed");
//here i want to add the arraylist FNAME and LNAME
return params;
}
Solved my problem with this
final JSONObject jsonBody = new JSONObject("{\"email_address\":\"mailchimptester#gmail.com\"," +
"\"status\":\"unsubscribed\"," +
"\"merge_fields\":{\"FNAME\":\"test\",\"LNAME\":\"teste\"}}");
So with this i can do the json how i want without maps and then validate it here https://jsonlint.com/
It's not an ArrayList it would be a Map. You can do it using this code
HashMap<String,String> merge_fields = new HashMap<>();
merge_fields.put("FNAME","testmailchimp");
merge_fields.put("LNAME","testingit");
String merge_fields_param = new JSONObject(merge_fields).toString();
params.put("merge_fields",merge_fields_param);
Related
I have a JSON Object I want to parse at this URL https://api.adviceslip.com/advice with this content:
{"slip": { "id": 137, "advice": "You're not that important; it's what you do that counts."}}
I have written this code in Android Studio but it does not seem to work.
String jsonString = handler.httpServiceCall(url);
if (jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject slip = jsonObject.getJSONObject("slip");
String id = slip.getString("id");
Log.d("slip id:", id);
String advice = slip.getString("advice");
Log.d("slip adv:", advice);
HashMap<String, String> map = new HashMap<>();
map.put("id", id);
map.put("advice", advice);
adviceSlip.setText(map.get("advice"));
}
Any help would be appreciated :)
One problem is that you are trying to get a String on an Int object, and perhaps this is one of your errors, please change your
splip.getString("id");
by
slip.getInt("id);
Another one is that you are creating a HashMap<String,String> but the id you getting from the json is an Int and perhaps you should change it to use HashMap<Int,String>
You can get the value from the jsonobject and simple set that value on the textview.
String jsonString = handler.httpServiceCall(url);
if (jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject slip = jsonObject.getJSONObject("slip");
String id = slip.getString("id");
Log.d("slip id:", id);
String advice = slip.getString("advice");
adviceSlip.setText(advice);
}
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 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;
}
I have the json array below which I am parsing and displaying it in listview row wise but I would like to combine/group same users to one row .Should I do it in android or mysql ? I am giving the working code for mysql too .I really appreciate any help.Thanks in Advance.
MYSQL:
http://sqlfiddle.com/#!2/19ea8/5
ANDROID:
wihin mainactivity for listview:
JSONArray arr = result.getJSONArray("JsonResultArray");
for(int i=0;i<arr.length();i++)
{
JSONObject e1 = arr.getJSONObject(i);
JSONObject json2 = (JSONObject) e1.get("data");
String name = json2.getString("name").trim();
String receiver = json2.getString("receiver").trim();
String sender = json2.getString("sender").trim();
String date = json2.getString("date").trim();
String msg = json2.getString("msg").trim();
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, Integer.toString(i+1));
map.put(KEY_TITLE, name);
map.put(KEY_msg, msg);
map.put(KEY_date,date);
// adding HashList to ArrayList
msgList.add(map);
}
}catch(Exception e){
e.printStackTrace();
}
in convert view:
HashMap<String, String> msgs = new HashMap<String, String>();
msgs = data.get(position);
name.setText(msgs.get(PopoverViewActivity.KEY_TITLE));
msg.setText(msgs.get(PopoverViewActivity.KEY_msg));
date1.setText(msgs.get(PopoverViewActivity.KEY_date));
If you want to group same users to one row. I suggest you to edit your mysql. You need to use
select * from table group by user
in your mqsql query, that can make you convenient to change your db and doesn't influence your UI.
In Android, just call your api and present data to listView.
I've created a JSON encode where you enter a HashTable (public Hashtable<?, ?> JSonDecode(String data) {... return objJS.toString(); } ) and get a string in JSON format. That is:
If I have a Hashtable with this (Hashtable in Hashtable):
Example Hashtable:
Hashtable<String, Object> exampleHT = new Hashtable<String, Object>();
exampleHT.put("Color", "Red");
exampleHT.put("OtherKey", "OtherValue");
exampleHT.put("OtherKey2", "OtherValue2");
Hashtable<String, Object> country = new Hashtable<String, Object>();
country.put("Spain", "Madrid");
country.put("France","Paris");
country.put("Italy", "Rome");
Hashtable<String, String> pokemon = new Hashtable<String, String>();
pokemon.put("Pikachu", "Electric");
pokemon.put("Charmander","Fire");
country.put("Pokemons", pokemon);
exampleHT.put("Countries", country);
I use my function(JSonEncode(exampleHT);) and I get this string:
{
"Color":"Red",
"Countries":{
"Spain":"Madrid",
"France":"Paris",
"Italy":"Rome",
"Pokemons":{
"Pikachu":"Electric",
"Charmander":"Fire"
}
},
"OtherKey":"OtherValue",
"OtherKey2":"OtherValue2"
}
It works perfectly! My problem is to create the reverse process, with JSonDecode.
Hashtable<?, ?> hashUnknown = JSonDecode(jsonStringExample);
public Hashtable<?, ?> JSonDecode(String data) {
// I do not know how to parse json in Hashtable, without indicating the tags manually.
}
I do not know how to parse json in Hashtable, without indicating the tags manually.
That is, without it:
JSONArray menuObject = new JSONArray (jObject.getString ("Color"));
JSONArray menuObject = new JSONArray (jObject.getString ("Countries"));
This should be dynamic without knowing json content without writing manually Color, Countries, ....
Any ideas or advice? Thanks,
You can get an Iterator object (java.util.Iterator) over the keys of your JSONObject (jObject)
So you can write something like this:
Iterator<String> it = jObject.keys();
String key = null;
Object value = null;
while (it.hasNext()) {
key = it.next();
value = jObject.get(key);
// Then test the instance of the value variable
// and perform some logic
}