gpt4 book ai didi

android - DefaultHttpClient 到 AndroidHttpClient

转载 作者:IT老高 更新时间:2023-10-28 22:03:56 25 4
gpt4 key购买 nike

我的代码有问题,希望得到帮助。我首先使用的是这段代码:

        new DefaultHttpClient().execute(new HttpGet(linkk)).getEntity().writeTo(
new FileOutputStream(f));

它在 android 2.3 上运行良好,但在 4.0 上却不行。经过一番研究,我听说使用 AndroidHttpClient 更好,这样它可以在 4.0 和 3.1 上运行。问题是我不知道自己的代码修改是否正确,网上关于AndroidhttpClient的例子也不多。

这是我调整后的代码:

    AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
HttpGet request = new HttpGet(linkk);
HttpResponse response = client.execute(request); //here is where the exception is thrown
response.getEntity().writeTo(new FileOutputStream(f));

这是 logcat 显示的内容:

     01-03 01:32:11.950: W/dalvikvm(17991): threadid=1: thread exiting with uncaught exception (group=0x40a2e1f8)
01-03 01:32:11.986: E/AndroidRuntime(17991): FATAL EXCEPTION: main
01-03 01:32:11.986: E/AndroidRuntime(17991): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lacra.fbirthdays/com.lacra.fbirthdays.ListV}: android.os.NetworkOnMainThreadException
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.os.Looper.loop(Looper.java:137)
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-03 01:32:11.986: E/AndroidRuntime(17991): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 01:32:11.986: E/AndroidRuntime(17991): at java.lang.reflect.Method.invoke(Method.java:511)
01-03 01:32:11.986: E/AndroidRuntime(17991): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 01:32:11.986: E/AndroidRuntime(17991): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 01:32:11.986: E/AndroidRuntime(17991): at dalvik.system.NativeStart.main(Native Method)
01-03 01:32:11.986: E/AndroidRuntime(17991): Caused by: android.os.NetworkOnMainThreadException
01-03 01:32:11.986: E/AndroidRuntime(17991): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-03 01:32:11.986: E/AndroidRuntime(17991): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-03 01:32:11.986: E/AndroidRuntime(17991): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-03 01:32:11.986: E/AndroidRuntime(17991): at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-03 01:32:11.986: E/AndroidRuntime(17991): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-03 01:32:11.986: E/AndroidRuntime(17991): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-03 01:32:11.986: E/AndroidRuntime(17991): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-03 01:32:11.986: E/AndroidRuntime(17991): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-03 01:32:11.986: E/AndroidRuntime(17991): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

最佳答案

StrictMode.ThreadPolicy从 API Level 9 开始引入,并且从 API Level 11 开始更改了默认线程策略,简而言之,不允许在 UI 线程上执行网络操作(包括 HttpClient 和 HttpUrlConnection)。如果你这样做,你会得到 NetworkOnMainThreadException。

这个限制可以改变,使用:

    if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

将上述代码添加到您的主要 Activity 的 onCreate() 方法中。

此外,始终建议将网络操作移出 UI 线程,例如,使用 AsyncTask。

希望对您有所帮助。

关于android - DefaultHttpClient 到 AndroidHttpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8706464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com