Android GalHttprequest 简介

来源:互联网 发布:淘宝商品规格怎么设置 编辑:程序博客网 时间:2024/04/28 20:12

GalHttprequest名字的由来

开发过iOS项目的朋友都应该知道 ASIHTTPRequest类库, ASIHTTPRequest对iOS SDK的底层API进行了封装,并使用一套简单的API调用接口即可十分方便地调用HTTP请求。由于之前有接触过ios开发,对ASIHTTPRequest印象十分深刻,最近一直在开发android上的应用,发觉android明显缺少这样一个方便请求HTTP数据及对数据进行缓存管理的工具,因此有了实现一个类似ASIHTTPRequest框架的想法。这就是GalHttprequest名字的由来。

GalHttprequest的简介

GalHttprequest 是基于Httpclient上再进行封装的开源项目了,提供了许多比系统自带的网络相关类库更加方便强大的接口API。目前它已支持以下功能:

* 同步请求Stirng、InputStream、Bitmap;
* 异步请求String、InputStream、Bitmap;支持回调接口;
* 支持异步下载文件,提供监听进度回调接口;
* 支持缓存参数设置;
* 支持多线程及队列请求;
* 自动适配移动、联通、电信wap代理;
* 支持快捷post请求;
* 附带一个强大的日志管理工具类LogUtil
* 自动组装url参数
* 提供简单post数据到服务器的API

GalHttprequest使用的小例子

以下是代码中有可能要用到的链接
static final String PATH_INPUTSTREAM = “http://qiuming.sinaapp.com/?feed=comments-rss2″ ;
static final String PATH_STRING = “http://qiuming.sinaapp.com/?feed=comments-rss2″ ;
static final String PATH_BITMAP = “http://tp3.sinaimg.cn/1859125850/180/5628821209/1″ ;
static final String PATH_WITHPARAMS = “http://qiuming.sinaapp.com/” ;
static final String PATH_POSTCONTENT = “http://qiuming.sinaapp.com/?feed=comments-rss2″ ;

* 同步请求InputStream

request = GalHttpRequest .requestWithURL ( this, PATH_INPUTSTREAM );

// 如果不检测缓存,则设置:
// request.setCacheEnable(false);
// 必须在调用startXXX()函数之前设置

// 返回的缓存已经是ufferedInputStream类型
InputStream is = request .startSynchronous ();
textView .setVisibility (View .VISIBLE );
if ( is!= null ) {
textView .setText (is .toString ());
}

* 同步请求String

request = GalHttpRequest .requestWithURL ( this, PATH_STRING );
// 根据服务器返回的状态读取内容,如果服务器内容没有改变,则直接读取缓存内容,如果服务器内容已经修改,则从服务器拉取数据
// 并刷新缓存内容
String string = request. startSyncRequestString ();

* 同步请求Bitmap

title .setText ("同步请求Bitmap" );
Header header = new BasicHeader ("Accept-Language" , "zh-cn,zh;q=0.5" );
// 支持添加自定义的 Http Header请求
request = GalHttpRequest .requestWithURL ( this, PATH_BITMAP ,
new Header[] { header }) ;
// 请求Bitmap,由于图片基本上不改变,因此如果存在缓存,则直接从缓存读取
Bitmap bitmap = request. startSyncRequestBitmap ();
imageView .setImageBitmap (bitmap );

* 异步请求InputStream

title .setText ("异步请求InputStream" );
request = GalHttpRequest .requestWithURL ( this, PATH_INPUTSTREAM );
// 必须先设置回调函数,否则调用异步请求无效
request. setListener (new GalHttpRequestListener () {
@Override
public void loadFinished ( final InputStream is, boolean fromcache ) {
//注意,由于返回的是InputStream,一般情况都需要长时间操作,所以,回调函数是在子线程调用
//因此使用handler
handler .post ( new Runnable() {
@Override
public void run () {
textView .setText (is .toString ());
textView .setVisibility (View .VISIBLE );
}
}) ;
}
@Override
// 请求失败时,有可能可以从缓存里面读取数据返回
public void loadFailed ( final HttpResponse respone ,
InputStream cacheInputStream ) {
handler .post ( new Runnable() {

@Override
public void run () {
textView .setText (respone .toString ());
textView .setVisibility (View .VISIBLE );
}
}) ;
}
}) ;
request. startAsynchronous ();

* 异步请求String

request = GalHttpRequest .requestWithURL ( this, PATH_STRING );
//第一次调用startAsynRequestString或者startAsynRequestBitmap必须在主线程调用
//因为只有在主线程中调用才可以初始化GalHttprequest内部的全局句柄Handler
request. startAsynRequestString (new GalHttpLoadTextCallBack () {
@Override
public void textLoaded (String text ) {
//该部分允许于UI线程
textView .setText (text );
textView .setVisibility (View .VISIBLE );
}
}) ;

* 异步请求Bitmap

request = GalHttpRequest .requestWithURL ( this, PATH_BITMAP );
request. startAsynRequestBitmap (new GalHttpLoadImageCallBack () {
@Override
public void imageLoaded (Bitmap bitmap ) {
imageView .setImageBitmap (bitmap );
imageView .setVisibility (View .VISIBLE );
}
}) ;

* 异步组装参数

title .setText ("组装http参数" );
//交给GalHttprequest自动组装 url中的参数
NameValuePair feedPair = new BasicNameValuePair ("feed" ,"comments-rss2" );
request = GalHttpRequest .requestWithURL ( this, PATH_WITHPARAMS ,feedPair );
request. startAsynRequestString (new GalHttpLoadTextCallBack () {
@Override
public void textLoaded (String text ) {
//该部分允许于UI线程
textView .setText (text );
textView .setVisibility (View .VISIBLE );
}
}) ;

* 异步post 数据给服务器

//交给GalHttprequest自动组装 url中的参数
request = GalHttpRequest .requestWithURL ( this, PATH_POSTCONTENT );
//设置post内容
request. setPostValueForKey ("name" , "qiuscut" );
request. startAsynRequestString (new GalHttpLoadTextCallBack () {
@Override
public void textLoaded (String text ) {
//该部分允许于UI线程
textView .setText ("在这里post应该是无效的,因为当前url不支持post" );
textView .setVisibility (View .VISIBLE );
}
}) ;

想获取关于GalHttprequest的信息可以访问官方网站:

http://code.google.com/p/galhttprequest/

0 0
原创粉丝点击