0

I have the following Java method for parsing XML data:

public static List<String> getStations(){

    List<String> stnames = new ArrayList<String>();

    try {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();


        Document doc = (Document) docBuilder.parse(new URL("http://api.irishrail.ie/realtime/realtime.asmx/getAllStationsXML_WithStationType?StationType=D").openStream());

        doc.getDocumentElement().normalize();

        NodeList stationames = doc.getElementsByTagName("objStation");

        for(int i = 0; i<stationames.getLength(); i++){

            Element firstStat = (Element) stationames.item(i);
            Element stationalias = (Element) firstStat.getElementsByTagName("StationDesc").item(0);

            stnames.add(stationalias.getTextContent().trim());

        }



    } catch (SAXParseException err) {

        System.out.println("** Parsing error" + ", line "
                + err.getLineNumber() + ", uri " + err.getSystemId());
        System.out.println(" " + err.getMessage());

    } catch (SAXException e) {

        Exception x = e.getException();
        ((x == null) ? e : x).printStackTrace();

    } catch (Throwable t) {
        t.printStackTrace();

    }

    return stnames;

}

As you can see the method returns a List of Strings. This works perfectly on Eclipse, as I can print the List out on the console, using System.out.println(getStations());

Now, I copied and pasted this method into my Android program. Clearly the method returns a List of Strings which is a requirement for setting up an Array Adapter for a List View in android.

So I have the following android code:

    List<String> sts = getStations();

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            sts );

    lv.setAdapter(arrayAdapter);  // where lv is my List View

Running this code gives me a blank List View. I am running the android project on my Android Phone through a USB cable. I do have WIFI on my Android device, I have also pasted the following permission to my manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    ...
    ...

So, does anyone have any idea why my List View is not showing any information?

It is driving me mad, I've been working on this simple problem for hours.

I am also having problems debugging using the Log.e() method so I can't provide any information there.

Greg Peckory
  • 7,700
  • 21
  • 67
  • 114
  • Try checking whether the list is empty sts.isEmpty(). I think the List is empty. – capt.swag Jun 18 '15 at 15:49
  • Probably a catched exception in getStations and as a result, an empty list is returned. – M4rtini Jun 18 '15 at 15:55
  • 1
    Are you running this in the main thread? Network code on the main thread is not allowed on Android – M4rtini Jun 18 '15 at 16:00
  • @4k3R The List is indeed empty. The source of the problem must be in the method then. This would probably imply that the connection is failing, And I am fairly certain my phone has connection. M4rtini This must mean you are correct. The method which requires the internet connection is in the MainActivity.java class but not in the onCreate() method. But it is being called in the onCreate() method. What would this imply? – Greg Peckory Jun 18 '15 at 16:54
  • @M4rtini see above comment – Greg Peckory Jun 18 '15 at 16:55
  • 1
    Without any debug info it's impossible to say for sure. But my guess would be a [NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – M4rtini Jun 18 '15 at 17:12
  • I ran a checkConnection method which shows I am connected which is great, but the List still won't populate and remains empty. – Greg Peckory Jun 18 '15 at 17:15

0 Answers0