1 |
package dk.thoerup.traininfo.provider; |
2 |
|
3 |
import org.simpleframework.xml.Serializer; |
4 |
import org.simpleframework.xml.core.Persister; |
5 |
|
6 |
import android.util.Log; |
7 |
import dk.thoerup.android.traininfo.common.MetroBean; |
8 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
9 |
import dk.thoerup.traininfo.util.DownloadUtil; |
10 |
import dk.thoerup.traininfo.util.XmlUtil; |
11 |
|
12 |
public class XmlMetroProvider implements MetroProvider { |
13 |
|
14 |
final int CACHE_TIMEOUT = 60000; |
15 |
|
16 |
AndroidTimeoutCache<Integer,MetroBean> metroCache = new AndroidTimeoutCache<Integer,MetroBean>(CACHE_TIMEOUT); |
17 |
|
18 |
|
19 |
@Override |
20 |
public MetroBean lookupMetroInfo(int stationID) { |
21 |
|
22 |
MetroBean metro = metroCache.get(stationID); |
23 |
|
24 |
if (metro == null) { |
25 |
metro = lookupMetroWorker(stationID); |
26 |
|
27 |
if (metro != null) { |
28 |
metroCache.put(stationID, metro); |
29 |
} |
30 |
|
31 |
} else { |
32 |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
33 |
} |
34 |
|
35 |
return metro; |
36 |
} |
37 |
|
38 |
|
39 |
private MetroBean lookupMetroWorker(int stationID) { |
40 |
|
41 |
try |
42 |
{ |
43 |
String url = XmlUtil.SERVICE_BASE + "/MetroServlet?station=" + stationID; |
44 |
Log.i("xmlurl",url); |
45 |
String doc = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
46 |
|
47 |
Serializer serializer = new Persister(); |
48 |
|
49 |
MetroBean metro = serializer.read(MetroBean.class, doc); |
50 |
|
51 |
return metro; |
52 |
|
53 |
|
54 |
} catch (Exception e) { |
55 |
Log.e("XmlMetroProvider", "lookupFunction", e); |
56 |
return null; |
57 |
} |
58 |
} |
59 |
|
60 |
} |