`

从Foursquare看手机端程序设计(1)

阅读更多

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

一.协议层

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

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

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

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

public final T parse(XmlPullParser parser) throws FoursquareParseException, FoursquareError {

///算法模板

try {

if (parser.getEventType() == XmlPullParser.START_DOCUMENT) {

parser.nextTag();

if (parser.getName().equals("error")) {

throw new FoursquareError(parser.nextText());

}

}

return parseInner(parser); //调用子类具体实现

} catch (IOException e) {

if (DEBUG) LOG.log(Level.FINE, "IOException", e);

throw new FoursquareParseException(e.getMessage());

} catch (XmlPullParserException e) {

if (DEBUG) LOG.log(Level.FINE, "XmlPullParserException", e);

throw new FoursquareParseException(e.getMessage());

}

}

 

 

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

服务器返回的结果

<?xml version=”1.0”?>

<city>

< geolat ></ geolat >

< geolong ></ geolong >

< id ></ id >

< name ></ name >

< shortname ></ shortname >

< timezone ></ timezone >

< cityid ></ cityid >

</city>


CityParser中的parseInner解析过程

parser.require(XmlPullParser.START_TAG, null, null);

City city = new City();               //解析结果

while (parser.nextTag() == XmlPullParser.START_TAG) {

String name = parser.getName();

if ("geolat".equals(name)) {

city.setGeolat(parser.nextText());

} else if ("geolong".equals(name)) {

city.setGeolong(parser.nextText());

} else if ("id".equals(name)) {

city.setId(parser.nextText());

} else if ("name".equals(name)) {

city.setName(parser.nextText());

} else if ("shortname".equals(name)) {

city.setShortname(parser.nextText());

} else if ("timezone".equals(name)) {

city.setTimezone(parser.nextText());

} else if ("cityid".equals(name)) {

city.setId(parser.nextText());

} else {

skipSubTree(parser);

}

}

return city

 

 

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

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

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

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

 

public void store(String key, InputStream is) {

        if (DEBUG) Log.d(TAG, "store: " + key);

        is = new BufferedInputStream(is);

        try {

          OutputStream os = new BufferedOutputStream(new FileOutputStream(getFile(key)));//获取存放路径


            byte[] b = new byte[2048];

            int count;

            int total = 0;


            while ((count = is.read(b)) > 0) {

                os.write(b, 0, count);

                total += count;

            }

            os.close();

            if (DEBUG) Log.d(TAG, "store complete: " + key);

        } catch (IOException e) {

            if (DEBUG) Log.d(TAG, "store failed to store: " + key, e);

            return;

        }

}

//获取路径

public File getFile(String hash) {

        return new File(mStorageDirectory.toString() + File.separator + hash); //存放路径

}

 

 

分享到:
评论
2 楼 leesazhang 2011-10-27  
谢谢!Foursquare好像是不开源了呢。
1 楼 xiaonan900214 2011-02-26  
现在Foursquare是不是不开源了~不能下载了

相关推荐

Global site tag (gtag.js) - Google Analytics