0

I try to load xml file in my Android application. When run userList.setInput(xmlUrl.openStream(), null); I see message "The Application has stoped unexpectedly".

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");
                }

What I do wrong? In log I see:

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}
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
user1884872
  • 197
  • 1
  • 3
  • 14

3 Answers3

0

Try doing parsing on another thread excepting main thread. You can do it using Async Task and parse xml in doInBackground() method.

Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
0

There are two Solution of this Problem but first one is great solution.

1) Don't write network call in Main UI Thread, Use Async Task for that.

2) Write below code into your MainActivity file after setContentView(R.layout.activity_main); but this is not proper way.

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

And below import statement into your java file.

import android.os.StrictMode;

And see below link for more information.

Caused by: android.os.NetworkOnMainThreadException

Community
  • 1
  • 1
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

The problem is not that you're doing your parsing in the main thread but is that you're doing a network call on the main thread! Use Async Task or Thread

VinceFR
  • 2,551
  • 1
  • 21
  • 27