外国人真具有共产主义精神,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()用于解析一个具体的协议。我们来看模板方法:
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); //存放路径
}
分享到:
相关推荐
foursquare数据集1-4
国外著名的LBS社交类应用程序 foursquare源码,供大家分析研究
Foursquare GPS 定位地图交友, 并获取商家打折信息。
Foursquare数据集 Abstract: Foursquare is a location-based social networking website, software for mobile devices. This service is available to users with GPS enabled mobile devices, such as iPhones ...
foursquare, 用于 python的Foursquare API客户端 foursquare用于 foursquare API的python 包装器。哲学:映射 foursquare one-to-one的端点简洁,简单,Pythonic 调用只处理原始数据,定义你自己的模型功能:pyth
FourSquare NYC 数据集
foursquare-ios-api, 用于iOS的Foursquare API v2 用于iOS的 Foursquarefoursquare API的简单 ... 它允许你将foursquare集成到你的iOS应用程序中。特性简单,小巧,易于使用使用Safari进行身份验证( 请参见下面的
Foursquare源码 里面登录的代码让我注释掉了,用的时候打开就行
定位社交网站foursquare的 android客户端源代码,上传给大家学习。希望有用。 谢谢下载。 我是为了赚点分下载别的, 不然不要分的!
数据集包含168个美国城市的foursquare签到数据,适合做基于地理位置的社交网络的研究
Unofficial Foursquare® Client for Android Wear. Download: (join the beta) Features: Lightning fast check-in from your watch: Quickly explore your neighbourhood: Future plans report ...
foursquare开源数据集,供做兴趣点推荐相关算法的人员使用,没有经过处理,大家可以下载根据自己的需要进行数据预处理
用于做位置预测和位置推荐的数据...Foursquare的用户较少,本文选取家在纽约的用户进行签到行为研巧。Foursquare签到 数据集用户个人信息中具有homecity属性.表征用户的家所在位置。
国外LBS--Foursquare案例研究报告 PDF
Collection Picker is an Android View library which looks like Foursquare Tastes picker. For more information see the website. Usage Gradle compile 'com.anton46:collection-picker:1.0.2' Maven ...
该资源为正在研究推荐系统的人们提供了FourSquare和Gowalla两大数据集,是经过处理过的,被用于RankGeoFM方法试验对比。有需要的可以下载
这个源码实现了Foursquare设计和交互并显示地理位置,源码TGFoursquareLocationDetail,TGFoursquareLocationDetail-Demo重新创造了Foursquare的设计和交互,并展示地位位置细节。该demo并没有实现Foursquare的全部...
有foursquare和gowalla两个数据集poidata.zip