0

私のアプリケーションは Android バージョン 2.3 以下で正常に実行されますが、Android 4.0 および 4.2 では応答が null になります。Google で検索した結果、HoneyComb と Ice Cream Sandwich は UI スレッドに対する悪用についてより厳格であることがわかりました。

ICS と HoneyComb が 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.

古いコードを変更する必要はありませんか?プロジェクトで HTTP リクエストを 50 回以上呼び出しました。したがって、すべてのクラスで手動で変更する必要があるため、HTTP 要求で実行されますか?

任意の提案をいただければ幸いです。

4

2 に答える 2

0

これは、メイン スレッドでネットワーク呼び出しを記述したことが原因です。代わりにAsynctaskを使用してください。ワークフローが少なくなり、Android では痛みのないスレッド化としても知られています。これは、ユーザーがスレッド管理に煩わされる必要がないことを意味します。

于 2013-03-06T07:00:50.330 に答える
0

はい、すべてのネットワーク関連の呼び出しをメイン スレッドから移動する必要があります。最も簡単な解決策は、 AsyncTaskを作成し、呼び出しを行ったすべての場所でそれをHTTPRequest呼び出すことです。AsyncTaskバックグラウンドでそれらを行いましょう。

于 2013-03-06T07:02:59.390 に答える