/[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 383 - (hide 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 torben 294 package dk.thoerup.traininfo.provider;
2    
3 torben 381 import java.net.URI;
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 torben 381 import android.net.Uri;
14 torben 294 import android.util.Log;
15     import dk.thoerup.traininfo.StationBean;
16     import dk.thoerup.traininfo.util.DownloadUtil;
17 torben 352 import dk.thoerup.traininfo.util.XmlUtil;
18 torben 294
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 torben 381
30 torben 294 @Override
31 torben 319 public boolean lookupStations(Location location) {
32 torben 357 String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();
33 torben 294 Log.i("url", url);
34 torben 381 return lookupStationsWorker(url);
35     }
36    
37     @Override
38     public boolean lookupStations(String name) {
39 torben 383
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 torben 381 Log.i("url", url);
50     return lookupStationsWorker(url);
51     }
52    
53    
54     public boolean lookupStationsWorker(String url) {
55     boolean success = false;
56    
57 torben 294 try {
58     stations.clear();
59    
60 torben 302 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
61 torben 294
62    
63 torben 352 Document doc = XmlUtil.parseXML(xml);
64 torben 294 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 torben 310 if (nodeName.equals("id"))
87     station.setId( Integer.parseInt(content));
88    
89 torben 294 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 torben 319 }
107     success = true;
108 torben 294
109    
110     } catch (Exception e) {
111     Log.e("XmlStationProvider", "lookupStations: ", e);
112     }
113 torben 319 return success;
114 torben 294 }
115     }

  ViewVC Help
Powered by ViewVC 1.1.20