/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 357 - (show annotations) (download)
Tue Sep 29 19:06:34 2009 UTC (14 years, 7 months ago) by torben
File size: 2233 byte(s)
Extract service base to a common static final string for easy switch to other service locations
1 package dk.thoerup.traininfo.provider;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Node;
8 import org.w3c.dom.NodeList;
9
10 import android.util.Log;
11 import dk.thoerup.traininfo.TimetableBean;
12 import dk.thoerup.traininfo.util.DownloadUtil;
13 import dk.thoerup.traininfo.util.XmlUtil;
14
15 public class XmlTimetableProvider implements TimetableProvider {
16
17 List<TimetableBean> timetables = new ArrayList<TimetableBean>();
18
19 @Override
20 public List<TimetableBean> getTimetable() {
21 return timetables;
22 }
23
24 @Override
25 public boolean lookupTimetable(String trainID) {
26 boolean success = false;
27 String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainID ;
28 Log.i("url", url);
29 try {
30 timetables.clear();
31
32 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
33
34
35 Document doc = XmlUtil.parseXML(xml);
36 Node rootNode = doc.getDocumentElement(); // stations
37 NodeList stationList = rootNode.getChildNodes();
38
39
40 for (int i=0; i<stationList.getLength(); i++) {
41 Node entryNode = stationList.item(i);
42
43 if (! entryNode.getNodeName().equals("entry"))
44 continue;
45
46 TimetableBean timetable = new TimetableBean();
47
48 NodeList entries = entryNode.getChildNodes();
49 for (int j=0; j<entries.getLength(); j++) {
50 Node current = entries.item(j);
51
52 String content = null;
53 if (current.getFirstChild() != null)
54 content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
55
56 String nodeName = current.getNodeName();
57
58 if (nodeName.equals("station"))
59 timetable.setStation( content );
60
61 if (nodeName.equals("arrival"))
62 timetable.setArrival( content );
63
64 if (nodeName.equals("departure"))
65 timetable.setDeparture( content );
66
67 if (nodeName.equals("current"))
68 timetable.setCurrent( Boolean.parseBoolean(content) );
69
70 }
71 timetables.add(timetable);
72 }
73 success = true;
74
75
76 } catch (Exception e) {
77 Log.e("XmlStationProvider", "lookupStations: ", e);
78 }
79 return success;
80
81 }
82
83 }

  ViewVC Help
Powered by ViewVC 1.1.20