0

我在托管服务器的 Android 应用程序中显示图像。所以,我需要设置StrictMode Policy.

但正如我所检查的,它需要 Api 9,我也想让它在 Api 8 中得到支持。那么我该如何解决这个问题呢?我已经在AsyncTask.

我的代码:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

如果我没有使用 StrictMode,则会出现错误。

日志猫:

05-08 12:25:08.711: E/AndroidRuntime(27622): android.os.NetworkOnMainThreadException
05-08 12:25:08.711: E/AndroidRuntime(27622):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1208)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at java.net.InetAddress.lookupHostByName(InetAddress.java:388)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at com.mytestbuddy.careermirror.Profile$UserProfile.onPostExecute(Profile.java:193)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at com.mytestbuddy.careermirror.Profile$UserProfile.onPostExecute(Profile.java:1)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at android.os.AsyncTask.finish(AsyncTask.java:631)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at android.os.Looper.loop(Looper.java:155)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at android.app.ActivityThread.main(ActivityThread.java:5511)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at java.lang.reflect.Method.invoke(Method.java:511)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
05-08 12:25:08.711: E/AndroidRuntime(27622):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

要解决NetworkOnMainThreadException您看到的问题,请阅读您的 HTTP 响应输入流,doInBackground()而不是onPostExecute().

于 2014-05-08T06:58:09.660 回答
0

StrictMode仅从 2.3 开始可用,因此您不能在 2.2 中使用它。

我可以给你两种可能的方法。

  1. 使用 2.3 [或]
  2. AsyncTask在安卓中使用。

我去AsyncTsk

我希望这能帮到您。

于 2014-05-08T06:50:40.183 回答