Facing issue with AsyncTask in Android - android

I am facing the following issue.
String[] pkg_id = new String[]{package_id};
Log.d("pkg_id","=>"+Arrays.toString(pkg_id));
HashMap<String,String> data_pkg_act = new HashMap<>();
for(int k=0;k<pk_id.length;k++)
{
Log.d("pkg_id","==>"+package_id);
//Here am getting pkg_id==>1 and pkg_id==> 2
try {
data_pkg_act.put("package_id", package_id);
new GetPackageDetails(data_pkg_act).execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
HashMap is overriding the 1st value with 2nd I wanted to make a call twice based on package_id. AsyncTask calling twice but with package id 2 am not able to call it for package id 1

this is just because, HashMap can not have same keys twice. as it works by key, value pair. you will need to use separate keys for both entries to make it work.

you are using same key 'package_id' for two different package ids

Related

Iterate through Java jsonObjects without array included

My problem is that this will create 3 new instances of DailyJobObjects with the same values as object number one (01, Bill, 50). And it's logical that it would do so, so how can I iterate through my jsonObject so I can separate the three objects? I have looked this up tirelessly but everything thing I have seen has and array included in the jsonData which would make things easier but this response Body is coming straight from a database - no arrays, just back to back objects. Iterating only gives me keys which I already did in a separate method to give me one half of my map. Now I need the values. You don't have to give me an answer, you can (I rather) point to something I'm missing. Thanks!
{"id":"01","name":"Bill","salary":"50"},
{"id":"02","name":"James","salary":"60"},
{"id":"03","name":"Ethan","salary":"70"}
JSONObject fields = new JSONObject(jsonData);
mObjectArray = new DailyJobObjectArray[fields.length()];
for(int i=0; i< fields.length(); i++) {
DailyJobObject mObject = new DailyJobObject();
mObject.setName(fields.getString("name"));
mObject.setSalary(fields.getString("salary"));
mObjectArray[i] = mObject;
}
return mObjectArray;
As #Selvin has mentioned, your json is not valid. Either get proper json from the database or parse it in a non-standard way. I would suggest getting a proper json array from the DB.
String[] splitString = jsondata.split("[^a-zA-Z \\{\\}]+(?![^\\{]*\\})");
for ( String s : splitString) {
try {
JSONObject field = new JSONObject(s);
String name = field.getString("name");
String id = field.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
}
I also agree that your mObject(...) does not make sense at all
Maybe you're looking for something like this
mObject.setName(name)

Unsupported operation exception when adding or clearing elemetings of list

I have made a function in which i first add contacts one my one.
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Contact contact = new Contact();
contact.setUserId(jsonObject.getString(ResponseParams.USER_ID));
contact.setPhoneNumber(jsonObject.getString(ResponseParams.PHONE_NUMBER));
contact.setUserName(jsonObject.getString(ResponseParams.USER_NAME));
((TazligenApp) activity.getApplication()).getTazligenContacts().add(contact);
} catch (JSONException e) {
e.printStackTrace();
}
but on the line:
((TazligenApp) activity.getApplication()).getTazligenContacts().add(contact);
i get unsupported operation exception and this obviously happens when i try to refresh contacts second time , Now i kinda know the reason but i don't know hwo to solve it.
P.S TazligenApp is my application class in android having some variables that i need throughout the cycle
Well i found the answer myself , but i am posting my solution so it might help someone :)
Well i just created a temp list . Added my items in that list and then just put it equal to my other list like
forloop(){
tempList.add(item);
}
mainList = tempList;
and it worked

Android Parse finding a result using a Pointer Id

I'm trying to get a query using as condition the Pointer Id of the object, for example I'm saving all the id's of an object in an array, then I want to get from another class (related by pointer), all the objects that uses that Id, so I already have this:
for (i = 0; i < num; i++) {
//restaurant.setObjectId(restId[i]);
ParseQuery<ParseObject> resultsitems = ParseQuery.getQuery("Item").whereEqualTo ("restaurant", restId[i]);
try {
objects=resultsitems.find();
} catch (ParseException e) {
e.printStackTrace();
}
(.......)
}
In my first try I tried to set the id into the restaurant object, then tried to use the query as:
ParseQuery<ParseObject> resultsitems = ParseQuery.getQuery("Item").whereEqualTo ("restaurant",restaurant );
But it didn't work, then I tried to search as shown in the code above, it doesn't crash but brings me nothing, how can I do this?
This is what really worked for me:
ParseObject obj = ParseObject.creatWithoutData("classNameThatPointedTo","fieldValue");
query.whereEqualTo("fieldName", obj);
Use: .findInBackground(new FindCallBack<Item>... (this will auto-complete in Android Studio), then put objects=resultsitems.find(); in the curly braces of the done() function.
The callback waits for the query to return before moving on with the script. Otherwise, the main thread will keep moving on without waiting for the data to come back from the server.

Parse sees only first element from array in Android

I am using Parse.com as my backend, and I want to download data from server. I have tags which filter these data. Unfortunately it works wrong. Lets say I have two tags "city1" and "city2", now I only get data for "city1".
public ArrayList<Dataset> getDatasetFromServer(Context context) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Dataset");
List<String> cities = DatabaseAdapter.getCityNames(context);
//cities list contains "city1" and "city2"
query.whereContainedIn("cities", Arrays.asList(cities.toArray(new String[cities.size()])));
ArrayList<Dataset> dataset = new ArrayList<>();
try {
List<ParseObject> parseDataset = query.find();
dataset = setDatasetList(parseDataset);
} catch (ParseException e) {
e.printStackTrace();
}
return dataset;
}
The problem is with this : Arrays.asList(cities.toArray(new String[cities.size()]).
Don't know why, but this convertion works wrong with Parse.
However, if I change above line to this
String[] array = {"city1", "city2"};
query.whereContainedIn("cities", Arrays.asList(array));
Everything works fine and I get data for city1 and city2.
My question is, what's the difference between these two solutions and how to fix this so the first solution works?
EDIT :
This also doesn't work :
query.whereContainedIn("cities", DatabaseAdapter.getCityNames(context));
getCityNames returns List<String>
Instead of converting your list to an array and then back to a list, just do this:
query.whereContainedIn("cities", cities);
The problem was my fault. Both solutions work good.
The reason it didn't worked was that my method
DatabaseAdapter.getCityNames(context) put a whitespace on the beggining of second element which I didn't saw.

How to add hash containing list in multipart entity?

I need my HTTP params should be
Parameters: {"MainHash"=>{"MyArray"=>[one,two,three]}}
For that I have tried
Try (1)
for (String item : array_items) {
entity.addPart("MainHash[MyArray[]]", new StringBody(item));
}
Result
Parameters: {"MainHash"=>{"MyArray"=>[nil,nil,nil]}} //nil for each item
Try (2)
entity.addPart("MainHash[MyArray[]]", new StringBody("["+items_string+"]"));
Result
Parameters: {"MainHash"=>{"MyArray"=>"[one,two,three]"}} //quotes added to array
When I tried it with list with out hash it is working.
But I need to append list inside hash.
Any suggestions on the same are highly appreciated.
I believe you want your MainHash with same key in MainArray.
So, you can try,
for (String item : array_items) {
entity.addPart("MainHash[MyArray][]", new StringBody(item));
}
It will create,
[one,two,three]
as an array.

Categories

Resources