Android手机应用基本都有向服务器请求数据的操作,例如音乐软件向服务器请求音乐列表,微博软件向服务器请求微博内容等等。
Android SDK提供了HttpURLConnection类,以及apache下开源的httpclient。
HttpClient简介(百度上抄的):HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.2 (GA)
上面对httpclient有个基本了解。这边就给大家用代码介绍httpclient一些简单的用法。
首先假如要向服务器端获取一个json,也就是数据内容。简单的说向服务器端获取一个字符串。由于android应用可能会频繁的请求数据内容,这边我稍微做点优化,把请求回来的内容和对应的请求地址做个简单的缓存。
假如定义一个请求缓存
public class RequestCache {
private static int CACHE_LIMIT = 10;
private LinkedList<String> history;
private Hashtable<String, String> cache;
public RequestCache(){
history = new LinkedList<String>();
cache = new Hashtable<String, String>();
}
//添加数据
public void put(String url, String data){
history.add(url);
//需要清理
if(history.size() > CACHE_LIMIT){
String old_url = (String) history.poll();
cache.remove(old_url);
}
cache.put(url, data);
}
//获取数据
public String get(String url){
return cache.get(url);
}
}
有了这个requestCache,就可以把请求地址和对应的请求结果存储起来。重点是数据请求:
public class Caller {
//数据缓存对象
private static RequestCache requestCache = null;
//get请求
public static String doGet(String url) throws WSError{
String data = null;
if(requestCache != null){
data = requestCache.get(url);
if(data != null){
return data;
}
}
URI encodedUri = null;
HttpGet httpGet = null;
try {
encodedUri = new URI(url);
httpGet = new HttpGet(encodedUri);
} catch (URISyntaxException e1) {
//清理一些空格
String encodedUrl = url.replace(' ', '+');
httpGet = new HttpGet(encodedUrl);
e1.printStackTrace();
}
//创建httpclient对象
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = null;
try {
//执行请求
try {
httpResponse = httpClient.execute(httpGet);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
//请求数据
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null){
InputStream inputStream = httpEntity.getContent();
data = convertStreamToString(inputStream);
//缓存结果
if(requestCache != null){
requestCache.put(url, data);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void setRequestCache(RequestCache requestCache) {
Caller.requestCache = requestCache;
}
}
上面就是简单的httpclient get请求把请求结果缓存下来。在调用setRequestCache方法的时候,最好在进入程序的时候,初始化好r这个类中的equestCache对象。
建议:可以在Android Application的onCreat()时候调用。
然后可以从服务器返回的字符串进行json解析,显示UI。
本文介绍了如何使用HTTPClient高效地在Android应用中实现数据请求及响应内容的缓存优化,包括请求地址与结果的存储、简单的GET请求实现、以及将请求结果缓存以减少重复请求带来的性能损耗。
200

被折叠的 条评论
为什么被折叠?



