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

  ViewVC Help
Powered by ViewVC 1.1.20