2016-11-05 56 views
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); 

     } 

    } 

但是,當我調試並運行應用程序時,它會得到Toast顯示。在AsyncTask工作的時候,有沒有辦法做到這一點?在AsyncTask中顯示Toast

謝謝!

+0

請檢查http://stackoverflow.com/questions/3134683/android-toast-in-a-thread – abhishesh

+0

請參閱:http://stackoverflow.com/questions/6134013/android-how-can-i-show -a-to-a-thread-running-in-a-remote-service – abhishesh

+0

你不能在doInBackground方法中顯示吐司,你必須在onPostExecute方法中顯示吐司 –

回答

-1

你不能在doInBackground函數中顯示吐司,因爲在UI線程上doInBackground函數不起作用。您可以將進度發佈到onProgressUpdate函數。它將在UI線程上工作。

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); 

    } 

} 
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 String.valueOf(statusCode); // make this change 
    } 

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

} 
1

你應該用我的代碼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; 
    } 
} 

然後,你可以做

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

這將在用戶界面上運行代碼線程,它會工作。

+0

在這裏使用'enum'是否有特定的原因?不會簡單的靜態方法效率更高嗎?根據[this](https://developer.android.com/topic/performance/memory.html)開發者指南文章_「你應該嚴格避免在Android上使用枚舉」_。 – earthw0rmjim

+0

@ earthw0rmjim枚舉單例模式,就是這樣。至於「嚴格避免使用枚舉」,那麼'enum's只是類,我們應該停止使用類?但是,是的,關鍵是發佈到UI線程的處理程序。 – EpicPandaForce

+0

不,我們不應該停止使用類,因爲沒有更高效的替代方案:)雖然有枚舉。爲什麼是單身模式?這裏沒有屬於特定情況的狀態。爲什麼不只是一個簡單的靜態方法? – earthw0rmjim

3

Toast屬於UI。

我們只能在主線程(UI線程)中更新UI。

AsyncTask.doInBackground()將永遠不會在主線程中調用,這就是原因。

0

在PostExecute方法中始終放置Toast消息。

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

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

Toast只能從UI線程顯示。在UI線程中調用onPostExecute,因此您可以將statusCode存儲在成員變量中,並在onPostExecute方法中檢查404,並在那裏顯示Toast。事情是這樣的:

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(); 
     } 
    } 
} 

或者只是通過狀態代碼作爲參數傳遞給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(); 
     } 
    } 
} 
1

這可能有助於

onPreExecute(){// 一些代碼#1 }

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

       } 
      }); 
} 

onPostExecute() { 
    // some code #3 
} 
0

簡單,當你必須顯示的東西進入你的UI線程(例如吐司消息)然後寫:

runOnUiThread(new Runnable(){ 
    public void run() { 
     //Interaction with UI (Toast message) 
    } 
}); 
1

Toast是UI元素,它不來了,因爲你的應用程序的UI在UI線程上運行,而doInBackground方法的AsyncTask運行在不同的線程中。因此,無論您想要執行的與UI相關的操作應該在onPostExecuteonPreExecute之內。如果出現這種情況,您必須更新doInBackground中的用戶界面,您可以使用處理程序線程或最佳方式,您可以使用runOnUiThread方法,並在其中添加您的敬酒。