我的應用程序在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請求上運行?
任何建議將不勝感激。
反正你不應該在主線程上運行網絡操作。所以改變它會是一件好事。 – 2013-03-06 07:01:23
您可以創建一個繼承HTTPClient和HTTPUrlConnection的類,然後覆蓋您正在使用的函數,並將調用包裝到AsyncTask中的超級函數中。 – Premsuraj 2013-03-06 07:01:52