--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2009/09/07 13:23:48 302 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2010/02/01 16:52:52 574 @@ -1,24 +1,19 @@ package dk.thoerup.traininfo.provider; -import java.io.ByteArrayInputStream; -import java.io.IOException; + +import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.w3c.dom.Text; -import org.xml.sax.SAXException; import android.location.Location; import android.util.Log; import dk.thoerup.traininfo.StationBean; import dk.thoerup.traininfo.util.DownloadUtil; +import dk.thoerup.traininfo.util.XmlUtil; public class XmlStationProvider implements StationProvider { @@ -30,19 +25,51 @@ return stations; } + + @Override + public boolean lookupStations(Location location) { + String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude(); + Log.i("url", url); + return lookupStationsWorker(url); + } + @Override - public void lookupStations(Location location) { + public boolean lookupStationsByName(String name) { + + // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); + String url = ""; + + try { + url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1"); + } catch (Exception e) { + Log.e("lookupStations", "Encoding failed", e); + } - //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(); Log.i("url", url); + return lookupStationsWorker(url); + } + + + @Override + public boolean lookupStationsByIds(String ids) { + String url = ""; + url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; + + Log.i("url", url); + return lookupStationsWorker(url); + } + + + public boolean lookupStationsWorker(String url) { + boolean success = false; + try { stations.clear(); String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); - Document doc = parseXML(xml); + Document doc = XmlUtil.parseXML(xml); Node rootNode = doc.getDocumentElement(); // stations NodeList stationList = rootNode.getChildNodes(); @@ -65,7 +92,8 @@ String nodeName = current.getNodeName(); - Log.i("XML", "" + nodeName + "=" + content); + if (nodeName.equals("id")) + station.setId( Integer.parseInt(content)); if (nodeName.equals("name")) station.setName(content); @@ -74,27 +102,32 @@ station.setLatitude( Double.parseDouble(content) ); if (nodeName.equals("longitude")) - station.setLongitude( Double.parseDouble(content) ); - - if (nodeName.equals("stationcode")) - station.setCode(content); + station.setLongitude( Double.parseDouble(content) ); if (nodeName.equals("calcdist")) station.setDistance( (int) Double.parseDouble(content) ); + + if (nodeName.equals("address")) + station.setAddress( content ); + + if (nodeName.equals("regional")) + station.setRegional( Boolean.parseBoolean(content) ); + + if (nodeName.equals("strain")) + station.setSTrain( Boolean.parseBoolean(content) ); + + if (nodeName.equals("metro")) + station.setMetro( Boolean.parseBoolean(content) ); } stations.add(station); - } + } + success = true; } catch (Exception e) { Log.e("XmlStationProvider", "lookupStations: ", e); } - } - - public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException - { - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - return builder.parse( new ByteArrayInputStream(str.getBytes()) ); + return success; } }