gabi906
gabi906

Reputation: 384

Can't make a toast appear when using doInBackground (AsyncTask)

i'm trying to post on facebook so i'm uising the following code. It works well, but I can't seem to post a simple confirmation toast.

private class FacebookPoster extends AsyncTask<String, Object, Object>
    {

        @Override
        protected Object doInBackground(String... message)
        {
            Bundle parameters = new Bundle();
            parameters.putString("message", message[0]);
            parameters.putString("description", "topic share");
            parameters.putString("link", ctx.getResources().getString(R.string.rateLink));
            parameters.putString("picture", ctx.getResources().getString(R.string.linkIconPicture));
            Log.d("ERRRR", "set params");
            try
            {
                facebook.request("me");
                String response = facebook.request("me/feed", parameters, "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || response.equals("false"))
                {
                    return "Blank response.";

                }
                else
                {
                    return "Message posted to your facebook wall!";

                }
            }
            catch (Exception e)
            {
                return "Failed to post to wall!";
            }
        }

        @Override
        protected void onProgressUpdate(Object... values)
        {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            showToast(values);

        }
    }

showToast function

private void showToast(Object message)
    {
        Toast.makeText(ctx, message.toString(), Toast.LENGTH_SHORT).show();
    }

I think the onProgressUpdate is not called. Please help.

Upvotes: 2

Views: 3130

Answers (4)

Rolf ツ
Rolf ツ

Reputation: 8781

onProgressUpdate is made for updating your progress-bar for example when the code is still running, and you need to call onProgressUpdate manual, using the publishProgress method.

You are returning a string (Object) in the doInBackground method. This string will be send to the onPostExecute method.

So what you should do is remove the onProgressUpdate method, and override the onPostExecute method and put the Toast code inside it.

Note: onProgressUpdate and publishProgress should only be used for updating progress while the background task is still running, not for showing the final result. For the final result you use the onPostExecute method.

http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 4

zeiger
zeiger

Reputation: 700

Override the onPostExecute() method and move the call to showToast() to that method.

It looks like your onProgressUpdate() is not being called at all. And in your scenario, you'd rather show a toast after doInBackground() finishes!

Upvotes: 0

RvdK
RvdK

Reputation: 19800

Ofcourse onProgressUpdate is not being called.

You have to call it yourself in your doInBackground:

publishProgress(<somenumber>);

http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 2

Graham Borland
Graham Borland

Reputation: 60691

You need to call publishProgress() inside your doInBackground().

Upvotes: 1

Related Questions