I'm using Java and Infusionsofts API to get a list of contacts based on the customer id. I can't figure out a way to do this. I'm using Googles Guava to use multimap but it's producing an error:
org.apache.xmlrpc.common.XmlRpcExtensionException: Serializable objects aren't supported, if isEnabledForExtensions() == false
So now i'm trying hashmap and i'm inserting "Id" as the key and the customer id as the value but there's always one entry in the hashmap.
How can I add to the parameters variable a map that contains:
["Id",11111]
["Id",22322]
["Id",44444]
List parameters = new ArrayList();
parameters.add(APP_ID);
parameters.add(TABLE_NAME);
parameters.add(LIMIT);
parameters.add(pageNumber);
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(int customerId : customerIds){
map.put("Id", customerId);
}
//PROBLEM IS HERE
parameters.add(map);
//THIS IS THE PROBLEM, I NEED TO ADD ["Id", customerId] multiple
//times with the customerId being different but since there's a hashmap
//There's always 1 entry in the map
String[] fields = {"Email","FirstName"};
parameters.add(fields);
Object[] contacts = null;
try{
contacts = ( Object [] ) client.execute("DataService.query",parameters);
}catch(XmlRpcException e){
e.printStackTrace();
}
for (int i = 0; i < contacts.length; i++) {
Map contact = (Map) contacts[i];
System.out.println(contact+"\n\n");
}
Related
Code is:
How can to add the Map to the List
public List<Map<Object, Object>> getReportees(String idOfEmp) throws Exception {
JSONArray jsonarr_s = (JSONArray) jobj.get("list");
Map<Object, Object> map = new HashMap<Object, Object>();
if (jsonarr_s.size() > 0) {
// Get data for List array
for (int i = 0; i < jsonarr_s.size(); i++) {
JSONObject jsonobj_1 = (JSONObject) jsonarr_s.get(i);
JSONObject jive = (JSONObject) jsonobj_1.get("jive");
Object names = jsonobj_1.get("displayName");
Object userid = jive.get("username");
map.put(names, userid); //return the map with the key value pairs
map = new HashMap<Object, Object>();
String UserId = userid.toString();
String output1 = resp1.getEntity(String.class);
JSONObject jobjs = (JSONObject) new JSONParser().parse(output1);
// Store the JSON object in JSON array as objects (For level 1 array element i.e
// issues)
JSONArray jsonarr_new = (JSONArray) jobjs.get("issues");
int numofjiras = jsonarr_new.size(); //this jira count must be mapped to the name and id
map.put("count", numofjiras);
}
return map;
} else {
map.put("errorcheck", msg);
}
return map;
}
}
I want the output like:
Name id count
AJ 235457 2
Geet 637571 0
Actually I am getting the Name and id in key value pairs.Then I am trying to pass each id to an api which will give me the count.So how can I return all the fileds i.e Name ,id and count.So here I am trying to map like for this Userid and Name this is the count.How can we acheive it.Plesae help.Thanks in advnce.
I think you can try creating a new class to represent each row in your output. For example, you can create an Employee class like this:
public class Employee {
private long id;
private String name;
private int issueCount;
//getters and setters
}
You can, then, use this class and assign the values from the JSONArray arrays to it. Once you get the value for "count", you can just add the Employee object to the map (or list).
Hope this helps.
You would need to declare a local list first and then return that list:
public List<Map<Object, Object>> getReportees(String idOfEmp) throws Exception {
JSONArray jsonarr_s = (JSONArray) jobj.get("list");
Map<Object, Object> map = new HashMap<Object, Object>();
List<Map<Object, Object>> resultList = new ArrayList<Map<Object,Object>>();
if (jsonarr_s.size() > 0) {
// Get data for List array
for (int i = 0; i < jsonarr_s.size(); i++) {
JSONObject jsonobj_1 = (JSONObject) jsonarr_s.get(i);
JSONObject jive = (JSONObject) jsonobj_1.get("jive");
Object names = jsonobj_1.get("displayName");
Object userid = jive.get("username");
map.put(names, userid); //return the map with the key value pairs
String UserId = userid.toString();
String output1 = resp1.getEntity(String.class);
JSONObject jobjs = (JSONObject) new JSONParser().parse(output1);
// Store the JSON object in JSON array as objects (For level 1 array element i.e
// issues)
JSONArray jsonarr_new = (JSONArray) jobjs.get("issues");
int numofjiras = jsonarr_new.size(); //this jira count must be mapped to the name and id
map.put("count", numofjiras);
}
resultList.add(map);
} else {
map.put("errorcheck", msg);
resultList.add(map);
}
return resultList;
}
Based on your results though you should consider flipping your data objects to instead be
Map<Object, List<Object>>
where the first Object in the map which is they key the name and then the list would contain two objects [id, count].
Am getting response from Elasticsearch with duplicates, to avoid that i used Hashmap implementation and i put all the values into the HashMap object.
After that am iterating over the HashMap object to convert into JSONArray.
Am geting one unique record from distinctObjects (HashMap Object). But after if convert into JSONArray., the length of JSONArray shows 2 it suppose to be 1 and am printing the JSONArray, it shows like below.
JSONArray --->[{"code":"VA1125-GGA-1","id":"code"},{"code":"12816","id":"id"}]
Expected Result should be :
JSONArray --->[{"code":"VA1125-GGA-1","id":"12816"}]
Please find my code below.
JSONObject responseObj;
JSONArray responseArray = new JSONArray();
Map<String, Object> distinctObjects = null;
SearchHit[] searchHits2 = searchResponse2.getHits().getHits();
for (SearchHit hit2 : searchHits2) {
Map<String, Object> sourceAsMap2 = hit2.getSourceAsMap();
distinctObjects = new HashMap<String, Object>();
distinctObjects.put("id", sourceAsMap2.get("id").toString());
distinctObjects.put("code", sourceAsMap2.get("code").toString());
}
Iterator it = distinctObjects.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
responseObj = new JSONObject();
responseObj.put("id", pair.getKey());
responseObj.put("code", pair.getValue());
responseArray.put(responseObj);
it.remove(); // avoids a ConcurrentModificationException
}
System.out.println("Link ID List Size --->"+responseArray.length());
System.out.println("JSONArray --->"+responseArray.toString());
It looks like you're adding both code and id as top level entries to your distinctObjects map which is why you're getting two objects back. Assuming you want to de-dup based on ID your first loop should look something like:
for (SearchHit hit2 : searchHits2) {
Map<String, Object> sourceAsMap2 = hit2.getSourceAsMap();
distinctObjects = new HashMap<String, Object>();
distinctObjects.put(sourceAsMap2.get("id"), sourceAsMap2.get("code").toString());
}
That will give you one entry in distinctObjects for every unique id with a value of the code.
If you wanted you could also add sourceAsMap2 as the value in distinctObjects to maintain the full response if you need more than just the code in downstream processing.
I have this arraylist and another for hashmap
ArrayList<HashMap<String, String>> agentList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> agentproperty = new HashMap<String, String>();
I have received jArray from php file. and trying to put it in the array using hashmap.
JSONArray jArray = jsonObj.getJSONArray(TAG_AGENT);
String id = null;
String name = null;
for (int i = 0; i < jArray.length(); i++) {
JSONObject a = jArray.getJSONObject(i);
id = a.getString(TAG_AGENTID);
name = a.getString(TAG_NAME);
agentproperty.put("AGENTID", id);
agentproperty.put("NAME", name);
Log.d("id",id);
Log.d("name",name);
agentList.add(agentproperty);
}
Log.d("firstperson",String.valueOf(agentList.get(0)));
Log.d("secondperson",String.valueOf(agentList.get(1)));
I can get
id 1, hame cathy; id 2, name john
for the log inside loop. But from the last log, I am getting
id 2, name john; id 2, name john
It seems the value of agentproperty is changed even after adding in arraylist.
I tried taking the agentList.add(agentproperty); outside of loop. Didnt work. Only inserts the last value (id 2, name john). Any idea how can i populate this array with all the values I get from loop. I will need to use agentList in listview.
yes it is. You have to create a new instance of the HashMap at every iteration, otherwise you will override the value for a given key, if an entry already exists
Move
HashMap<String, String> agentproperty = new HashMap<String, String>();
inside the for loop
I use following complex data structure.
departures = new TreeMap<String, Map<String, Set<MyObject>>>();
arrivals=new HashMap<String, Set<MyObject>>();
flights=new HashSet<MyObject>();
Then I use loops (I also tried other loops).
for(String dep: nizDep){
for(String arr: nizArr){
for(MyObject flight: _flights){
if(flight.getFrom().equalsIgnoreCase(dep)&&flight.getTo().equalsIgnoreCase(arr)){
flights.add(flight);
}
}
if(!flights.isEmpty()){
arrivals.put(arr, flights);
flights.clear();
}
}
if(!arrivals.isEmpty()){
departures.put(dep, arrivals);
arrivals.clear();
}
}
System.out.println(departures.size()); //result 14
System.out.println(departures.containsKey("Madrid")); //result true
arrivals=departures.get("Madrid");
System.out.println(arrivals.size()); //result 0, arrivals is empty. WHY?
My question is how to use this complex data structure and how to retrieve arrivals from departures?
System.out.println(arrivals.size()); //result 0, arrivals is empty. WHY?
BECAUSE When you call flights.clear(); after arrivals.put(arr, flights); or arrivals.clear(); after departures.put(dep, arrivals);, this clears your original objects(flights and arrivals). Please bring your initialization statements i.e.
Map<String, Set<MyObject>> arrivals=new HashMap<String, Set<MyObject>>();
Set<MyObject>(); flights=new HashSet<MyObject>();
within the for loops or replace that statement as below:
if(!flights.isEmpty()){
Set<MyObject> newflights=new HashSet<MyObject>();
newflights.addAll(flights); //copy elements to new set
arrivals.put(arr, newflights);
flights.clear();
}
Same you may do with departures.
Now for retrievals:
Set<String> arrivalKeys = departures.keySet();
Interator<String> arrIter = arrivalKeys.iterator();
while(arrIter.hasNext()){
String arrKey = arrIter.next();
Map<String, Set<MyObject>> arrivals = departures.get(arrKey );
//use your arrivals map object
}
Same you can do to retrieve flights from arrivals e.g.
for each arrivals retrieved as above:
Set<String> flightKeys = arrivals.keySet();
Interator<String> flIter = flightKeys.iterator();
while(flIter.hasNext()){
String flKey = flIter.next();
Set<MyObject> flights = arrivals.get(flKey );
//use your flights set object
}
arrivals=new HashMap<String, Set<MyObject>>();
departures = new TreeMap<String, Map<String, Set<MyObject>>>();
for(String dep: nizDep){
for(String arr: nizArr){
for(MyObject flight: _flights){
if(flight.getFrom().equalsIgnoreCase(dep)&&flight.getTo().equalsIgnoreCase(arr)){
flights=new HashSet<MyObject>();
flights.add(flight);
arrivals.put(arr, flights);
departures.put(dep, arrivals);
}
}
}
}
System.out.println(departures.size()); //result 14
if(departures.containsKey("Madrid")) {
arrivals=departures.get("Madrid");
System.out.println(arrivals.size());
}
In case you want to keep a one-to-one mapping between arrivals and flights, then this code works. In case you want to keep a global structure of maintaining the set of flights then you'll have to create another global gflights object and put every flights object into it.
I am new in Android development, and I am trying to receive a HashMap in RESULT by using XMLRPC but every time it's crash the application, this is my code please advice me :
Object RESULT = XMLRPCClient.callEx(methodname,new Object[] {params});
Map FRESULT= (Map) RESULT;
I have been dealing with this also and managed to get the values this way:
try {
Object[] answer = (Object[]) client.call("call", sessionId, method, params);
HashMap map = (HashMap) answer[0]; // get first item of the response because in my case the response was an array of Objects with one item in it holding the HashMap
Object[] records = (Object[]) map.get("records"); // I only needed values from "records" key
for (int i = 0; i < records.length; i++) {
HashMap record = (HashMap) records[i]; // create another map from the records values, in my case uid's of categories
Category cat = new Category(); // creating new instance of my Category class
cat.setCatUid((String) record.get("uid")); // calling a method of the Category class to set Uid to the value from record HashMap
m_categories.add(cat); // this adds it to my ArrayList<Category>
}
} catch (XMLRPCException e) {
Log.e(method, "Exception", e);
}
I'm sure it's a mess, I'm noob in Java myself, but it worked for me. Hope it helps :)
Now the Application pass this peacefully after implementing :
Object RESULT = XmlRpcConnect.ServerCall_a(method,new Object[] {params});
Map<String, Object> FRESULT= (HashMap<String, Object>) RESULT;
with some changes in my XmlRpcConnect Class:
#SuppressWarnings("unchecked");
public static Object ServerCall_a(String method, Object[] params){
XMLRPCClient client = new XMLRPCClient(server);
HashMap<String, Object> result=null;
try{
result = (HashMap<String, Object>) client.callEx(method, params);
}
catch(XMLRPCFault f){
// result = ("Fault message: " + f.getMessage());
}
catch(XMLRPCException e){
// result = ("Exception message: " + e.getMessage());
}
return result;
}
but when trying to extract the values it's crash again , any advice :
if (FRESULT.get("status") == null) {
result = (String) FRESULT.get("status");
toastDialog(result);
}