GalHttprequest类库简介——android平台上的一个轻量级的http网络请求及缓存框架

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的信息能够访问官方网站:

想及时了解GalHttprequest的最新消息能够关注做者的微博:
欢迎转发,请保留文章出处: http://my.oschina.net/qiuscut/blog/54882
相关文章
相关标签/搜索