Android - How to Download XML Files with Android 4.0?


Ask by : user1761337 November 06, 2012 23:50

I will Download XML File with Android 4.0 my old Code works at Android 2.3.3 here:

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 musst have an Example without DefaultHttpClient . T.I.A.

View original question

Answer by : ErcanNovember 06, 2012 23:50

your problem may be the "strict mode" here.

you have to do http requests with a thread or an AsyncTask.

class RequestTask extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... params) {
             //http request here
            //return the response as string
        }
        @Override
        protected void onPostExecute(String result) {
            //set the the data you get
        }

then:

new RequestTask().execute(yourHttpRequestString)
View original answer