/[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 381 - (show annotations) (download)
Fri Oct 2 10:39:09 2009 UTC (14 years, 7 months ago) by torben
File size: 2783 byte(s)
Basic implementation of searchStationByName feature
1 package dk.thoerup.traininfo.provider;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Node;
9 import org.w3c.dom.NodeList;
10
11 import android.location.Location;
12 import android.net.Uri;
13 import android.util.Log;
14 import dk.thoerup.traininfo.StationBean;
15 import dk.thoerup.traininfo.util.DownloadUtil;
16 import dk.thoerup.traininfo.util.XmlUtil;
17
18 public class XmlStationProvider implements StationProvider {
19
20 List<StationBean> stations = new ArrayList<StationBean>();
21
22
23 @Override
24 public List<StationBean> getStations() {
25 return stations;
26 }
27
28
29 @Override
30 public boolean lookupStations(Location location) {
31 String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();
32 Log.i("url", url);
33 return lookupStationsWorker(url);
34 }
35
36 @Override
37 public boolean lookupStations(String name) {
38 String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);
39 Log.i("url", url);
40 return lookupStationsWorker(url);
41 }
42
43
44 public boolean lookupStationsWorker(String url) {
45 boolean success = false;
46
47 try {
48 stations.clear();
49
50 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
51
52
53 Document doc = XmlUtil.parseXML(xml);
54 Node rootNode = doc.getDocumentElement(); // stations
55 NodeList stationList = rootNode.getChildNodes();
56
57
58 for (int i=0; i<stationList.getLength(); i++) {
59 Node stationNode = stationList.item(i);
60
61 if (! stationNode.getNodeName().equals("station"))
62 continue;
63
64 StationBean station = new StationBean();
65
66 NodeList stationElements = stationNode.getChildNodes();
67 for (int j=0; j<stationElements.getLength(); j++) {
68 Node current = stationElements.item(j);
69
70 String content = null;
71 if (current.getFirstChild() != null)
72 content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
73
74 String nodeName = current.getNodeName();
75
76 if (nodeName.equals("id"))
77 station.setId( Integer.parseInt(content));
78
79 if (nodeName.equals("name"))
80 station.setName(content);
81
82 if (nodeName.equals("latitude"))
83 station.setLatitude( Double.parseDouble(content) );
84
85 if (nodeName.equals("longitude"))
86 station.setLongitude( Double.parseDouble(content) );
87
88 if (nodeName.equals("stationcode"))
89 station.setCode(content);
90
91 if (nodeName.equals("calcdist"))
92 station.setDistance( (int) Double.parseDouble(content) );
93
94 }
95 stations.add(station);
96 }
97 success = true;
98
99
100 } catch (Exception e) {
101 Log.e("XmlStationProvider", "lookupStations: ", e);
102 }
103 return success;
104 }
105 }

  ViewVC Help
Powered by ViewVC 1.1.20