It work's at Smartphone(Android 2.3.3) but Tablet(Android 4.0) => Force Close!!

 public String getXmlFromUrl(String url) {
        String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost); // HERE FORCE CLOSE, why???
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    // return XML
    return xml;

Any suggestion with example?

  • \$\begingroup\$ Try adding yet another catch block like this: catch (Throwable t) { t.printStackTrace(); } Then see what it prints to your error log. \$\endgroup\$ – ZeroOne Nov 29 '12 at 15:34
  • \$\begingroup\$ because you are networking on the uithread. next time : read the stacktrace, find the exception name, google it. \$\endgroup\$ – njzk2 Jan 15 '13 at 18:18

You should remember this.

From Android 3.x Honeycomb or later, you cannot perform Network IO on the UI thread and doing this throws android.os.NetworkOnMainThreadException. You must use Asynctask .

You have to put your getXmlFromUrl(..) code inside Asynctask method

For more Information Refer this Link http://developer.android.com/reference/android/os/AsyncTask.html

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.