foursquared 1

http://zhanwc.iteye.com/blog/834772

外国人真具有共产主义精神,Foursquare都拿出开源了,不像国内某些公司。Foursquare下载地址主页地址http://code.google.com/p/foursquared/ 。下载方式hg clone https://foursquared.googlecode.com/hg/ foursquared ,在linux下用hg命令可以直接下载。Foursquare在代码组织方面当然是相当不错的,层次逻辑规划的相当好,在解决网络读写时的界面阻塞,以及图片加载等耗时操作方面采用的方式值得借鉴。下面以传输协议,图片加载,网络读取方面介绍。

一.协议层

Foursquare采用http协议传输数据。目前如Foursquare,twitter都采用如手机+浏览器+iPad等诸多设备,为了服务器的统一,采用http协议。

Foursquare中的基类HttpApi定义了Http协议的一些接口doHttpRequest,及doHttpPost,AbstractHttpApi则实现了上述方法。Foursquare与服务器交换数据的格式采用XML格式。Foursquare客户端要获取数据时首先构造好http请求,通过http层的doHttpRequest,及doHttpPost层发送http请求,服务器解析http请求,把结果保存为XML格式返回给客户端。以City类来解释XML解析过程,过程中涉及四个类,AbstractParser解析基类,主要用户构造解析器基类,提供解析方法,CityParser继承自AbstractParser,用于解析一条协议,FoursqureType接口无函数定义,用于表示是一个Foursqure类型,City继承于FoursqureType表示一个解析结果。

解析方式中采用了设计模式中的模板模式—定义一个操作中的算法骨架,而将进一步实现延迟到子类中,子类不改变一个算法的结构即可中定义改算法的某些特定步骤,达到复用代码的目的。

在AbstractParser类中定义了解析算法的模板,并且定义了抽象方法abstract protected T parseInner()用于解析一个具体的协议。我们来看模板方法:

1.public final T parse(XmlPullParser parser) throws FoursquareParseException, FoursquareError {   
2.  
3.///算法模板   
4.  
5.try {   
6.  
7.if (parser.getEventType() == XmlPullParser.START_DOCUMENT) {   
8.  
9.parser.nextTag();   
10.  
11.if (parser.getName().equals("error")) {   
12.  
13.throw new FoursquareError(parser.nextText());   
14.  
15.}   
16.  
17.}   
18.  
19.return parseInner(parser); //调用子类具体实现   
20.  
21.} catch (IOException e) {   
22.  
23.if (DEBUG) LOG.log(Level.FINE, "IOException", e);   
24.  
25.throw new FoursquareParseException(e.getMessage());   
26.  
27.} catch (XmlPullParserException e) {   
28.  
29.if (DEBUG) LOG.log(Level.FINE, "XmlPullParserException", e);   
30.  
31.throw new FoursquareParseException(e.getMessage());   
32.  
33.}   
34.  
35.}  

再看下CityParser 子类具体解析的过程:

服务器返回的结果

1.<?xml version=”1.0”?>   
2.  
3.<city>   
4.  
5.< geolat ></ geolat >   
6.  
7.< geolong ></ geolong >   
8.  
9.< id ></ id >   
10.  
11.< name ></ name >   
12.  
13.< shortname ></ shortname >   
14.  
15.< timezone ></ timezone >   
16.  
17.< cityid ></ cityid >   
18.  
19.</city>   
20.  
21.  
22.CityParser中的parseInner解析过程   
23.  
24.parser.require(XmlPullParser.START_TAG, null, null);   
25.  
26.City city = new City();               //解析结果   
27.  
28.while (parser.nextTag() == XmlPullParser.START_TAG) {   
29.  
30.String name = parser.getName();   
31.  
32.if ("geolat".equals(name)) {   
33.  
34.city.setGeolat(parser.nextText());   
35.  
36.} else if ("geolong".equals(name)) {   
37.  
38.city.setGeolong(parser.nextText());   
39.  
40.} else if ("id".equals(name)) {   
41.  
42.city.setId(parser.nextText());   
43.  
44.} else if ("name".equals(name)) {   
45.  
46.city.setName(parser.nextText());   
47.  
48.} else if ("shortname".equals(name)) {   
49.  
50.city.setShortname(parser.nextText());   
51.  
52.} else if ("timezone".equals(name)) {   
53.  
54.city.setTimezone(parser.nextText());   
55.  
56.} else if ("cityid".equals(name)) {   
57.  
58.city.setId(parser.nextText());   
59.  
60.} else {   
61.  
62.skipSubTree(parser);   
63.  
64.}   
65.  
66.}   
67.  
68.return city  

Foursquare中要新添加一条协议的时候只要继承AbstractParser并实现其中的parseInner方法,实现FoursqureType定义一个新的类型就可以了。采用模板方法,无疑提高了系统的可扩展性,以及清晰地代码结构,容易维护,这就是采用面向对象思想带来的好处。

二.图标读取优化 延迟加载+缓存+多线程读取+线程池技术

考虑到手机带宽的限制,以及提升性能,缓存是必不可少的组件。在手机端缓存,主要用户缓存一些常用的不易改变的图片,如:地点,用户,朋友头像等。来分析下缓存的具体实现。缓存实现主要在BaseDiskCache类中。

//用于存放一个图片到缓存中


 

1.public void store(String key, InputStream is) {   
2.  
3.        if (DEBUG) Log.d(TAG, "store: " + key);   
4.  
5.        is = new BufferedInputStream(is);   
6.  
7.        try {   
8.  
9.          OutputStream os = new BufferedOutputStream(new FileOutputStream(getFile(key)));//获取存放路径   
10.  
11.  
12.            byte[] b = new byte[2048];   
13.  
14.            int count;   
15.  
16.            int total = 0;   
17.  
18.  
19.            while ((count = is.read(b)) > 0) {   
20.  
21.                os.write(b, 0, count);   
22.  
23.                total += count;   
24.  
25.            }   
26.  
27.            os.close();   
28.  
29.            if (DEBUG) Log.d(TAG, "store complete: " + key);   
30.  
31.        } catch (IOException e) {   
32.  
33.            if (DEBUG) Log.d(TAG, "store failed to store: " + key, e);   
34.  
35.            return;   
36.  
37.        }   
38.  
39.}   
40.  
41.//获取路径   
42.  
43.public File getFile(String hash) {   
44.  
45.        return new File(mStorageDirectory.toString() + File.separator + hash); //存放路径   
46.  
47.}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值