1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
import java.io.ByteArrayInputStream; |
|
4 |
import java.io.IOException; |
import java.net.URLEncoder; |
5 |
import java.util.ArrayList; |
import java.util.ArrayList; |
6 |
import java.util.List; |
import java.util.List; |
7 |
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
|
8 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
9 |
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
10 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
|
import org.xml.sax.SAXException; |
|
11 |
|
|
12 |
import android.location.Location; |
import android.location.Location; |
13 |
import android.util.Log; |
import android.util.Log; |
14 |
import dk.thoerup.traininfo.StationBean; |
import dk.thoerup.traininfo.StationBean; |
15 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
16 |
|
import dk.thoerup.traininfo.util.XmlUtil; |
17 |
|
|
18 |
public class XmlStationProvider implements StationProvider { |
public class XmlStationProvider implements StationProvider { |
19 |
|
|
25 |
return stations; |
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 lookupStationsByName(String name) { |
38 |
|
|
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 |
|
Log.i("url", url); |
49 |
|
return lookupStationsWorker(url); |
50 |
|
} |
51 |
|
|
52 |
|
|
53 |
@Override |
@Override |
54 |
public void lookupStations(Location location) { |
public boolean lookupStationsByIds(String ids) { |
55 |
|
String url = ""; |
56 |
|
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
57 |
|
|
|
//String url = "http://pumba.t-hoerup.dk:8080/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
|
|
String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
|
58 |
Log.i("url", url); |
Log.i("url", url); |
59 |
|
return lookupStationsWorker(url); |
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
|
public boolean lookupStationsWorker(String url) { |
64 |
|
boolean success = false; |
65 |
|
|
66 |
try { |
try { |
67 |
stations.clear(); |
stations.clear(); |
68 |
|
|
69 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
70 |
|
|
71 |
|
|
72 |
Document doc = parseXML(xml); |
Document doc = XmlUtil.parseXML(xml); |
73 |
Node rootNode = doc.getDocumentElement(); // stations |
Node rootNode = doc.getDocumentElement(); // stations |
74 |
NodeList stationList = rootNode.getChildNodes(); |
NodeList stationList = rootNode.getChildNodes(); |
75 |
|
|
92 |
|
|
93 |
String nodeName = current.getNodeName(); |
String nodeName = current.getNodeName(); |
94 |
|
|
|
Log.i("XML", "" + nodeName + "=" + content); |
|
|
|
|
95 |
if (nodeName.equals("id")) |
if (nodeName.equals("id")) |
96 |
station.setId( Integer.parseInt(content)); |
station.setId( Integer.parseInt(content)); |
97 |
|
|
109 |
|
|
110 |
if (nodeName.equals("calcdist")) |
if (nodeName.equals("calcdist")) |
111 |
station.setDistance( (int) Double.parseDouble(content) ); |
station.setDistance( (int) Double.parseDouble(content) ); |
112 |
|
|
113 |
|
if (nodeName.equals("address")) |
114 |
|
station.setAddress( content ); |
115 |
|
|
116 |
} |
} |
117 |
stations.add(station); |
stations.add(station); |
118 |
} |
} |
119 |
|
success = true; |
120 |
|
|
121 |
|
|
122 |
} catch (Exception e) { |
} catch (Exception e) { |
123 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
124 |
} |
} |
125 |
} |
return success; |
|
|
|
|
public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException |
|
|
{ |
|
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
|
|
return builder.parse( new ByteArrayInputStream(str.getBytes()) ); |
|
126 |
} |
} |
127 |
} |
} |