0
  public class ayncClass extends AsyncTask<String, Void, String> {

        public void onPreExecute(){


        }
        @Override
        protected String doInBackground(String... params) {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(URL HERE);
            try{
                HttpResponse responseGiven = client.execute(get);
                StatusLine statusLine = responseGiven.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if(statusCode == 404){
                    Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();

                }
            } catch(Exception e){

            }
            return null;
        }

        public void onPostExecute(...){
            super.onPostExecute(s);

        }

    }

But when I debug and run the app, it does the get the Toast to show up. Is there a way to do actions withing the AsyncTask, while its working?

Thanks!

DilllyBar
  • 59
  • 1
  • 7
  • please check http://stackoverflow.com/questions/3134683/android-toast-in-a-thread – abhishesh Nov 05 '16 at 08:28
  • Refer : http://stackoverflow.com/questions/6134013/android-how-can-i-show-a-toast-from-a-thread-running-in-a-remote-service – abhishesh Nov 05 '16 at 08:29
  • you can't show toast in doInBackground method, you have to show toast inside onPostExecute method – Deepak Kumar Nov 05 '16 at 08:35
  • doInBackground method is for dooing stafff like network communication, calculation etc, not for showing or interacting with UI !. Use onPostExecute or onPreExecute. In on PostExecute check your result and show proper UI element. – Rafal Nov 05 '16 at 08:46

9 Answers9

3

Toast belongs to UI.

We can only update UI in main thread (UI Thread).

AsyncTask.doInBackground() will never be called in main thread and that's the reason.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Qian Sijianhao
  • 564
  • 7
  • 19
1

You should use my code from here:

public enum Toaster {
    INSTANCE;

    private final Handler handler = new Handler(Looper.getMainLooper());

    public void showToast(final Context context, final String message, final int length) {
        handler.post(
            new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, message, length).show();
                }
            }
        );
    }

    public static Toaster get() {
        return INSTANCE;
    }
}

Then you can do

Toaster.get().showToast(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT);

This will run your code on the UI thread, and it will work.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Is there a specific reason for using an `enum` here? Wouldn't a simple static method be much more efficient? According to [this](https://developer.android.com/topic/performance/memory.html) developer's guide article _"You should strictly avoid using enums on Android"_. – earthw0rmjim Nov 05 '16 at 09:48
  • @earthw0rmjim enum singleton pattern, that's all. As for "strictly avoid using enums", well `enum`s are just classes, should we stop using Classes? But yes, the point is the handler that posts to the UI thread. – EpicPandaForce Nov 05 '16 at 10:34
  • No, we shouldn't stop using classes, because there are no more efficient alternatives :) There are to enums though. Why the singleton pattern? There's no state here that belongs to the particular instance. Why not just a simple static method? – earthw0rmjim Nov 05 '16 at 10:52
1

This may help

onPreExecute() { // some code #1 }

doInBackground() {
    runOnUiThread(new Runnable() {
                public void run() {
                    // some code #3 (Write your code here to run in UI thread)

                }
            });
}

onPostExecute() {
   // some code #3
}
Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38
1

Toast is UI element , it is not coming because your application's UI runs on UI thread while doInBackground method of AsyncTask runs in different thread. Hence, whatever UI related operations you want to perform should be in onPostExecute or onPreExecute. If such a condition is there that you have to update UI in doInBackground,you can use handler thread or best way, you can use runOnUiThread method and inside that you can add your toast.

Anuja Kothekar
  • 2,537
  • 2
  • 15
  • 28
0

try below

public class ayncClass extends AsyncTask<String, Void, String> {

    public void onPreExecute(){


    }
    @Override
    protected String doInBackground(String... params) {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(URL HERE);
        try{
            HttpResponse responseGiven = client.execute(get);
            StatusLine statusLine = responseGiven.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if(statusCode == 404){
                // Toast.makeText(getApplicationContext(), "ERROR", //Toast.LENGTH_SHORT).show();

            }
        } catch(Exception e){

        }
        return String.valueOf(statusCode ); // make this change
    }

    public void onPostExecute(String result){
        super.onPostExecute(s);
 Toast.makeText(getApplicationContext(), result, 
 Toast.LENGTH_SHORT).show();
    }

}
Sush
  • 3,864
  • 2
  • 17
  • 35
0

Place the Toast message alway in PostExecute method.

public void onPostExecute(...) { super.onPostExecute(s);

    Toast.makeText(context, "Hellooo I am at Post Execute method", Toast.LENGTH_SHORT).show();
    }
0

Toast can be shown only from UI thread. onPostExecute is called in the UI thread, so you can store statusCode in a member variable and check for 404 in the onPostExecute method and display the Toast there. Something like this:

public class ayncClass extends AsyncTask<String, Void, String> {

    private int mStatusCode;

    @Override
    protected String doInBackground(String... params) {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(URL HERE);
        try{
            HttpResponse responseGiven = client.execute(get);
            StatusLine statusLine = responseGiven.getStatusLine();
            mStatusCode = statusLine.getStatusCode();
        } catch(Exception e){
            // do NOT let catch blocks without log
            // if something bad happens you will never know
        }
        return null;
    }

    public void onPostExecute(...){
        super.onPostExecute(s);
        if(mStatusCode == 404){
            Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
        }
    }
}

Or simply pass the status code as a parameter to onPostExecute:

public class ayncClass extends AsyncTask<String, Void, Integer> {

    @Override
    protected Integer doInBackground(String... params) {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(URL HERE);
        try{
            HttpResponse responseGiven = client.execute(get);
            StatusLine statusLine = responseGiven.getStatusLine();
            return statusLine.getStatusCode();
        } catch(Exception e){
            // do NOT let catch blocks without log
            // if something bad happens you will never know
        }
        return -1;
    }

    public void onPostExecute(Integer statusCode){
        super.onPostExecute(s);
        if(statusCode == 404){
            Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
        }
    }
}
Fabricio
  • 7,705
  • 9
  • 52
  • 87
0

Simply when you must show something into your UI thread (for example the Toast message) then write:

runOnUiThread(new Runnable(){
    public void run() {
        //Interaction with UI (Toast message)
    }
});
Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62
-1

You can not show toast in doInBackground function because doInBackground function does not work on UI Thread. You can publish progress to onProgressUpdate function. it will work on UI Thread.

public class ayncClass extends AsyncTask<String, Void, String> {

    private final Context context;

    public ayncClass(Context context) {
        this.context = context;
    }

    public void onPreExecute(){


    }
    @Override
    protected String doInBackground(String... params) {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(URL HERE);
        try{
            HttpResponse responseGiven = client.execute(get);
            StatusLine statusLine = responseGiven.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if(stat-usCode == 404){
            // you can not use toast in doInBackground function. you need to pass to progress
            publishProgress(0);
            }
        } catch(Exception e){

        }
        return null;
    }

    @Override
        protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        Toast.makeText(context, "ERROR", Toast.LENGTH_SHORT).show();
        }

    public void onPostExecute(...){
        super.onPostExecute(s);

    }

}
okarakose
  • 3,692
  • 5
  • 24
  • 44