更新版本的Android发布与新设备不兼容

问题描述:

我的应用程序在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

这是因为你在主线程上写了网络调用..只需使用Asynctask来代替。它的工作流程较少,在Android中也被称为无痛线程,意味着用户不需要麻烦线程管理。

是的,你必须从你的主线程移动所有网络相关的调用。最简单的解决方案是创建一个AsyncTask,并在您拨打HTTPRequest的所有地方拨打该号码。让您的AsyncTask在后台执行这些操作。