ophe
ophe

Reputation: 3

When trying to get Xml data from URL, it returns null

I'm trying to get xml data and parse it with an async task. Here is what I did : In OnCreate method I get url as string. I tested my url and it doesn't return null. Also has permission to connect to Internet.

            startDownload start = new startDownload();
            start.execute(url.toString());  

And my Async class :

protected class startDownload extends AsyncTask<String, Void, String>{
    @Override
    protected void onPreExecute() {

        eczaDialog = ProgressDialog.show(ListViewXML.this,"", "Loading...");
    }
    @Override
    protected String doInBackground(String... aurl) {
        try {
            URL url = new URL(aurl[0]);

            DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize(); ....

When i debug my code i saw that this doc variable returned null. I don't understand where was the problem. I hope you can help me to find out Thanks.

Upvotes: 0

Views: 889

Answers (1)

Guillaume
Guillaume

Reputation: 9061

You have to get the content of the xml. You can use this, the code returns the content in the string, after you can create an XML object :

public static String getStringPage(String url){
    StringBuffer stringBuffer = new StringBuffer();
    BufferedReader bufferedReader = null;
    HttpClient httpClient = null;
    HttpGet httpGet = null;
    URI uri = null;
    HttpResponse httpResponse = null;
    InputStream inputStream = null;
    String HTMLCode = null;


    //Create client and a query to get the page
        httpClient = new DefaultHttpClient();
        httpGet = new HttpGet();

        //Set the query with the url in parameter
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        httpGet.setURI(uri);

        //Execute the query
        try {
            httpResponse = httpClient.execute(httpGet);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Create a buffer to get the page
        try {
            inputStream = httpResponse.getEntity().getContent();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        //Get the buffer caracters
    try {
        HTMLCode = bufferedReader.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    while (HTMLCode!= null){
        stringBuffer.append(HTMLCode);
        stringBuffer.append("\n");
        try {
            HTMLCode = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Return the string of the page code
    return stringBuffer.toString();
}

Upvotes: 1

Related Questions