1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
import java.io.ByteArrayInputStream; |
import java.net.URI; |
|
import java.io.IOException; |
|
4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
|
7 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
8 |
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
9 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
|
import org.w3c.dom.Text; |
|
|
import org.xml.sax.SAXException; |
|
10 |
|
|
11 |
import android.location.Location; |
import android.location.Location; |
12 |
|
import android.net.Uri; |
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 |
@Override |
37 |
public void lookupStations(Location location) { |
public boolean lookupStations(String name) { |
38 |
|
String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
|
//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(); |
|
39 |
Log.i("url", url); |
Log.i("url", url); |
40 |
|
return lookupStationsWorker(url); |
41 |
|
} |
42 |
|
|
43 |
|
|
44 |
|
public boolean lookupStationsWorker(String url) { |
45 |
|
boolean success = false; |
46 |
|
|
47 |
try { |
try { |
48 |
stations.clear(); |
stations.clear(); |
49 |
|
|
50 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
51 |
|
|
52 |
|
|
53 |
Document doc = parseXML(xml); |
Document doc = XmlUtil.parseXML(xml); |
54 |
Node rootNode = doc.getDocumentElement(); // stations |
Node rootNode = doc.getDocumentElement(); // stations |
55 |
NodeList stationList = rootNode.getChildNodes(); |
NodeList stationList = rootNode.getChildNodes(); |
56 |
|
|
73 |
|
|
74 |
String nodeName = current.getNodeName(); |
String nodeName = current.getNodeName(); |
75 |
|
|
76 |
Log.i("XML", "" + nodeName + "=" + content); |
if (nodeName.equals("id")) |
77 |
|
station.setId( Integer.parseInt(content)); |
78 |
|
|
79 |
if (nodeName.equals("name")) |
if (nodeName.equals("name")) |
80 |
station.setName(content); |
station.setName(content); |
93 |
|
|
94 |
} |
} |
95 |
stations.add(station); |
stations.add(station); |
96 |
} |
} |
97 |
|
success = true; |
98 |
|
|
99 |
|
|
100 |
} catch (Exception e) { |
} catch (Exception e) { |
101 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
102 |
} |
} |
103 |
} |
return success; |
|
|
|
|
public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException |
|
|
{ |
|
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
|
|
return builder.parse( new ByteArrayInputStream(str.getBytes()) ); |
|
104 |
} |
} |
105 |
} |
} |