我在 asyntask.doInBackgroud 中运行 http 连接(使用 HttpClient.execute)。当 http 服务器未启动时,我希望我的 android 应用程序会向用户提示正确的信息,例如“请稍后再试”。但是无论我尝试捕获异常,我的应用程序都会直接崩溃。没有机会提示用户有关情况。
我附上了下面的日志,
07-13 03:50:51.605: E/Util.java, function sendPost(1137): IOException encountered.
07-13 03:50:51.605: W/System.err(1137): org.apache.http.NoHttpResponseException: The target server failed to respond
07-13 03:50:51.605: W/System.err(1137): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
07-13 03:50:51.605: W/System.err(1137): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
07-13 03:50:51.605: W/System.err(1137): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
07-13 03:50:51.605: W/System.err(1137): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
07-13 03:50:51.605: W/System.err(1137): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
07-13 03:50:51.605: W/System.err(1137): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
07-13 03:50:51.615: W/System.err(1137): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
07-13 03:50:51.615: W/System.err(1137): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
07-13 03:50:51.624: W/System.err(1137): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-13 03:50:51.624: W/System.err(1137): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-13 03:50:51.624: W/System.err(1137): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-13 03:50:51.624: W/System.err(1137): at game.location.Util.sendPost(Util.java:27)
07-13 03:50:51.624: W/System.err(1137): at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:73)
07-13 03:50:51.624: W/System.err(1137): at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:1)
07-13 03:50:51.634: W/System.err(1137): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-13 03:50:51.634: W/System.err(1137): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-13 03:50:51.634: W/System.err(1137): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-13 03:50:51.664: W/System.err(1137): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-13 03:50:51.664: W/System.err(1137): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-13 03:50:51.664: W/System.err(1137): at java.lang.Thread.run(Thread.java:1096)
07-13 03:50:51.664: W/dalvikvm(1137): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
07-13 03:50:51.694: E/AndroidRuntime(1137): FATAL EXCEPTION: AsyncTask #1
07-13 03:50:51.694: E/AndroidRuntime(1137): java.lang.RuntimeException: An error occured while executing doInBackground()
07-13 03:50:51.694: E/AndroidRuntime(1137): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.lang.Thread.run(Thread.java:1096)
07-13 03:50:51.694: E/AndroidRuntime(1137): Caused by: java.lang.NullPointerException
07-13 03:50:51.694: E/AndroidRuntime(1137): at game.location.Util.sendPost(Util.java:37)
07-13 03:50:51.694: E/AndroidRuntime(1137): at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:73)
07-13 03:50:51.694: E/AndroidRuntime(1137): at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:1)
07-13 03:50:51.694: E/AndroidRuntime(1137): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-13 03:50:51.694: E/AndroidRuntime(1137): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-13 03:50:51.694: E/AndroidRuntime(1137): ... 4 more
07-13 03:50:51.943: W/IInputConnectionWrapper(1137): showStatusIcon on inactive InputConnection
07-13 03:50:53.114: E/WindowManager(1137): Activity game.location.LoginForm has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f5cbf0 that was originally added here
07-13 03:50:53.114: E/WindowManager(1137): android.view.WindowLeaked: Activity game.location.LoginForm has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f5cbf0 that was originally added here
07-13 03:50:53.114: E/WindowManager(1137): at android.view.ViewRoot.<init>(ViewRoot.java:247)
07-13 03:50:53.114: E/WindowManager(1137): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
07-13 03:50:53.114: E/WindowManager(1137): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-13 03:50:53.114: E/WindowManager(1137): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-13 03:50:53.114: E/WindowManager(1137): at android.app.Dialog.show(Dialog.java:241)
07-13 03:50:53.114: E/WindowManager(1137): at game.location.AbstractActivity.showProgressDialog(AbstractActivity.java:39)
07-13 03:50:53.114: E/WindowManager(1137): at game.location.LoginForm$1.onClick(LoginForm.java:43)
07-13 03:50:53.114: E/WindowManager(1137): at android.view.View.performClick(View.java:2408)
07-13 03:50:53.114: E/WindowManager(1137): at android.view.View$PerformClick.run(View.java:8816)
07-13 03:50:53.114: E/WindowManager(1137): at android.os.Handler.handleCallback(Handler.java:587)
07-13 03:50:53.114: E/WindowManager(1137): at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 03:50:53.114: E/WindowManager(1137): at android.os.Looper.loop(Looper.java:123)
07-13 03:50:53.114: E/WindowManager(1137): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-13 03:50:53.114: E/WindowManager(1137): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 03:50:53.114: E/WindowManager(1137): at java.lang.reflect.Method.invoke(Method.java:521)
07-13 03:50:53.114: E/WindowManager(1137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-13 03:50:53.114: E/WindowManager(1137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-13 03:50:53.114: E/WindowManager(1137): at dalvik.system.NativeStart.main(Native Method)
doInBackGround 中的代码:
protected String doInBackground(String... arg0) {
HttpPost httpPost = new HttpPost(Global.Url.LOGIN.getLink());
String result = "";
List<NameValuePair> pair = new ArrayList<NameValuePair>();
pair.add(new BasicNameValuePair("userName", arg0[0]));
pair.add(new BasicNameValuePair("password", arg0[1]));
try {
httpPost.setEntity(new UrlEncodedFormEntity(pair));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
result = Util.sendPost(httpPost).toString();
return result;
}
我已经放置了所有必要的异常捕获的代码,其中确切的错误来自:
public static StringBuilder sendPost(HttpPost httpPost){
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
StringBuilder result = null;
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
Log.e("Util.java, function sendPost", "ClientProtocolException encountered.");
e.printStackTrace();
} catch (IOException e) {
Log.e("Util.java, function sendPost", "IOException encountered.");
e.printStackTrace();
}
try {
result = inputStreamToString(response.getEntity().getContent());
} catch (IllegalStateException e) {
Log.e("Util.java, function sendPost", "IllegalStateException encountered.");
e.printStackTrace();
} catch (IOException e) {
Log.e("Util.java, function sendPost", "IOException encountered.");
e.printStackTrace();
}
return result;
}
感谢您的帮助。
public static StringBuilder sendPost(HttpPost httpPost){
StringEntity se = null;
try {
se = new StringEntity("envelope",HTTP.UTF_8);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
httpPost.setEntity(se);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
StringBuilder result = null;
final HttpParams httpParameters = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3 * 1000);
HttpConnectionParams.setSoTimeout(httpParameters, 3 * 1000);
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
Log.e("Util.java, function sendPost", "ClientProtocolException encountered.");
e.printStackTrace();
} catch (IOException e) {
Log.e("Util.java, function sendPost", "IOException encountered.");
e.printStackTrace();
} catch (Exception e){
}
try {
result = inputStreamToString(response.getEntity().getContent());
} catch (IllegalStateException e) {
Log.e("Util.java, function sendPost", "IllegalStateException encountered.");
e.printStackTrace();
} catch (IOException e) {
Log.e("Util.java, function sendPost", "IOException encountered.");
e.printStackTrace();
}
return result;
}
嗨@user370305 我已将我的功能更改为这样,但它仍然无法正常工作。