Emraan Khalil
Emraan Khalil

Reputation: 51

HttpResponse in Android

I have a problem that I want to parse an XML-file on the web. Now mine HttpResponce gets too much time too read that XML-file, so I get the error "Become Runtime Error" 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 :)

Upvotes: 5

Views: 3352

Answers (3)

ravishi
ravishi

Reputation: 3579

You want to use an HTTP GET request instead of a POST since you aren't 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);

Upvotes: 1

Dmitry
Dmitry

Reputation: 17350

In addition to what Binyamin Sharet said, there is a special mode you can use during development that can catch design errors like this.

StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users.

Upvotes: 1

MByD
MByD

Reputation: 137422

As a general rule in UI programming, not only Android, you should move to a background thread any heavy-processing/time consuming task.

There are good facilities for that which helps you performing the long task in background and post its final/intermediate results to the UI thread in Android, such as AsyncTask and Handlers.

Upvotes: 3

Related Questions