我正在爲Android編寫應用程序。應用程序連接到服務器以檢索評論。 感興趣的代碼是:Android 4.0及更高版本的httpclient.execute(httppost)問題
BufferedReader input = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.domain.com/personal_proj/directorydirectory/file.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.i("TEST","after httpclient.execute()");
HttpEntity entity = response.getEntity();
我測試在HTC野火,HTC Desire Z的這個代碼和它的作品。如果這個代碼是在Android 4.0以上版本中執行,它從來沒有達到線
Log.i("TEST","after httpclient.execute()");
,服務器永遠不會請求,它永遠不會拋出異常。
任何想法?
這很奇怪,但是在你的幫助下,我注意到我沒有很好地處理這個例外。看到我的答案。 – user1148096
很高興你明白了,但是,你永遠不應該在主線程上做一個網絡操作或任何其他長時間運行的背景。 –