Search code examples
javaandroidandroid-asynctaskgeonames

How to get and parse Geonames XML using AsyncTask/Android


What I am trying to do, is to get a Geonames XML file, containing nearby Wikipedia articles, based on my GPS location and display Links to the articles on my map or in a spinner or whatever.

This code is working perfectly fine in a non-android application:

private double lat = 44; //in my app, this would be the actual coordinates
private double lng = 9;

try{
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder= factory.newDocumentBuilder();

        URL url = new URL("http://api.geonames.org/findNearbyWikipedia?lat="+lat+"&lng="+lng+"&username=my_username");
        InputStream stream = url.openStream();
        Document xmlDocument = builder.parse(stream);


        NodeList titleNodes= xmlDocument.getElementsByTagName("title");
        NodeList articleURLNodes= xmlDocument.getElementsByTagName("wikipediaUrl");
        for(int i=0; i<titleNodes.getLength(); i++){


        System.out.println("Artikel "+ (i+1)+": "+ titleNodes.item(i).getTextContent() + " " + articleURLNodes.item(i).getTextContent());
        }
        }catch(Exception ex){
        System.out.println(ex.getMessage());
        }

}

It prints all the articles + urls.

Now, as I am using android, I need to use AsyncTaks, otherwise I get a NetworkOnMainThread Exception.. so far I didn't really understand how those work and how I can use them.

This is my attempt:

private class DownloadXMLTask extends AsyncTask<String, Void, InputStream> {

     String wikiUrl;
     URL url;
     InputStream stream;


     protected InputStream doInBackground(String... urls) {

        try {

            wikiUrl=urls[0];
            url = new URL(wikiUrl);
            InputStream stream = url.openStream();

        }catch(Exception ex){
            System.out.println(ex.getMessage());
            }
        return stream;

     }

I want to pass the url and get the InputStream in return to do the parsing like shown above.

But how do I get the result of the AsyncTask?

P.S. Here's my solution:

private class DownloadXMLTask extends AsyncTask<String, Void, List<NodeList>> {

     String wikiUrl;
     URL url;
     InputStream stream;
     DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
     DocumentBuilder builder;
     List<NodeList> liste = new ArrayList<NodeList>();

     protected List<NodeList> doInBackground(String... urls) {

        try {

            wikiUrl=urls[0];
            url = new URL(wikiUrl);
            InputStream stream = url.openStream();
            builder = factory.newDocumentBuilder();
            Document xmlDocument = builder.parse(stream);

            NodeList titleNodes=     xmlDocument.getElementsByTagName("title");
            NodeList articleURLNodes= xmlDocument.getElementsByTagName("wikipediaUrl");

            liste.add(titleNodes);
            liste.add(articleURLNodes);

            return liste;
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            }
        return null;


     }
 }

I return a list to which I attached the NodeLists, I need for further processing.

In my Activity I can access them like this:

List<NodeList> liste = new DownloadXMLTask().execute(wikiAdresse).get();
NodeList titel = liste.get(0);
NodeList articleURL = liste.get(1);

// just a quick toast to show it works! =)
Toast.makeText(this, titel.item(0).getTextContent()+" "+articleURL.item(0).getTextContent(), Toast.LENGTH_LONG).show(); 

Solution

  • Here is a tutorial for AsyncTask http://droidapp.co.uk/?p=177

    And here is one for parsing RSS but any XML like your code will follow same with amendments http://droidapp.co.uk/?p=166