Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:23:30+00:00 2026-06-03T12:23:30+00:00

What I am trying to do, is to get a Geonames XML file, containing

  • 0

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(); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:23:33+00:00Added an answer on June 3, 2026 at 12:23 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my app i am trying to get an xml file from an URL.
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
Hi am trying get GPS using network provider and GPS provider. but am getting
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying get Struts 2 and Tiles to work and I am using
Trying to get a list of all possible combinations of 2 arrays passed in
trying to get some data from a plist into the MWPhotoBrowser sample app. Any
Trying to get my css / C# functions to look like this: body {
Trying to get an ASP application deployed; it worked for a while but then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.