/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1066 - (show annotations) (download)
Thu Sep 16 15:32:42 2010 UTC (13 years, 8 months ago) by torben
File size: 2785 byte(s)
Experimental #5, update TrainInfo client to use common data-beans
1 package dk.thoerup.traininfo.provider;
2
3
4 import java.net.URLEncoder;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.simpleframework.xml.Serializer;
9 import org.simpleframework.xml.core.Persister;
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13
14 import android.location.Location;
15 import android.util.Log;
16 import dk.thoerup.android.traininfo.common.StationBean;
17 import dk.thoerup.android.traininfo.common.TimetableBean;
18 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
19 import dk.thoerup.traininfo.util.DownloadUtil;
20 import dk.thoerup.traininfo.util.XmlUtil;
21
22 public class XmlStationProvider implements StationProvider {
23
24 final static int CACHE_TIMEOUT = 300*1000;
25
26 //List<StationBean> stations = new ArrayList<StationBean>();
27 AndroidTimeoutCache<String, StationBean> stationCache = new AndroidTimeoutCache<String, StationBean>(CACHE_TIMEOUT);
28
29
30 double roundToPlaces(double value, int places) {
31 double pow = Math.pow(10, places);
32 double temp = Math.round( value*pow );
33
34 return temp / pow;
35 }
36
37 @Override
38 public StationBean lookupStations(Location location) {
39 double lat = roundToPlaces(location.getLatitude(), 4);
40 double lng = roundToPlaces(location.getLongitude(), 4);
41
42 String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng;
43 Log.i("url", url);
44 return lookupStationsWorker(url);
45 }
46
47 @Override
48 public StationBean lookupStationsByName(String name) {
49
50 // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);
51 String url = "";
52
53 try {
54 url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1");
55 } catch (Exception e) {
56 Log.e("lookupStations", "Encoding failed", e);
57 }
58
59 Log.i("url", url);
60 return lookupStationsWorker(url);
61 }
62
63
64 @Override
65 public StationBean lookupStationsByIds(String ids) {
66 String url = "";
67 url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
68
69 Log.i("url", url);
70 return lookupStationsWorker(url);
71 }
72
73 public StationBean lookupStationsWorker(String url) {
74
75 StationBean tmpStations = stationCache.get(url);
76
77 if (tmpStations != null) {
78 Log.i("lookupStations", "cache hit " + url);
79 } else {
80 tmpStations = fetchStations(url);
81
82 if (tmpStations != null) {
83 stationCache.put(url, tmpStations);
84 }
85 }
86
87 return tmpStations;
88 }
89
90
91 public StationBean fetchStations(String url) {
92
93 try {
94
95 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
96
97 Serializer serializer = new Persister();
98
99 StationBean stations = serializer.read(StationBean.class, xml);
100
101
102 return stations;
103
104
105 } catch (Exception e) {
106 Log.e("XmlStationProvider", "lookupStations: ", e);
107 return null;
108 }
109 }
110 }

  ViewVC Help
Powered by ViewVC 1.1.20