1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
import java.io.IOException; |
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
5 |
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
|
6 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
7 |
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
8 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
|
import org.xml.sax.SAXException; |
|
9 |
|
|
10 |
import android.location.Location; |
import android.location.Location; |
11 |
import android.util.Log; |
import android.util.Log; |
12 |
import dk.thoerup.traininfo.StationBean; |
import dk.thoerup.traininfo.StationBean; |
13 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
14 |
|
import dk.thoerup.traininfo.util.XmlUtil; |
15 |
|
|
16 |
public class XmlStationProvider implements StationProvider { |
public class XmlStationProvider implements StationProvider { |
17 |
|
|
24 |
} |
} |
25 |
|
|
26 |
@Override |
@Override |
27 |
public void lookupStations(Location location) { |
public boolean lookupStations(Location location) { |
28 |
|
boolean success = false; |
|
//String url = "http://pumba.t-hoerup.dk:8080/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
|
29 |
String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
30 |
Log.i("url", url); |
Log.i("url", url); |
31 |
try { |
try { |
34 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
35 |
|
|
36 |
|
|
37 |
Document doc = parseXML(xml); |
Document doc = XmlUtil.parseXML(xml); |
38 |
Node rootNode = doc.getDocumentElement(); // stations |
Node rootNode = doc.getDocumentElement(); // stations |
39 |
NodeList stationList = rootNode.getChildNodes(); |
NodeList stationList = rootNode.getChildNodes(); |
40 |
|
|
57 |
|
|
58 |
String nodeName = current.getNodeName(); |
String nodeName = current.getNodeName(); |
59 |
|
|
|
Log.i("XML", "" + nodeName + "=" + content); |
|
|
|
|
60 |
if (nodeName.equals("id")) |
if (nodeName.equals("id")) |
61 |
station.setId( Integer.parseInt(content)); |
station.setId( Integer.parseInt(content)); |
62 |
|
|
77 |
|
|
78 |
} |
} |
79 |
stations.add(station); |
stations.add(station); |
80 |
} |
} |
81 |
|
success = true; |
82 |
|
|
83 |
|
|
84 |
} catch (Exception e) { |
} catch (Exception e) { |
85 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
86 |
} |
} |
87 |
} |
return success; |
|
|
|
|
public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException |
|
|
{ |
|
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
|
|
return builder.parse( new ByteArrayInputStream(str.getBytes()) ); |
|
88 |
} |
} |
89 |
} |
} |