I am trying to get a list of values from parse.com for a specific object, however i seem to be struggling with one error that keeps coming up on my screen which doesn't seem to be wrong, unless it is related to some other issue that i may have missed. i have initialised my aF variable as List aF; as you cannot see it in this class. the error i am getting is on aF = query.find() where it says unhandled exceptions,
protected Void doInBackground(Void... params) {
// Locate the class table named "UploadedFiles" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("UploadedFiles");
query.orderByDescending("_created_at");
try {
Toast.makeText(getActivity(), "Success",
Toast.LENGTH_LONG).show();
aF = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
I also tried to implement an if statement exception rule, but it also gives me errors in which i thought i shouldn't use it, as i cannot implement a return statement with this case.
public void done(ParseException e) {
if (e == null) {
aF = query.find()
Toast.makeText(getActivity(), "Success",
Toast.LENGTH_LONG).show();
} else {
Log.d("Error", e.toString());
}
}
});
EDIT
protected Void doInBackground(Void... params) {
// Locate the class table named "UploadedFiles" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("UploadedFiles");
query.orderByDescending("_created_at");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
try {
aF = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}
});
return null;
}
Thank you for all answers, I am getting now a new error saying that variable query is accessed from within inner class, needs to be declared final. does that mean i have to set it as a return statement ?
You have use like this.
Use callback FindCallback
Retrieves a list of ParseObjects that satisfy this query from the
source in a background thread.
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> arg0, ParseException arg1) {
// TODO Auto-generated method stub
}
});
Related
I'm trying to access a string outside the parse query, it's not working outside query but it works inside the query.
I declared the global variable and and tried to access it but it's still not working, instead of string it returns null.
I'm new to Java and Android so please forgive me if it's stupid question.
Any help would be appreciated.
public class ActivityQuiz extends AppCompatActivity {
ArrayList<String> answerList = new ArrayList<>();
int c=0;
String Answer = "correct";
String selectedOption;
public static String checkAns;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Question");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> questions, ParseException e) {
if (e == null) {
for (ParseObject question : questions) {
answerList.add(question.getString(Answer));
checkAns = getanswerList.get(c);
Log.d("String check",""+checkAns); //here it's working
}
} else {
e.printStackTrace();
}
}
});
try{
String checkanswer = checkAns ; // here returns null instead of string
Log.d("String checkans",""+checkanswer);}
catch (Exception e)
{
e.printStackTrace();
Log.d("Error", "Error, Not Working");
}
}
There is very simple problem. In first instance you are getting the value of checkAns from the data after hitting the API (in background). So if data come from API it will be non-null.
In later part, since the API response takes time, try-catch block will be executed before it sets value in checkAns So checkAns is equal to initialised value which is null.
Ideally, you should use the value of checkAns only inside done() method.
add your try-catch code inside ParseQuery like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Question");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> questions, ParseException e) {
if (e == null) {
for (ParseObject question : questions) {
answerList.add(question.getString(Answer));
checkAns = getanswerList.get(c);
Log.d("String check",""+checkAns); //here it's working
// ....Add your try catch code.....
try{
String checkanswer = checkAns ; // also working well
Log.d("String checkans",""+checkanswer);}
catch (Exception e)
{
e.printStackTrace();
Log.d("Error", "Error, Not Working");
}
}
} else {
e.printStackTrace();
}
}
});
That is normal behavior for asynchronous functions.
By the time your try/catch block is executed checkanswer is still null because your query takes time to execute. Once done() is executed you'll have your string.
If you must have your value in that block, you can sleep a second or two and check if checkanswer is no longer null. Be careful with sleeps as they will hang your program if you do use them in your main thread
The Problem is that your try catch block will execute befor your query is done means the overriden done method will call and set the checkAns after you execute the try catch block.
If you will use the findInBackground method than make a method in your done method in which you will do all your stuff like:
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> questions, ParseException e) {
if (e == null) {
successful(questions);
} else {
e.printStackTrace();
}
}
});
private void successful(List<ParseObject> questions){
for (ParseObject question : questions) {
answerList.add(question.getString(Answer));
Log.d("String checkans",""+checkanswer);
}
String checkanswer = getanswerList.get(c);
}
else you can use the find() method:
List<ParseObject> questions = query.find();
for (ParseObject question : questions) {
answerList.add(question.getString(Answer));
Log.d("String checkans",""+checkanswer);
}
String checkanswer = getanswerList.get(c);
I'm using CloudBoost for Android application and i had some problems when I try to save save a data in a table, I'm not getting any results. This is my code:
CloudApp.init("*****", "*****");
...
new Thread(new Runnable() {
#Override
public void run() {
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1"); //Already this is not shown
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override
public void done(final CloudObject x, final CloudException e) {
if(e != null)
//error
Log.e("LOG", "Errore");
if(x!=null)
//cloudObject
Log.e("LOG", "FATTO");
}
});
} catch (CloudException e) {
e.printStackTrace();
}
}
});
Your log isn't shown because you made a Thread without calling start().
Android typically uses Asynctask anyway, but I'm not sure why you really need a Thread with this library... That save method looks asynchronous
CloudApp.init("*****", "*****")
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1");
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override
im trying to implement SQLGrammarException into my method.
This method show me column error, but i need to show what procedure the column with error from.
public static PersistenceMicrodataException dealHibernateException(Throwable e) {
e.printStackTrace();
Throwable t = ExceptionUtil.getCause(e);
return new PersistenceMicrodataException(t.getMessage(), t);
}
I try this:
public static PersistenceMicrodataException dealHibernateException(Throwable e) {
try {
Throwable t = ExceptionUtil.getCause(e);
} catch (Exception e) {
System.out.println(t.getMessage());
System.out.println(((SQLGrammarException) t).getSQLState());
System.out.println(((SQLGrammarException) t).getErrorCode());
System.out.println(t.getCause());
}
return new PersistenceMicrodataException(e.getMessage(), e);
}
Someone can help me with this?
I found solution!
public static PersistenceMicrodataException dealHibernateException(Throwable e) {
String concatError = ((SQLGrammarException) e).getSQL() + ((SQLGrammarException) e).getClass() + ((SQLGrammarException) e).getCause();
while (e != null) {
java.lang.System.out.println(concatError);
break;
}
Throwable t = ExceptionUtil.getCause(e);
return new PersistenceMicrodataException(concatError,t);
}
I am currently developing an Android app for existing IOS app, so using Session object to store some data on Parse is crucial for me.
As for creating and uploading sessions to Parse I have no problems.
public static void syncUser() {
ParseUser.getCurrentUser().fetchInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e != null) {
Log.d("", "SYNC ERROR");
e.printStackTrace();
}
if (object != null) {
syncSessions();
}
}
});
}
private static void syncSessions() {
ParseQuery<ParseSession> query = ParseQuery.getQuery(ParseSession.class);
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseSession>() {
#Override
public void done(List<ParseSession> objects, ParseException e) {
for (ParseSession session : objects) {
fetchSession(session, null);
}
}
});
}
public static void fetchSession(final ParseSession session, final OnResultCallback cb) {
session.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
if (cb != null)
cb.onResult(false);
e.printStackTrace();
} else {
if (cb != null)
cb.onResult(true);
ParseRelation<ParseSession> relation = ParseUser.getCurrentUser().getRelation("sessions");
relation.add(session);
syncUser();
}
}
});
}
public static void addNewSession(Date date, String link, int successValue) {
final ParseSession session = new ParseSession();
session.put("date", date);
session.put("link", link);
session.put("successValue", successValue);
session.pinInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null)
e.printStackTrace();
else {
fetchSession(session, new ParseManager.OnResultCallback() {
#Override
public void onResult(boolean success) {
if (success) {
try {
session.unpin();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
});
}
}
});
}
public interface OnResultCallback {
void onResult(boolean success);
}
For creating new Session with my parameters and uploading it I use addNewSession() method, and it displays in Parse dashboard correctly, they have columns for my fields (date, link, successValue) and are stored as the default Session object.
But when I try to load them from Parse to my client, it doesn't work. I load them with this method:
public static void getSessions(final OnResultCallback cb) {
ParseRelation<ParseSession> relation = ParseUser.getCurrentUser().getRelation("sessions");
ParseQuery<ParseSession> query = relation.getQuery();
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseSession>() {
#Override
public void done(List<ParseSession> objects, ParseException e) {
if (e != null) {
e.printStackTrace();
if (cb != null)
cb.onResult(false);
} else {
cb.onResult(true);
}
//if (objects != null)
//USE SESSIONS
}
});
}
I catch an exception:
W/System.err: com.parse.ParseRequest$ParseRequestException: wrong type of relation. Expecting: , but received: _Session
W/System.err: at com.parse.ParseRequest.newPermanentException(ParseRequest.java:270)
Quite similar code on IOS works fine with "sessions" relation. What mistakes have I done?
UPD. I have noticed that I get this exception only when I have allready send some custom created Session to Parse with addNewSession() and then trying to get them using getSessions(). Maybe creating and sending the Session is the problem?
I found the sollution. After I add my newly created Session to User relations with
ParseRelation<ParseSession> relation = ParseUser.getCurrentUser().getRelation("sessions");
relation.add(session);
I tried to save them with fetchInBackground, which was a mistake. I changed syncUser() to ParseUser.getCurrentUser().saveInBackground() and everything worked.
I've installed the example parse server (https://github.com/ParsePlatform/parse-server-example) on my desktop and made a simple app to test it.
My app saves an object to the server, gets the object and sets mTextView's value to the value of my object.
The problem is, when I try this code to get data from server it works:
query.getInBackground("5K7N8a8Dmd", new GetCallback<ParseObject>() ...
(got the object id with curl)
but when I try this (to get object id w/o using curl):
gameScore.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
objectId = gameScore.getObjectId();
} else {
Log.e("saveInBackground", getErrorMessage(e));
}
}
});
...
query.getInBackground(objectId, new GetCallback<ParseObject>() ...
it doesn't work.
logcat:
E/getInBackground﹕ no results found for query - code: 101
MainActivity.java
public class MainActivity extends Activity {
public TextView mTextView;
public String objectId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_view);
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId(Constants.APP_ID)
.server(Constants.SERVER_URL)
.build()
);
final ParseObject gameScore = new ParseObject("Foo1234");
gameScore.put("score", 5000);
gameScore.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
objectId = gameScore.getObjectId();
} else {
Log.e("saveInBackground", getErrorMessage(e));
}
}
});
ParseQuery<ParseObject> query = ParseQuery.getQuery("Foo1234");
query.getInBackground(objectId, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
mTextView.setText(Integer.toString(gameScore.getInt("score")));
} else {
Log.e("getInBackground", getErrorMessage(e));
}
}
});
}
public String getErrorMessage(ParseException e) {
return e.getMessage() + " - code: " + e.getCode();
}
}
Constants.java
public class Constants {
public static String SERVER_URL = "http://192.168.1.14:1337/parse/";
public static String APP_ID = "myAppId";
}
Thanks in advance.
Try this
ParseQuery<ParseObject> query = ParseQuery.getQuery("_Foo1234");
query.whereEqualTo("objectId","wfBB0gpCkP");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
// row of Object Id "wfBB0gpCkP"
} else {
// error
}
}
});
Also change
ParseQuery<ParseObject> query = ParseQuery.getQuery("Foo1234");
to
ParseQuery<ParseObject> query = ParseQuery.getQuery("_Foo1234");
See if this works.
The problem is the "background" part of saveInBackground(). The save isn't complete on the line following the save. In fact, it hasn't even begun to save. The save isn't complete until the completion handler runs.
Consider...
// code here runs **first**
gameScore.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
// code here runs **third, much later than second**
if (e == null) {
objectId = gameScore.getObjectId();
} else {
Log.e("saveInBackground", getErrorMessage(e));
}
}
// code here runs **second, immediately**
Run the "get" query within the completion handler of the save and you'll see that it works. (Of course, there's not much reason to get the object since you have the handle to the object upon which you just invoked save).