I'm working on my first android app and I'm trying to use Quickblox.com as my backend.
In order to use it, I need to authorize the app by creating a session using their SDK.
So, I have the following code:
// Initialize QuickBlox application with credentials.
QBSettings.getInstance().fastConfigInit(Consts.APP_ID, Consts.AUTH_KEY, Consts.AUTH_SECRET);
// Authorize application
QBAuth.createSession(new QBCallback() {
@Override public void onComplete(Result result) {}
@Override
public void onComplete(Result result, Object context) {
if (result.isSuccess()) {
showMainScreen();
} else {
// print errors that came from server
Toast.makeText(getBaseContext(), result.getErrors().get(0), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
}
}, QBQueries.QB_QUERY_AUTHORIZE_APP);
This code works well with an emulator, but it doesn't work if I try with a real android phone. I have a connection timeout error. I think I need to make this kind of requests (Web Services) in the background right?
So I tried to use the AsyncTask to make the request to QB in the background, and changed the code to this:
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Initialize QuickBlox application with credentials.
QBSettings.getInstance().fastConfigInit(Consts.APP_ID, Consts.AUTH_KEY, Consts.AUTH_SECRET);
// Authorize application
QBAuth.createSession(new QBCallback() {
@Override public void onComplete(Result result) {}
@Override
public void onComplete(Result result, Object context) {
if (result.isSuccess()) {
showMainScreen();
} else {
// print errors that came from server
Toast.makeText(getBaseContext(), result.getErrors().get(0), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
}
}, QBQueries.QB_QUERY_AUTHORIZE_APP);
return null;
}
}.execute();
I've seen a lot of similar questions here at SO, but I can't seem to find an answer that works with my code. I saw that functions that deal with the UI need to be called from the main thread, so I suppose that the code I have inside
onComplete(Result result, Object context)
should be inside a block like this right?
runOnUiThread(new Runnable()
{
public void run()
{
// code here
}
});
But I tried that as well and it didn't work. Any guesses?
I believe the problem is not because of the Toast and showMainScreen(). It still fails with this code:
// Initialize QuickBlox application with credentials.
QBSettings.getInstance().fastConfigInit(Consts.APP_ID, Consts.AUTH_KEY, Consts.AUTH_SECRET);
QBAuth.createSession(new QBCallback() {
@Override
public void onComplete(Result arg0, Object arg1) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Result arg0) {
// TODO Auto-generated method stub
}
}, QBQueries.QB_QUERY_AUTHORIZE_APP);
But it doesn't fail if I just create the QBCallback object, without passing it to the QBAuth.createSession function.