2013-03-06 128 views
0

我的應用程序在Android 2.3及更低版本上成功運行,但它在android 4.0和4.2中給出了響應null。在谷歌搜索後,我開始知道HoneyComb和冰淇淋三明治對UI線程的濫用要嚴格得多。更新版本的Android發佈與新設備不兼容

其他操作是ICS一些例子和蜂巢不會讓你到UI線程上執行是:

Opening a Socket connection (i.e. new Socket()). 
HTTP requests (i.e. HTTPClient and HTTPUrlConnection). 
Attempting to connect to a remote MySQL database. 
Downloading a file (i.e. Downloader.downloadFile()). 

If you are attempting to perform any of these operations on the UI thread, you must wrap them in a worker thread. The easiest way to do this is to use of an AsyncTask, which allows you to perform asynchronous work on your user interface. An AsyncTask will perform the blocking operations in a worker thread and will publish the results on the UI thread, without requiring you to handle threads and/or handlers yourself. 

這意味着NOE我必須要改變我以前的代碼?我在我的項目中調用了超過50次的HTTP請求。所以我必須在每個類中手動更改它,因此它將在HTTP請求上運行?

任何建議將不勝感激。

+2

反正你不應該在主線程上運行網絡操作。所以改變它會是一件好事。 – 2013-03-06 07:01:23

+1

您可以創建一個繼承HTTPClient和HTTPUrlConnection的類,然後覆蓋您正在使用的函數,並將調用包裝到AsyncTask中的超級函數中。 – Premsuraj 2013-03-06 07:01:52

回答

0

這是因爲你在主線程上寫了網絡調用..只需使用Asynctask來代替。它的工作流程較少,在Android中也被稱爲無痛線程,意味着用戶不需要麻煩線程管理。

0

是的,你必須從你的主線程移動所有網絡相關的調用。最簡單的解決方案是創建一個AsyncTask,並在您撥打HTTPRequest的所有地方撥打該號碼。讓您的AsyncTask在後臺執行這些操作。

相關問題