Retrieve data from a class in Parse.com - android

I have a class in Parse.com, as XYZ, and it has columns email, password, phone, age. I have email and password combination and I want to check whether there is such a combination in the class? How do I do this?
I have a class Donate, and I have been given an email(e) and password(pw). Here goes the code:
`ParseQuery<ParseObject> query = ParseQuery.getQuery("donate");
query.whereEqualTo("email", e);
query.whereEqualTo("password", pw);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Success!!!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Something went wrong!!!", Toast.LENGTH_LONG).show();
}
}
}); `
And this always shows 'success' irrespective of the strings e, and pw.

ParseQuery return ParseException just if there was technical issue calling the server.
else, even though your query doesn't find exactly what you looked for,
it will return an empty list and the 'e' var will be null.
so what you need to do is :
ParseQuery<ParseObject> query = ParseQuery.getQuery("donate");
query.whereEqualTo("email", e);
query.whereEqualTo("password", pw);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
if (list.isEmpty() == false) { // add this
Toast.makeText(getApplicationContext(), "Success!!!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Worng password!!!", Toast.LENGTH_LONG).show(); // add this
}
} else {
Toast.makeText(getApplicationContext(), "Something went wrong!!!", Toast.LENGTH_LONG).show();
}
}
});

Related

Parse Frame Work Update Query does not work

I am using parse frame work in android, I am updating user object in that but I update that old data is replaced with new data here my code is . How to resolve it any one help me solve out it.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", currentUser.getObjectId());
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
currentUser.put("forbiddenWords", wordArray);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
hideProgressDialog();
if (e == null) {
Snackbar.make(parentView, R.string.string_setting_chnaged, Snackbar.LENGTH_LONG).show();
} else {
e.printStackTrace();
}
}
});
}
});
I guess you wanna update some value of your current user. Try this code
ParseUser user = ParseUser.getCurrentUser();
user.put("forbiddenWords", wordArray);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
hideProgressDialog();
if (e == null) {
Snackbar.make(parentView, R.string.string_setting_chnaged, Snackbar.LENGTH_LONG).show();
} else {
e.printStackTrace();
}
}
});

android parse, how to find other username?

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "female");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, com.parse.ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Username Found",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Username Not Found",
Toast.LENGTH_LONG).show();
}
}
});'
I have problem with these code, i want to search other username, please help me.
I already write the code but always get in "Username found" but in database there no name like that.
You are wrong because, into findInBackground callback, you currently check only the e variable (if it is null or not) and not the objects size. Exception is alwasy null if the query return a valid result from Cloud Code (in this case the object is not null and it size is 0 or greather than); exception variable is valorized only if the execution of query raise an exception/error.
To check if a query returns a records or not you must to check the size of objects (List):
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "female");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, com.parse.ParseException e) {
if (e == null) {
if (objects.size() > 0) {
Toast.makeText(getApplicationContext(), "Username Found",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Username Not Found",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "An error has occurred!",
Toast.LENGTH_LONG).show();
}
}
});
Take in consideration the opportunity to limit the query to 1 result (if you looking for one specific record) or use getFirstInBackground method to improve query performance.

How to check if Username/Email already exists android parse

I've searched everywhere but haven't found anything about how to check if a username/email address in the parse database is taken. I've found some answers on the internet on parse.com forums but they weren't clear.
Thanks if you could help.
If this has an answer somewhere, then please comment instead of marking so I can delete it.
I think this will do what you need if I understand your question correctly:
final ParseQuery<ParseUser> emailQuery = ParseUser.getQuery();
emailQuery.whereEqualTo("email", emailAddress);
final ParseQuery<ParseUser> usernameQuery = ParseUser.getQuery();
usernameQuery.whereEqualTo("email", username);
List<ParseQuery> queries = new ArrayList<>();
queries.add(emailQuery);
queries.add(usernameQuery);
final ParseQuery<ParseUser> query = ParseQuery.or(queries);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> results, ParseException e) {
// results has the list of users that match either the email address or username
}
});
https://www.parse.com/docs/android/guide#queries-compound-queries
Or you could do it this way:
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// yippee!!
} else {
switch (e.getCode()) {
case ParseException.USERNAME_TAKEN: {
// report error
break;
}
case ParseException.EMAIL_TAKEN: {
// report error
break;
}
default: {
// Something else went wrong
}
}
}
}
});
Try this to check your data in parse. I hope this will helpful for you.
// this is create query of your parse table
ParseQuery<ParseObject> query = new ParseQuery<>(your prars tableName);
query.whereEqualTo(YourParseColumeNameEmail, yourEmail);
query.whereEqualTo(YourParseColumeNamePassword, yourPassword);
// this is for doing in background
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
if (scoreList.size() == 0) {
// if there is no data like your email and password then it,s come here
} else {
// if there is data like your email and password then it,s come here
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});

