Enigman
Enigman

Reputation: 640

AsyncTask: Why is the XML file not being read?

The following is my code. XMLfunctions reads the XML file and stores it in String xml. I had to use AsyncTask as I got a NetworkOnMainThreadException. However I'm not sure of how to do it and am messing it up somewhere.

package foo.bar.quux;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.os.AsyncTask;


public class XMLfunctions {


    int numResults;
    NodeList nodes;
    Document doc;
    static ServiceTasks task;
    String line;
    public final static Document XMLfromString(String xml){

        Document doc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            System.out.println("XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            System.out.println("Wrong XML file structure: " + e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("I/O exeption: " + e.getMessage());
            return null;
        }

        return doc;

    }


     private class ServiceTasks extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... urls) {

                            try {

                                    DefaultHttpClient httpClient = new DefaultHttpClient();
                                    HttpPost httpPost = new HttpPost("http://10.0.2.2/example.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>";
                                }
                            return null;


            }


        } 

     public String getXML(){     

         return line;
    }

     public void executeXML(){
         task = new ServiceTasks();
         task.execute();
     }


}

The methods executeXML() and getXML() are called in another activity :

  public class Main extends ListActivity {

         String xml;
         static XMLfunctions funct;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listofcategories);
            funct = new XMLfunctions();
            funct.executeXML();
            xml = funct.getXML();
.
.
.
}

However xml seems to be null even after executeXML is called as I get a NullPointerException. Where am I going wrong?

Upvotes: 0

Views: 215

Answers (1)

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

You will not get the results until the AsyncTask is done.

So, even though, the control is moved to the line xml = funct.getXML();, and this line is executed, this does not mean that the AsyncTask is done.

Your results will be available when doInBackground returns and in your AsyncTask override the function onPostExecute and report results back to the caller.

Implement a listener in such a way:

package foo.bar.quux;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.os.AsyncTask;


public class XMLfunctions {

    public interface ListenerToXML {
            public void AsyncTaskDone();
    }
    ListenerToXML mListener;
    public void setListener(ListenerToXML listener) {
        mListener = listener;
    }
    int numResults;
    NodeList nodes;
    Document doc;
    static ServiceTasks task;
    String line;
    public final static Document XMLfromString(String xml){

        Document doc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            System.out.println("XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            System.out.println("Wrong XML file structure: " + e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("I/O exeption: " + e.getMessage());
            return null;
        }

        return doc;

    }


     private class ServiceTasks extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... urls) {

                            try {

                                    DefaultHttpClient httpClient = new DefaultHttpClient();
                                    HttpPost httpPost = new HttpPost("http://10.0.2.2/example.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>";
                                }
                            return null;


            }
@Override
protected void onPostExecute(Void result) {
    if(mListener!=null)
        mListener.AsyncTaskDone();
    }


        } 

     public String getXML(){     

         return line;
    }

     public void executeXML(){
         task = new ServiceTasks();
         task.execute();
     }


}




  public class Main extends ListActivity implements ListenerToXML {

         String xml;
         static XMLfunctions funct;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listofcategories);
            funct = new XMLfunctions();
            funct.setListener(this);
            funct.executeXML();
.
.
.
}


            public void AsyncTaskDone() {
               //here you can get the results
            xml = funct.getXML();
            }

Upvotes: 2

Related Questions