I'm trying to read from parseObject. I have a class over there and I'm trying to get the messages from there but I get only the last message that is sent from the device.
So if I send two messages only the last one is sent.
Can anyone help me?
This is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
//happend = (boolean) smsObject.get("happend");
result = time;
}
} else {
}
}
});
if (System.currentTimeMillis() >= result.getTime()&& happend == false) {
// count++;
sendMsg2(myMsg, usrNum);
happend = true;
}
Use this
for (ParseObject smsObject : objects) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
//happend = (boolean) smsObject.get("happend");
result = time;
if (System.currentTimeMillis() >= result.getTime()&& happend == false) {
// count++;
sendMsg2(myMsg, usrNum);
happend = true;
}
}
I hope this will solve your problem
It seems that myMsg and usrNum are not an Array or a List.
You only call sendMsg2(myMsg, usrNum) once, after the loop has ended. That is why you only get the last message.
Move sendMsg2(myMsg, usrNum) into the loop.
The problem seems to be that you have located your sendMsg2() outside of both - the callback scope and the loop scope. If you want to have all of your messages being sent you have to put sendMsg2() inside of the loop.
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
//happend = (boolean) smsObject.get("happend");
result = time;
if (System.currentTimeMillis() >= result.getTime()&& happend == false) {
// count++;
sendMsg2(myMsg, usrNum);
happend = true;
}
}
} else {
}
}
});
Related
public void pictureQuery() {
friendsPic = new ArrayList<>();
for (int i = 0; i < friendsList.length(); i++) {
ParseQuery parseQuery = ParseQuery.getQuery("Data");
parseQuery.whereEqualTo("firstName", "John");
//parseQuery.whereEqualTo("userId",friendsID.get(i));
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
String link = objects.get(0).get("picUrl").toString();
Bitmap bitmap = null;
try {
bitmap = task.execute(link).get();
friendsPic.add(bitmap);
} catch (Exception e1) {
e1.printStackTrace();
}
Log.i("FDA, done", friendsPic.toString());
} else {
Log.i("FD", "0002");
}
} else {
e.printStackTrace();
}
}
});
}
}
Hi guys,
I am trying to retrieve picture URL from parse server (stored as a string), originally I was trying to make a query using the Id, but also tried to hard code it using:
parseQuery.whereEqualTo("firstName", "John");
It always seems to go to the else statement of object.size() > 0 i.e.
Log.i("FD", "0002");
I think i've checked everything including:
size of friendsList ( for the for loop )
spelling, made sure that "Data", "firstName" and "John" are the same on Parse Server
The value of friendsID.get(i)
Tried to use findFirstInBackground instead (same issue)
I have also compared it with my other code for different data ( that works ) :
ParseQuery parseQuery = ParseQuery.getQuery("SportData");
parseQuery.whereEqualTo("id", mApp.getId());
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
saveSportData(objects.get(0), false);
} else if (objects.size() < 1) {
saveSportData(null, true);
}
} else {
e.printStackTrace();
}
}
});
Any idea what am I doing wrong, or what may be causing it ?
Thanks a lot.
Method checkMobileTableAccount uses to check a phone number in table Account, strMobile is phone number to search.
public boolean checkMobileTableAccount(final String strMobile) {
result = false;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Account");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject user : objects) {
String mobile = user.getString("phonenumber");
if (strMobile.equals(mobile)) {
result = true;
break;
}
}
}
});
return result;
}
How can I get result after for loop finished?
Just dont use method findInBackground(). Use find() which is not making your request in background.
public boolean checkMobileTableAccount(final String strMobile) {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Account");
try {
List<ParseObject> objects = query.find();
for (ParseObject user : objects) {
String mobile = user.getString("phonenumber");
if (strMobile.equals(mobile)) {
return true;
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
Retrieved data and stored in local database but while trying to pull relation from the local database I get empty list which indicates that the relation data is not pinned to the local database.
public void retrieveThreadListStoreInDB()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Threads");
query.include("postedBy");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if(e == null)
{
// Query is successful now lets load data from parse
ParseObject.pinAllInBackground((List<ParseObject>) list, new SaveCallback() {
#Override
public void done(ParseException e) {
if(e == null)
{
if(!isFinishing())
{
// TODO : Notify to refresh data
}
}
else
{
Log.d("DEBUG","ERROR PINNING DATA WITH EXCEPTION : "+e);
}
}
});
}
}
});
So how should I pin "relation" data to the local database ???
What is the best way to read comments which is a relation. shall I store them in local datastore or pull data in the background ??
make sure you enableLocalDatastore store in application class Parse.enableLocalDatastore(getApplicationContext());
Parse local data store will not work with cache policy. We can access relational data like below
private void getParseData() {
if (parseObjectId != null) {
mApp.show_PDialog(context, "Loading..");
ParseQuery<ParseObject> parseQuery = new ParseQuery<>("Profile");
parseQuery.getInBackground(parseObjectId, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
try {
newName = parseObject.getString("name");
newGender = parseObject.getString("gender");
newDOB.setTime(parseObject.getDate("dob"));
newTOB.setTime(parseObject.getDate("tob"));
newCurrentLocation = parseObject.getParseObject("currentLocation");
newPOB = parseObject.getParseObject("placeOfBirth");
if (newName != null) {
displayName.setText(newName);
}
if (newGender != null) {
gender.setText(newGender);
}
if (newDOB != null) {
DateFormat df = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String subdateStr = df.format(newDOB.getTime());
datePicker.setText(subdateStr);
}
if (newTOB != null) {
DateFormat df = new SimpleDateFormat("hh:mm a", Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String subdateStr = df.format(newTOB.getTime());
timepicker.setText(subdateStr);
}
if (newPOB != null) {
placeOfBirth.setText(newPOB.fetchIfNeeded().getString("name")
+ ", " + newPOB.fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name") + ", " + newPOB.getParseObject("Parent").fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name"));
}
if (newCurrentLocation != null) {
currentLocation.setText(newCurrentLocation.fetchIfNeeded().getString("name")
+ ", " + newCurrentLocation.fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name") + ", " + newCurrentLocation.fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name"));
}
} catch (ParseException e1) {
e1.printStackTrace();
}
} else {
e.printStackTrace();
}
mApp.dialog.dismiss();
}
});
} else {
getActivity().finish();
}
}
I am trying to parse a boolean value after using it with parseObject, but I cannot parse it?
Here is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
if (smsObject!=null) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
happend = (boolean) smsObject.get("happend");
result = time;
}
if (result != null) {
if (System.currentTimeMillis() >= result.getTime() && happend == false) {
// count++;
if (usrNum != null && myMsg != null) {
Log.d("message", myMsg);
Log.d("time", String.valueOf(result));
sendMsg2(myMsg, usrNum);
smsObject.put("happend", true);
}
}
}
}
}
}
});
You should call default methods of ParseObject to retrieve data instead of parsing :
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
if (smsObject!=null) {
Date time = smsObject.getDate("date");
myMsg = smsObject.getString("message");
usrNum = smsObject.getString("phoneNumber");
happend = smsObject.getBoolean("happend");
result = time;
}
if (result != null) {
if (System.currentTimeMillis() >= result.getTime() && happend == false) {
// count++;
if (usrNum != null && myMsg != null) {
Log.d("message", myMsg);
Log.d("time", String.valueOf(result));
sendMsg2(myMsg, usrNum);
smsObject.put("happend", true);
}
}
}
}
}
}
});
You can simply use For loop for getting single object of Parse object and can use getBoolean() method for getting boolean value.
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
happend = objects.get(i).getBoolean("happend");
}
}
}
i have around 13000 records on one table(HashTag -classname) . i want to retrieve all of them on a single query. but parse allows only 1000 per query. any other ways get the all the records..
ParseQuery<ParseObject> query = ParseQuery.getQuery("HashTag");
query.whereExists("Tag"); query.orderByAscending("Type"); query.setLimit(1000);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list,
ParseException e) {
// TODO Auto-generated method stub
if (e == null)
{
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
ParseObject p = list.get(i);
String tagid = p.getString("Tag");
String Type = p.getString("Type");
class2 c2 = new class2();
c2.type = "" + Type;
c2.tag = "" + tagid;
listClass2.add(c2);
}
}
Sure, you can run multiple queries on the same table, with query's skip property incremented by 1000 each time:
Get the total number of records via query.count(), and use it to set a 'skip' variable
Run a new query for each 1000 records, updating your skip property accordingly
Process records as normal when each query returns
Something like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("HashTag");
query.whereExists("Tag");
query.countInBackground(new CountCallback() {
public void done(int count, ParseException e) {
if (e == null) {
// The count request succeeded. Run the query multiple times using the query count
int numQueries = Math.ceil(count / 1000); //Gives you how many queries to run
for(int skipNum = 0; l < numQueries; l++){
ParseQuery<ParseObject> query = ParseQuery.getQuery("HashTag");
query.whereExists("Tag"); query.orderByAscending("Type");
query.setLimit(skipNum * 1000);
query.findInBackground(new FindCallback<ParseObject>() {
//Run your query as normal here
}
}
} else {
// The request failed
}
}
//Declare a global variable for storing the complete data
private static List<ParseObject>allObjects;
allObjects=new ArrayList<ParseObject>();
ParseQuery<ParseObject>query3=ParseQuery.getQuery("HashTag");
query3.whereExists("Tag");
query3.setLimit(1000);
query3.findInBackground(getallobjects());
int limit=1000;
int skip=0;
//callback method:
private FindCallback<ParseObject>getallobjects(){
return new FindCallback<ParseObject>(){
#Override
public void done(List<ParseObject>list,ParseException e){
allObjects.addAll(list);
if(list.size()==limit){
skip=skip+limit;
ParseQuery<ParseObject>query=ParseQuery.getQuery("HashTag");
query.setSkip(skip);
query.setLimit(limit);
query.findInBackground(getallobjects());
}else{
//you have full data in allobjects
for(int i=0;i<allObjects.size();i++){}
}
}}}
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("TestObject");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
for(ParseObject p : list){
Log.d("--", (String) p.get("foo")+p.getCreatedAt());
}
}
});