--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2010/08/02 23:01:47 1004 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2010/10/04 08:42:12 1160 @@ -2,16 +2,14 @@ import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.List; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; + import android.location.Location; import android.util.Log; -import dk.thoerup.traininfo.StationBean; +import dk.thoerup.android.traininfo.common.StationBean; import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; import dk.thoerup.traininfo.util.XmlUtil; @@ -20,41 +18,46 @@ final static int CACHE_TIMEOUT = 300*1000; - List stations = new ArrayList(); - AndroidTimeoutCache> stationCache = new AndroidTimeoutCache>(CACHE_TIMEOUT); + + AndroidTimeoutCache stationCache = new AndroidTimeoutCache(CACHE_TIMEOUT); - @Override - public List getStations() { - return stations; - } + double roundToPlaces(double value, int places) { + double pow = Math.pow(10, places); + double temp = Math.round( value*pow ); + + return temp / pow; + } @Override - public boolean lookupStations(Location location) { - String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude(); + public StationBean lookupStations(Location location) { + double lat = roundToPlaces(location.getLatitude(), 4); + double lng = roundToPlaces(location.getLongitude(), 4); + + String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng; Log.i("url", url); return lookupStationsWorker(url); } @Override - public boolean lookupStationsByName(String name) { - - // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); - String url = ""; + public StationBean lookupStationsByName(String name) { try { - url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1"); + name = URLEncoder.encode(name, "ISO8859-1"); } catch (Exception e) { - Log.e("lookupStations", "Encoding failed", e); + Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best } + String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name; + + Log.i("url", url); return lookupStationsWorker(url); } @Override - public boolean lookupStationsByIds(String ids) { + public StationBean lookupStationsByIds(String ids) { String url = ""; url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; @@ -62,93 +65,44 @@ return lookupStationsWorker(url); } - public boolean lookupStationsWorker(String url) { - boolean result; - List tmpStations = stationCache.get(url); + public StationBean lookupStationsWorker(String url) { + + StationBean tmpStations = stationCache.get(url); if (tmpStations != null) { Log.i("lookupStations", "cache hit " + url); - result = true; } else { - result = fetchStations(url); + tmpStations = fetchStations(url); - if (result == true) { - stationCache.put(url, stations); + if (tmpStations != null) { + stationCache.put(url, tmpStations); } } - return result; + return tmpStations; } - public boolean fetchStations(String url) { - boolean success = false; + public StationBean fetchStations(String url) { try { - stations.clear(); String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); - - Document doc = XmlUtil.parseXML(xml); - Node rootNode = doc.getDocumentElement(); // stations - NodeList stationList = rootNode.getChildNodes(); - - - for (int i=0; i