Question

I have a problem with downloading XML file from Internet in Android. I wrote some code and it works on Android 2.3 emulator. However, it doesn't work on Android 4.03 emulator and it doesn't work on real Android 4 device either. Here is my code:

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

I have error on line:

HttpResponse httpResponse = httpClient.execute(httpPost);

Here is log from 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

What can I do with it?

Was it helpful?

Solution

You're performing a (potentially slow) network operation on the main thread. If your target SDK is 11 (Honeycomb) or higher this will throw a NetworkOnMainThreadException on Honeycomb or above, because this behaviour can block the UI and lead to an unresponsive app. Even on pre-Honeycomb devices this behaviour is discouraged.

You could use an AsyncTask to get around this, loading the data in its doInBackground(..).

OTHER TIPS

You are using network feature on the main thread. You need to use AsyncTask to accomplish this. Starting from Honeycomb Strictmode checking is enabled by default. So Network operations raise an exception. Strictmode was not enabled in Gingerbread. Hence it is working in gingerbread but not in ICS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top