1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
import java.io.ByteArrayInputStream; |
import java.net.URI; |
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.net.Uri; |
14 |
import android.util.Log; |
import android.util.Log; |
15 |
import dk.thoerup.traininfo.StationBean; |
import dk.thoerup.traininfo.StationBean; |
16 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
17 |
|
import dk.thoerup.traininfo.util.XmlUtil; |
18 |
|
|
19 |
public class XmlStationProvider implements StationProvider { |
public class XmlStationProvider implements StationProvider { |
20 |
|
|
26 |
return stations; |
return stations; |
27 |
} |
} |
28 |
|
|
29 |
|
|
30 |
@Override |
@Override |
31 |
public boolean lookupStations(Location location) { |
public boolean lookupStations(Location location) { |
32 |
boolean success = false; |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
|
String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
|
33 |
Log.i("url", url); |
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 { |
try { |
58 |
stations.clear(); |
stations.clear(); |
59 |
|
|
60 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
61 |
|
|
62 |
|
|
63 |
Document doc = parseXML(xml); |
Document doc = XmlUtil.parseXML(xml); |
64 |
Node rootNode = doc.getDocumentElement(); // stations |
Node rootNode = doc.getDocumentElement(); // stations |
65 |
NodeList stationList = rootNode.getChildNodes(); |
NodeList stationList = rootNode.getChildNodes(); |
66 |
|
|
112 |
} |
} |
113 |
return success; |
return success; |
114 |
} |
} |
|
|
|
|
private Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException |
|
|
{ |
|
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
|
|
return builder.parse( new ByteArrayInputStream(str.getBytes()) ); |
|
|
} |
|
115 |
} |
} |