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
謝謝!
請檢查http://stackoverflow.com/questions/3134683/android-toast-in-a-thread – abhishesh
請參閱:http://stackoverflow.com/questions/6134013/android-how-can-i-show -a-to-a-thread-running-in-a-remote-service – abhishesh
你不能在doInBackground方法中顯示吐司,你必須在onPostExecute方法中顯示吐司 –