猿问

从DefaultHttpClient到AndroidHttpClient

我的代码有问题,希望得到一些帮助。我首先使用此代码:


        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)


隔江千里
浏览 345回答 3
3回答

开心每一天1111

使用AsyncTask,以使网络请求不会阻止UI线程。该NetworkOnMainThreadException因为API版本11,这就是为什么它是只显示了3.0和最多的原因进行了介绍。private class NetworkTask extends AsyncTask<String, Void, HttpResponse> {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected HttpResponse doInBackground(String... params) {&nbsp; &nbsp; &nbsp; &nbsp; String link = params[0];&nbsp; &nbsp; &nbsp; &nbsp; HttpGet request = new HttpGet(link);&nbsp; &nbsp; &nbsp; &nbsp; AndroidHttpClient client = AndroidHttpClient.newInstance("Android");&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return client.execute(request);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; client.close();&nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPostExecute(HttpResponse result) {&nbsp; &nbsp; &nbsp; &nbsp; //Do something with result&nbsp; &nbsp; &nbsp; &nbsp; if (result != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.getEntity().writeTo(new FileOutputStream(f));&nbsp; &nbsp; }}要简单地调用此线程,请执行此操作。new NetworkTask().execute(linkk);看看这篇写在Android开发人员网站上的文章。它更详细地说明了如何编写应用程序以处理线程。
随时随地看视频慕课网APP

相关分类

Python
我要回答