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

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20