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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1249 - (show annotations) (download)
Thu Mar 31 17:39:20 2011 UTC (13 years, 1 month ago) by torben
File size: 1903 byte(s)
Prepare traininfo fetcher for regional/stog split
1 package dk.thoerup.traininfo.provider;
2
3
4
5 import org.simpleframework.xml.Serializer;
6 import org.simpleframework.xml.core.Persister;
7
8
9 import android.util.Log;
10 import dk.thoerup.android.traininfo.common.DepartureBean;
11 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
12 import dk.thoerup.traininfo.util.DownloadUtil;
13 import dk.thoerup.traininfo.util.XmlUtil;
14
15 public class XmlDepartureProvider implements DepartureProvider {
16
17 final static int CACHE_TIMEOUT = 60*1000;
18
19
20 AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);
21
22
23
24 @Override
25 public DepartureBean lookupDepartures(int stationID, boolean arrival,String type) {
26
27 String key = "" + stationID + ":" + arrival + ":" + type;
28
29 DepartureBean departures = departureCache.get(key);
30
31 if (departures == null) {
32 departures = lookupDeparturesWorker(stationID, arrival, type);
33
34 if (departures != null) {
35 departureCache.put(key, departures);
36 }
37
38 } else {
39 Log.i("XmlDepartureProvider", "cache hit !!!");
40 }
41
42 return departures;
43 }
44
45 private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival, String type) {
46
47 try
48 {
49 int iArrival = arrival ? 1 : 0;
50 String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival + "&type=" + type;
51 Log.i("xmlurl",url);
52 String doc = DownloadUtil.getContentString(url, 30000, "ISO-8859-1");
53
54 Serializer serializer = new Persister();
55
56 DepartureBean departures = serializer.read(DepartureBean.class, doc);
57
58
59 return departures;
60
61
62 } catch (Exception e) {
63 Log.e("XmlDepartureProvider", "looupFunction", e);
64 return null;
65 }
66 }
67
68 @Override
69 public void purgeOldEntries() {
70 departureCache.purgeOldEntries();
71 }
72
73 }

  ViewVC Help
Powered by ViewVC 1.1.20