0

我在 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

我能用它做什么?

4

2 回答 2

0

您正在主线程上使用网络功能。您需要使用AsyncTask来完成此操作。从 Honeycomb 开始Strictmode检查默认启用。所以网络操作会引发异常。Gingerbread 中未启用严格模式。因此,它适用于姜饼,但不适用于 ICS

于 2012-08-15T17:26:50.047 回答
0

您正在主线程上执行(可能很慢)网络操作。如果您的目标 SDK 是 11 (Honeycomb) 或更高版本,这将NetworkOnMainThreadException在 Honeycomb 或更高版本上抛出异常,因为此行为可能会阻塞 UI 并导致应用程序无响应。即使在预 Honeycomb 设备上,也不鼓励这种行为

您可以使用 anAsyncTask来解决这个问题,将数据加载到其doInBackground(..).

于 2012-08-15T18:07:35.217 回答