0

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.

miguel.gazela
  • 143
  • 2
  • 14

2 Answers2

0

You can't show a Toast in doInBackground of AsyncTask as it is a different threat and not the main UI thread.

To show a toast or any UI related task you have to do it in onPostExecute method of AsyncTask.

What you can do is

    new AsyncTask<Void, Void, boolean>() {

            @Override
            protected boolean doInBackground(Void... params) {

                // Initialize QuickBlox application with credentials.
                QBSettings.getInstance().fastConfigInit(Consts.APP_ID, Consts.AUTH_KEY, Consts.AUTH_SECRET);
                boolean res = false;

                // Authorize application

                QBAuth.createSession(new QBCallback() {
                    @Override public void onComplete(Result result) {}
                    @Override
                    public void onComplete(Result result, Object context) {
                        if (result.isSuccess()) {
                            //showMainScreen();
                            res = true
                        } 
                        //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 res;
            }

          protected void onPostExecute(boolean result) {
             if(result) {
                showMainScreen();
             } else {
                Toast.makeText(getBaseContext(), result.getErrors().get(0),                   Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.INVISIBLE);
             }
          }
        }.execute();
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
0

You don't need to use AsyncTasks, because all operations already perform in background.

And also this one

 QBAuth.createSession(new QBCallback() {

it performs request in background to QB and call callback in Main thread.

Could you explain what 'connection timeout error' you got. Is there anything in log?

Rubycon
  • 18,156
  • 10
  • 49
  • 70