parse.com update data android

I have an object at parse.com in the class Grillen with data.
Now I want to make it such that if I click a button it adds to the row with the same name as selected in a spinner, and it updates the columns Betrag and Rechnung. I tried the following code but it doesn't work.
public void createRechnung(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen")
// Retrieve the object by id
query.getInBackground(String.valueOf(mNameInput), new GetCallback<ParseObject>() {
public void done(ParseObject Grillen, ParseException e) {
if (e == null) {
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground();
}
}
});
}
Use this code to check what's going wrong
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
// Retrieve the object by id
query.getInBackground(String.valueOf(mNameInput), new GetCallback<ParseObject>() {
public void done(ParseObject Grillen, ParseException eg) {
if (eg == null) {
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
If you successfully save, then your exception is null, if it didn't you will know why via stacktrace. I suggest running this code and letting the execution flow of the app be based on whether or not the save was successful or not.
Read this: getInBackground(String, callback) You must pass the objectId of the object you are trying to get, rather than the name. Make sure you are getting by ObjectID and not name as it appears you are doing. ObjectId looks like a random string and should be found in your database by looking online on Parse
Do this instead:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
query.whereEqualTo("name", String.valueOf(mNameInput));
// Retrieve the object by id
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException eg) {
if (eg == null && objects.size() > 0) {
ParseObject Grillen = objects.get(0);
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
Note: I have no idea what the column name is for the 'name' attribute of Grillen objects, so you might need to change this to whatever you call the name column for that object.
Okay i have get the Spinner to work for the name but not the EditText
here is some part of the codes
public class RechnungActivity extends AppCompatActivity {
private EditText mBetragInput;
private Spinner mNameInput;
private String mName;
private String mBetrag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechnung);
ParseObject.registerSubclass(Grillen.class);
mBetragInput = (EditText) findViewById(R.id.Sonstiges);
mNameInput = (Spinner) findViewById(R.id.Name);
mName = mNameInput.getSelectedItem().toString();
mBetrag = mBetragInput.getText().toString();
}
public void createRechnung(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
query.whereEqualTo("name", mName);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException eg) {
if (eg == null && objects.size() > 0) {
mBetrag = mBetragInput.getText().toString();
ParseObject Grillen = objects.get(0);
Grillen.put("Betrag", mBetrag);
Grillen.put("Rechnung", true);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
}

cannnot save the result of parse query in an android variabe

I am currently using Parse.com's API for an android application. I want to retreive an array from parse and save it as a list to work with later. but tha problem is when i'm out of the query my list turnes empty. can u help me?
ParseUser currentUser = ParseUser.getCurrentUser();
String userId= currentUser.getObjectId();
final ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("Friendship");
pQuery.whereEqualTo("user", userId);
pQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("friendsList")!=null) {
list11 = p.getList("friendsList");
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+ list11.get(2).toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
});
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+list11.get(1).toString(),
Toast.LENGTH_LONG).show();
the first Toast is working but the second one gives me a NullPointerException.
You can only put second Toast in done method like this:-
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("friendsList")!=null) {
list11 = p.getList("friendsList");
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+ list11.get(2).toString(),
Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+list11.get(1).toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
Because findInBackground method start a new background thread.when you show second toast then list11 may be empty. Due to this it throws a NullPointerException

Categories

Resources