简体   繁体   中英

Android Http xml error

I would like to get in string one page xml response, but my script always shutdown, anybody has an idea what is the problem with that? In the manifest i take:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Code:

              String URL = "http://sample.com/test.xml";

             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost(URL);

    try {
        response = httpclient.execute(httppost);

    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block

    } catch (IOException e1) {
        // TODO Auto-generated catch block

    }

    HttpEntity r_entity = response.getEntity();

    try {
        xmlString = EntityUtils.toString(r_entity);
        String s = "2";
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

You can't do networking on the UI Thread (the main thread).

You will need to do your network call on another thread. Use one of the following:

Better yet, do this tutorial:

http://developer.android.com/training/basics/network-ops/index.html

This tutorial explains the basic tasks involved in connecting to the network, monitoring the network connection (including connection changes), and giving users control over an app's network usage. It also describes how to parse and consume XML data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM