在Android 4上从Internet下载XML文件时出现异常(但它适用于Android 2.3)

时间:2012-08-15 17:09:54

标签: android http-post httpclient android-xml android-internet

我在Android中从Internet下载XML文件时遇到问题。我写了一些代码,它适用于Android 2.3模拟器。但是,它无法在Android 4.03模拟器上运行,也无法在真正的Android 4设备上运行。 这是我的代码:

public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

我在网上有错误:

HttpResponse httpResponse = httpClient.execute(httpPost);

这是来自LogCat的日志:

08-15 16:59:32.436: E/AndroidRuntime(623): FATAL EXCEPTION: main
08-15 16:59:32.436: E/AndroidRuntime(623): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testy/com.example.testy.MainActivity}: android.os.NetworkOnMainThreadException
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.os.Looper.loop(Looper.java:137)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-15 16:59:32.436: E/AndroidRuntime(623):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 16:59:32.436: E/AndroidRuntime(623):  at java.lang.reflect.Method.invoke(Method.java:511)
08-15 16:59:32.436: E/AndroidRuntime(623):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-15 16:59:32.436: E/AndroidRuntime(623):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-15 16:59:32.436: E/AndroidRuntime(623):  at dalvik.system.NativeStart.main(Native Method)
08-15 16:59:32.436: E/AndroidRuntime(623): Caused by: android.os.NetworkOnMainThreadException
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-15 16:59:32.436: E/AndroidRuntime(623):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-15 16:59:32.436: E/AndroidRuntime(623):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-15 16:59:32.436: E/AndroidRuntime(623):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-15 16:59:32.436: E/AndroidRuntime(623):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-15 16:59:32.436: E/AndroidRuntime(623):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-15 16:59:32.436: E/AndroidRuntime(623):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-15 16:59:32.436: E/AndroidRuntime(623):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-15 16:59:32.436: E/AndroidRuntime(623):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-15 16:59:32.436: E/AndroidRuntime(623):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-15 16:59:32.436: E/AndroidRuntime(623):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-15 16:59:32.436: E/AndroidRuntime(623):  at com.example.testy.XMLParser.getXmlFromUrl(XMLParser.java:54)
08-15 16:59:32.436: E/AndroidRuntime(623):  at com.example.testy.MainActivity.onCreate(MainActivity.java:34)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.Activity.performCreate(Activity.java:4465)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-15 16:59:32.436: E/AndroidRuntime(623):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-15 16:59:32.436: E/AndroidRuntime(623):  ... 11 more

我该怎么办?

2 个答案:

答案 0 :(得分:0)

您正在主线程上使用网络功能。您需要使用AsyncTask来完成此任务。 从Honeycomb Strictmode检查开始默认启用。因此网络运营引发了一个例外。姜饼中未启用Strictmode。因此它适用于姜饼,但不适用于ICS

答案 1 :(得分:0)

您正在主线程上执行(可能很慢)网络操作。如果您的目标SDK是11(Honeycomb)或更高,这将在Honeycomb或更高版本上抛出NetworkOnMainThreadException,因为此行为可能会阻止UI并导致应用程序无响应。即使是在Honeycomb之前的设备上this behaviour is discouraged

您可以使用AsyncTask解决此问题,在doInBackground(..)中加载数据。