HttpResponse on Android

I have a problem that I want to parse an XML file on the Internet. Now my HttpResponce has been reading this XML file for too long, so I get the "Become Runtime Error" error message for my GUIs.

try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://chukk.nuzoka.com/name.xml");

        HttpResponse httpResponse = httpClient.execute(httpPost);

        HttpEntity httpEntity = httpResponse.getEntity();
        line = EntityUtils.toString(httpEntity);


    } catch (UnsupportedEncodingException e) {
        line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
    } catch (MalformedURLException e) {
        line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
    } catch (IOException e) {
        line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
    }

Any help is appreciated :)

+5
source share
3 answers

As a rule, in user interface programming, and not just on Android, you should move any task of processing large volumes / time to the background thread.

, / Android, AsyncTask .

+3

, Binyamin Sharet, , , .

StrictMode , . , . , ANR .

+1

You want to use an HTTP GET request instead of a POST, since you are not posting any parameters to the web server.

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://chukk.nuzoka.com/name.xml"));
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
+1
source

All Articles