0

我收到以下错误......

08-18 00:02:19.230: D/CLIPBOARD(17993): Hide Clipboard dialog at Starting input: finished by someone else... !
08-18 00:02:21.515: E/Connectivity(17993): true
08-18 00:02:21.535: D/AndroidRuntime(17993): Shutting down VM
08-18 00:02:21.535: W/dalvikvm(17993): threadid=1: thread exiting with uncaught exception (group=0x40c3c1f8)
08-18 00:02:21.545: E/AndroidRuntime(17993): FATAL EXCEPTION: main
08-18 00:02:21.545: E/AndroidRuntime(17993): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.yipl.rssfeed/org.yipl.rssfeed.News}: android.os.NetworkOnMainThreadException
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.os.Looper.loop(Looper.java:137)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.ActivityThread.main(ActivityThread.java:4507)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at java.lang.reflect.Method.invokeNative(Native Method)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at java.lang.reflect.Method.invoke(Method.java:511)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at dalvik.system.NativeStart.main(Native Method)
08-18 00:02:21.545: E/AndroidRuntime(17993): Caused by: android.os.NetworkOnMainThreadException
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.yipl.rssfeed.XMLParser.getXmlFromUrl(XMLParser.java:46)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at org.yipl.rssfeed.News.onCreate(News.java:50)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.Activity.performCreate(Activity.java:4465)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-18 00:02:21.545: E/AndroidRuntime(17993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-18 00:02:21.545: E/AndroidRuntime(17993):    ... 11 more

我尝试使用 logcat 跟踪错误并在以下代码中发现错误:

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

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

        HttpResponse httpResponse = httpClient.execute(httpPost);//I Found  error here in this line...

        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
    Log.e("ErrorPositionTestiong", xml);
    return xml;
}

我的这个程序是 XML 解析功能的一部分。我不明白那个错误是什么意思。请帮助我。在此先感谢。

4

1 回答 1

4

这是一个严格模式违规。简而言之,您不应该在主线程上执行任何网络或数据库操作。使用 AsyncTask 获取 XML。

于 2012-08-17T18:28:02.723 回答