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 finalStringPATH_INPUTSTREAM="http://qiuming.sinaapp.com/?feed=comments-rss2"; static finalStringPATH_STRING="http://qiuming.sinaapp.com/?feed=comments-rss2"; static finalStringPATH_BITMAP="http://tp3.sinaimg.cn/1859125850/180/5628821209/1"; static finalStringPATH_WITHPARAMS="http://qiuming.sinaapp.com/"; static finalStringPATH_POSTCONTENT="http://qiuming.sinaapp.com/?feed=comments-rss2";
  • 同步请求InputStream
request=GalHttpRequest.requestWithURL( this,PATH_INPUTSTREAM);
// 如果不检测缓存,则设置: // request.setCacheEnable(false); // 必须在调用startXXX()函数之前设置
// 返回的缓存已经是ufferedInputStream类型 InputStreamis=request.startSynchronous(); textView.setVisibility(View.VISIBLE); if(is!= null) { textView.setText(is.toString()); }
  • 同步请求String
request=GalHttpRequest.requestWithURL( this,PATH_STRING); // 根据服务器返回的状态读取内容,如果服务器内容没有改变,则直接读取缓存内容,如果服务器内容已经修改,则从服务器拉取数据 // 并刷新缓存内容 Stringstring=request.startSyncRequestString();
  • 同步请求Bitmap
title.setText("同步请求Bitmap"); Headerheader= newBasicHeader("Accept-Language","zh-cn,zh;q=0.5"); // 支持添加自定义的 HttpHeader请求 request=GalHttpRequest.requestWithURL( this,PATH_BITMAP, newHeader[] {header}); // 请求Bitmap,由于图片基本上不改变,因此如果存在缓存,则直接从缓存读取 Bitmapbitmap=request.startSyncRequestBitmap(); imageView.setImageBitmap(bitmap);


  • 异步请求InputStream
title.setText("异步请求InputStream"); request=GalHttpRequest.requestWithURL( this,PATH_INPUTSTREAM); // 必须先设置回调函数,否则调用异步请求无效 request.setListener( newGalHttpRequestListener() { @Override public voidloadFinished( finalInputStreamis, booleanfromcache) { //注意,由于返回的是InputStream,一般情况都需要长时间操作,所以,回调函数是在子线程调用 //因此使用handler handler.post( newRunnable() { @Override public voidrun() { textView.setText(is.toString()); textView.setVisibility(View.VISIBLE); } }); } @Override // 请求失败时,有可能可以从缓存里面读取数据返回 public voidloadFailed( finalHttpResponserespone, InputStreamcacheInputStream) { handler.post( newRunnable() { @Override public voidrun() { textView.setText(respone.toString()); textView.setVisibility(View.VISIBLE); } }); } }); request.startAsynchronous();


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


  • 异步请求Bitmap
request=GalHttpRequest.requestWithURL( this,PATH_BITMAP); request.startAsynRequestBitmap( newGalHttpLoadImageCallBack() { @Override public voidimageLoaded(Bitmapbitmap) { imageView.setImageBitmap(bitmap); imageView.setVisibility(View.VISIBLE); } });

  • 异步组装参数
title.setText("组装http参数"); //交给GalHttprequest自动组装 url中的参数 NameValuePairfeedPair= newBasicNameValuePair("feed","comments-rss2"); request=GalHttpRequest.requestWithURL( this,PATH_WITHPARAMS,feedPair); request.startAsynRequestString( newGalHttpLoadTextCallBack() { @Override public voidtextLoaded(Stringtext) { //该部分允许于UI线程 textView.setText(text); textView.setVisibility(View.VISIBLE); } });

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

想获取关于GalHttprequest的信息可以访问官方网站: http://code.google.com/p/galhttprequest/
想及时了解GalHttprequest的最新消息可以关注作者的微博: http://weibo.com/201200360
欢迎转发,请保留文章出处: http://my.oschina.net/qiuscut/blog/54882

更多相关文章

  1. Android4.0 input touch解析
  2. Android周学习Step By Step(7)--Activity简介
  3. Android应用的跨语言调用小结
  4. Android客户端采用Http 协议Post方式请求与服务端进行数据交互
  5. Android应用程序框架层和系统运行库层日志系统源代码分析
  6. Android(安卓)Service 浅析(生命周期,启动方式,前台Service)
  7. android网络连接使用GET方式请求服务器时的setDoOutput(true)惹
  8. Android中native进程内存泄露的调试技巧
  9. android 判断网络是否可用,并调用系统设置项

随机推荐

  1. Android如何引用其他工程
  2. 【Mark】Android(安卓)Basic_Activity Em
  3. android 对话框大全
  4. android 五种布局模式
  5. Android(安卓)视频录制
  6. Android设置文本框单行多行显示
  7. android短信demo
  8. Android验证码读取
  9. Android版本和API Level , ADT对应关系
  10. Android(安卓)Option Menu