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

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.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: 2439 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.location.Location;
11 import android.util.Log;
12 import dk.thoerup.traininfo.StationBean;
13 import dk.thoerup.traininfo.util.DownloadUtil;
14 import dk.thoerup.traininfo.util.XmlUtil;
15
16 public class XmlStationProvider implements StationProvider {
17
18 List<StationBean> stations = new ArrayList<StationBean>();
19
20
21 @Override
22 public List<StationBean> getStations() {
23 return stations;
24 }
25
26 @Override
27 public boolean lookupStations(Location location) {
28 boolean success = false;
29 String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();
30 Log.i("url", url);
31 try {
32 stations.clear();
33
34 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
35
36
37 Document doc = XmlUtil.parseXML(xml);
38 Node rootNode = doc.getDocumentElement(); // stations
39 NodeList stationList = rootNode.getChildNodes();
40
41
42 for (int i=0; i<stationList.getLength(); i++) {
43 Node stationNode = stationList.item(i);
44
45 if (! stationNode.getNodeName().equals("station"))
46 continue;
47
48 StationBean station = new StationBean();
49
50 NodeList stationElements = stationNode.getChildNodes();
51 for (int j=0; j<stationElements.getLength(); j++) {
52 Node current = stationElements.item(j);
53
54 String content = null;
55 if (current.getFirstChild() != null)
56 content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
57
58 String nodeName = current.getNodeName();
59
60 if (nodeName.equals("id"))
61 station.setId( Integer.parseInt(content));
62
63 if (nodeName.equals("name"))
64 station.setName(content);
65
66 if (nodeName.equals("latitude"))
67 station.setLatitude( Double.parseDouble(content) );
68
69 if (nodeName.equals("longitude"))
70 station.setLongitude( Double.parseDouble(content) );
71
72 if (nodeName.equals("stationcode"))
73 station.setCode(content);
74
75 if (nodeName.equals("calcdist"))
76 station.setDistance( (int) Double.parseDouble(content) );
77
78 }
79 stations.add(station);
80 }
81 success = true;
82
83
84 } catch (Exception e) {
85 Log.e("XmlStationProvider", "lookupStations: ", e);
86 }
87 return success;
88 }
89 }

  ViewVC Help
Powered by ViewVC 1.1.20