0

在我的代码中,当服务器没有响应时,日志中会显示错误消息,但Toastcatch 块中的消息不会显示。我也试过getApplicationContext()但没有帮助。当服务器没有响应时,我会强制关闭并显示消息“应用程序已意外停止。请稍后再试。”

package de.vogella.android.asyntask;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ReadWebpageAsynTask extends Activity {
    private static final String LOG_TAG = null;

private TextView textView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.TextView01);
}

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
 protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
 DefaultHttpClient client = new DefaultHttpClient();
 HttpGet httpGet = new HttpGet(url);
 try {
 HttpResponse execute = client.execute(httpGet);
 InputStream content = execute.getEntity().getContent();

 BufferedReader buffer = new BufferedReader(
   new InputStreamReader(content));
 String s = "";
 while ((s = buffer.readLine()) != null) {
  response += s;
 }

} catch (Exception e) {
         Toast.makeText(ReadWebpageAsynTask.this, "error  server not responding
"     + e.getMessage().toString(),  Toast.LENGTH_LONG).show();


       Log.e(LOG_TAG, "Connection Error ", e);

 e.printStackTrace();
}
}
return response;
}

@Override
protected void onPostExecute(String result) {
textView.setText(result);

 }
}

public void readWebpage(View view) {
 DownloadWebPageTask task = new DownloadWebPageTask();
 task.execute(new String[] { "http://www.vogella.de" });

}
}

这是logcat的输出

 08-12 03:11:28.856: W/dalvikvm(824): threadid=7: thread exiting with uncaught        
  exception (group=0x4001d800)
 08-12 03:11:28.876: E/AndroidRuntime(824): FATAL EXCEPTION: AsyncTask #1
 08-12 03:11:28.876: E/AndroidRuntime(824): java.lang.RuntimeException: An error
  occured while executing doInBackground()
08-12 03:11:28.876: E/AndroidRuntime(824):  at     
android.os.AsyncTask$3.done(AsyncTask.java:200)

08-12 03:11:28.876: E/AndroidRuntime(824):  at     
 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)


   08-12 03:11:28.876: E/AndroidRuntime(824):   at     
 java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-12 03:11:28.876: E/AndroidRuntime(824):  at     
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 08-12 03:11:28.876: E/AndroidRuntime(824):     at     
java.util.concurrent.FutureTask.run(FutureTask.java:137)
 08-12 03:11:28.876: E/AndroidRuntime(824):     at     
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-12 03:11:28.876: E/AndroidRuntime(824):  at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
 08-12 03:11:28.876: E/AndroidRuntime(824):     at    
 java.lang.Thread.run(Thread.java:1096)

 08-12 03:11:28.876: E/AndroidRuntime(824): Caused by: java.lang.RuntimeException:    
 Can't create handler inside thread that has not called Looper.prepare()
 08-12 03:11:28.876: E/AndroidRuntime(824):     at android.os.Handler.<init>    
 (Handler.java:121)
 08-12 03:11:28.876: E/AndroidRuntime(824):     at android.widget.Toast.<init>  
(Toast.java:68)
  08-12 03:11:28.876: E/AndroidRuntime(824):    at     
android.widget.Toast.makeText(Toast.java:231)
 08-12 03:11:28.876: E/AndroidRuntime(824):     at    

  08-12 03:11:28.876: E/AndroidRuntime(824):    at    
 android.os.AsyncTask$2.call(AsyncTask.java:185)
 08-12 03:11:28.876: E/AndroidRuntime(824):     at    
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-12 03:11:28.876: E/AndroidRuntime(824):  ... 4 more
4

1 回答 1

7

我最好的猜测是 Asynctask 线程在显示吐司时完成。所以用

runOnUiThread(new Runnable(){
   @Override
   public void run() {
         Toast.makeText(ReadWebpageAsynTask.this,
         "error - server not responding" + e.getMessage().toString(),
         Toast.LENGTH_LONG).show();
   }
});
于 2012-08-11T21:26:22.330 回答