0

我尝试在我的 Android 应用程序中加载 xml 文件。当运行 userList.setInput(xmlUrl.openStream(), null); 我看到消息“应用程序已意外停止”。

URL xmlUrl = null;
                String tmp = "";
                 Log.d(LOG_TAG, "BUTTON IS ON");

                try {
                    xmlUrl = new URL("http://55.55.55.55/1.xml");
                    Log.d(LOG_TAG, "1");
                } catch (MalformedURLException e) {             
                    e.printStackTrace();
                }
                XmlPullParser userList = null;
                try {
                    userList = XmlPullParserFactory.newInstance().newPullParser();
                    Log.d(LOG_TAG, "2");
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    userList.setInput(xmlUrl.openStream(), null);
                    Log.d(LOG_TAG, "3");
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.d(LOG_TAG, "3x");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.d(LOG_TAG, "3y");
                }

我做错了什么?在日志中我看到:

12-07 11:53:33.712: DEBUG/dalvikvm(263): GC_EXPLICIT freed 3K, 11% free 17590K/19719K, paused 3ms+4ms
12-07 11:53:33.892: INFO/ActivityManager(88): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.hotdog_master/.MainActivity } from pid 263
12-07 11:53:33.892: WARN/WindowManager(88): Failure taking screenshot for (230x135) to layer 21005
12-07 11:53:33.952: INFO/ActivityManager(88): Start proc com.example.hotdog_master for activity com.example.hotdog_master/.MainActivity: pid=778 uid=10036 gids={3003}
12-07 11:53:35.682: INFO/ActivityManager(88): Displayed com.example.hotdog_master/.MainActivity: +1s746ms
12-07 11:53:36.714: DEBUG/dalvikvm(263): GC_EXPLICIT freed 7K, 11% free 17592K/19719K, paused 3ms+4ms
12-07 11:53:39.112: DEBUG/AndroidRuntime(778): Shutting down VM
12-07 11:53:39.112: WARN/dalvikvm(778): threadid=1: thread exiting with uncaught exception (group=0x40014760)
12-07 11:53:39.122: ERROR/AndroidRuntime(778): FATAL EXCEPTION: main
12-07 11:53:39.122: ERROR/AndroidRuntime(778): android.os.NetworkOnMainThreadException
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at java.net.Socket.connect(Socket.java:901)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at java.net.URL.openStream(URL.java:645)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at com.example.hotdog_master.MainActivity.Xmlworker(MainActivity.java:72)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at com.example.hotdog_master.MainActivity$1.onClick(MainActivity.java:37)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at android.view.View.performClick(View.java:3110)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at android.view.View$PerformClick.run(View.java:11934)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at android.os.Handler.handleCallback(Handler.java:587)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at android.os.Looper.loop(Looper.java:132)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at android.app.ActivityThread.main(ActivityThread.java:4123)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at java.lang.reflect.Method.invoke(Method.java:491)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-07 11:53:39.122: ERROR/AndroidRuntime(778):     at dalvik.system.NativeStart.main(Native Method)
12-07 11:53:39.142: WARN/ActivityManager(88):   Force finishing activity com.example.hotdog_master/.MainActivity
12-07 11:53:39.142: WARN/WindowManager(88): Failure taking screenshot for (230x135) to layer 21010
12-07 11:53:39.662: WARN/ActivityManager(88): Activity pause timeout for ActivityRecord{407709a8 com.example.hotdog_master/.MainActivity}
12-07 11:53:50.532: WARN/ActivityManager(88): Activity destroy timeout for ActivityRecord{407709a8 com.example.hotdog_master/.MainActivity}
4

3 回答 3

0

这个问题有两个解决方案,但第一个是很好的解决方案。

1)不要在主 UI 线程中编写网络调用,为此使用异步任务。

2)之后将以下代码写入您的 MainActivity 文件,setContentView(R.layout.activity_main);但这不是正确的方法。

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

并将下面的 import 语句放入您的 java 文件中。

import android.os.StrictMode;

有关更多信息,请参见下面的链接。

引起:android.os.NetworkOnMainThreadException

于 2012-12-07T09:27:21.007 回答
0

问题不在于您在主线程中进行解析,而在于您正在主线程上进行网络调用!使用异步任务或线程

于 2012-12-07T09:27:31.983 回答
0

尝试在除主线程之外的另一个线程上进行解析。您可以Async Task在方法中使用和解析 xml来做到这一点doInBackground()

于 2012-12-07T09:24:18.357 回答