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.
Related
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
Hitting a brick wall in my code at the moment for fetching json objects from multiple pages(using a loop) in a AsyncTask. It reaches the last page, but getting the correct if statement to ensure that the loop DOESN'T run again and continues on is baffling me.
String data = //some correct json data with next element that holds a uri parseable string
JSONObject initial = new JSONObject(data);
String next = initial.getString(nextObjSTR);
//gonna start from the "last" page and recursively return to the 1st page
if(*The if condition I need help with*) {
//there is another page
makeConnection(Uri.parse(next));
}
Basically, the last page of json elements has a next element with a null or no element value, which triggers the IOException error caught in makeConnection method because my initial if statement has always been failing.
Can I get a reason or help as to the appropriate if check for Strings from json? I've tried String != null as NullPointerExceptions occur if I use any method from String to compare. Likewise, JsonObject.NULL comparison doesn't work for me either.
None of the other answers worked, and I ended up questioning whether the element was really null despite looking at the parsed json data via an online tool. In the end, JSONObject.IsNull(element mapping name) is the right approach.
If you're sure that the value is either null (empty) or a correct URI, and assuming that the nextObjSTR key is always present in the data JSON, then that will do:
if (next != null && !next.trim().isEmpty()) {
makeConnection(Uri.parse(next));
}
Or, since you're on Android, it's better use the more convenient method:
if (!TextUtils.isEmpty(next)) {
makeConnection(Uri.parse(next));
}
You can use the optString Method of the JSONObject. If the JSON key is not this method will return a empty string, so you can check it easily:
String next = initial.optString(nextObjSTR);
if ( ! next.isEmpty() ) {
makeConnection(Uri.parse(next));
}
Source: https://developer.android.com/reference/org/json/JSONObject.html#optString(java.lang.String)
you must check value with key is has in json object.
Try below code:
JSONObject initial = new JSONObject(data);
if(initial.has(nextObjSTR)) {
String next = initial.getString(nextObjSTR);
if (next != null && !next.isEmpty()) {
makeConnection(Uri.parse(next));
}
}
I do like this...
String value;
if(jsonObject.get("name").toString.equals("null")){
value = "";
else{
value = jsonObject.getString("name");
}
I'm using for first time sugar and all seems that work fine. I can save the data and seems that I'm getting the data when I try to do a find. My problem is that I'm getting the object and not the value that I have store and I'm not really sure why, because I'm doing the same that I can see in the official documentation
This is that I'm doing to get the data:
Select name= Select.from(MyClass.class).where(Condition.prop("name").lt("Bob"));
String data = name.toString();
Log.e("aaaaa", data.toString());
This is that I'm gettin in the log:
com.orm.query.Select#3ce7ff7b
With that Select statement, you are querying for the MyClass object by name, and that returns you a Select object.
To get the name from it you should fetch results from the Select (here I do it with .first()) and put that in a MyClass object. Then you can get the name from that.
MyClass myClass = Select.from(MyClass.class)
.where(Condition.prop("name").lt("Bob"))
.first();
String name = myClass.getName();
provided your MyClass has a method getName()
I'm doing the same that I can see in the official documentation
I don't thnk you are.
In the documentation here, it shows this piece of code:
Select.from(TestRecord.class)
.where(Condition.prop("test").eq("satya"),
Condition.prop("prop").eq(2))
.list();
When you compare it with your code, you see that you missed the call to list.
If you don't call list, the where method will return a Select object. And that's all you get.
list will return all of the results that the query has found. So the correct way to do this would be:
List<MyClass> results =
Select.from(MyClass.class).
where(Condition.prop("name").
lt("Bob")).list();
for (MyClass obj: results) {
Log.i("aaaa", obj.toString());
// or you can write
// Log.i("aaaa", obj.name);
}
The above would print all the results. If you only need the first result, you can call results.get(0).
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.
I have 2 pointer objects(pointing to ParseUser) in my table "Attack".It seems these pointer objects take a while to get retrieved.Hence my code directly didnt work and gave me the exception:
java.lang.IllegalStateException: ParseObject has no data for this key. Call fetchIfNeeded() to get the data.
I then did the needful surrounding the fetchIfNeeded function with a try-catch block:
ParseObject battle = null;
try {
battle = objects.get(0).fetchIfNeeded();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ParseUser attacker1 = battle.getParseUser("Attacker");
Log.i("dontest",attacker1.getUsername());
It is still returning the same.I even checked with isDataAvailable function and it returned true.Any way around this?
P.S.: My query returns exactly 1 row which I checked with the size() function.
Here's the documentation describing fetchIfNeeded() function.
Use Your query like this..
ParseQuery<ParseObject> pQueryFromAttack = new ParseQuery<ParseObject>("Attack");
// use include to fetch details while quering----increase loading speed by doing this.
pQueryFromAttack.include("pointer to _user table");
List<ParseObject> listObj = pQueryFromAttack.find();
for eg:
String userName =listObj.get(0).fetchIfNeeded().getString("name");
Use it like this...