这里介绍了android快速开发框架afinal0.3的http操作模块 FinalHttp,希望能对你们有所帮助。

afinal 网址:

https://github.com/yangfuhai/afinal

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


普通的get操作:


  1. FinalHttpfh=newFinalHttp();
  2. fh.get("http://www.yangfuhai.com",newAjaxCallBack<String>(){
  3. @Override
  4. publicvoidonLoading(longcount,longcurrent){//每1秒钟自动被回调一次
  5. textView.setText(current+"/"+count);
  6. }
  7. @Override
  8. publicvoidonSuccess(Stringt){
  9. textView.setText(t==null?"null":t);
  10. }
  11. @Override
  12. publicvoidonStart(){
  13. //开始http请求的时候回调
  14. }
  15. @Override
  16. publicvoidonFailure(Throwablet,StringstrMsg){
  17. //加载失败的时候回调
  18. }
  19. });


  1. FinalHttpfh=newFinalHttp();
  2. fh.get("http://www.yangfuhai.com",newAjaxCallBack<String>(){
  3. @Override
  4. publicvoidonLoading(longcount,longcurrent){//每5秒钟自动被回调一次,通过progress是否回调onLoading和回调频率
  5. textView.setText(current+"/"+count);
  6. }
  7. @Override
  8. publicvoidonSuccess(Stringt){
  9. textView.setText(t==null?"null":t);
  10. }
  11. }.progress(true,5));//通过这里设置onloading的频率



文件上传或者数据提交:


  1. AjaxParamsparams=newAjaxParams();
  2. params.put("username","michaelyang");
  3. params.put("password","123456");
  4. params.put("email","test@tsz.net");
  5. params.put("profile_picture",newFile("/mnt/sdcard/pic.jpg"));//上传文件
  6. params.put("profile_picture2",inputStream);//上传数据流
  7. params.put("profile_picture3",newByteArrayInputStream(bytes));//提交字节流
  8. FinalHttpfh=newFinalHttp();
  9. fh.post("http://www.yangfuhai.com",params,newAjaxCallBack<String>(){
  10. @Override
  11. publicvoidonLoading(longcount,longcurrent){
  12. textView.setText(current+"/"+count);
  13. }
  14. @Override
  15. publicvoidonSuccess(Stringt){
  16. textView.setText(t==null?"null":t);
  17. }
  18. });

android文件下载:


  1. FinalHttpfh=newFinalHttp();
  2. fh.download("http://www.xxx.com/下载路径/xxx.apk","/mnt/sdcard/testapk.apk",newAjaxCallBack<File>(){
  3. @Override
  4. publicvoidonLoading(longcount,longcurrent){
  5. textView.setText("下载进度:"+current+"/"+count);
  6. }
  7. @Override
  8. publicvoidonSuccess(Filet){
  9. textView.setText(t==null?"null":t.getAbsoluteFile().toString());
  10. }
  11. });

http cookie操作:


  1. BasicClientCookiebcc=newBasicClientCookie("","");
  2. bcc.setDomain("yangfuhai.com");
  3. bcc.setPath("/");
  4. bcc.setVersion(1);
  5. PreferencesCookieStorepcs=newPreferencesCookieStore(this);
  6. pcs.addCookie(bcc);
  7. FinalHttpfh=newFinalHttp();
  8. hk.setCookieStore(pcs);
  9. fh.post("http://www.yangfuhai.com",newAjaxCallBack<String>(){
  10. @Override
  11. publicvoidonLoading(longcount,longcurrent){
  12. textView.setText(current+"/"+count);
  13. }
  14. @Override
  15. publicvoidonSuccess(Stringt){
  16. textView.setText(t==null?"null":t);
  17. }
  18. });

亲,是不是有了这个框架,你就可以减少至少一半的代码呢?

更多相关文章

  1. Unknown host ‘XXXX: nodename nor servname provided, or not
  2. Android利用DownloadManager实现文件下载
  3. Github android客户端源代码分析之一:环境搭建
  4. android 三种定位方式
  5. Android(安卓)判断网络状态,并且在没有网络的时候,打开网络设置对
  6. Android(安卓)-- 多线程下载
  7. Android源码下载并绑定到Eclipse中
  8. Android(安卓)Studio 查看不到SDK源码解决办法
  9. Android利用DownloadManager实现文件下载

随机推荐

  1. Android(安卓)Tools集合下载
  2. Android(安卓)中文 API (18) —— AbsSeekB
  3. android DPI与分辨率的关系及计算方式
  4. Android(安卓)NDK开发:SeetaFace2实现人脸
  5. 一步步教你为网站开发Android客户端
  6. android背景选择器selector用法汇总
  7. Android命令行工具logcat详细用法!
  8. [置顶] Android——4.2.2 源码目录结构分
  9. 从头学Android之多媒体--使用MediaPlayer
  10. Android(安卓)4.2 BT系统之蓝牙关闭过程