这里介绍了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. Android Camera数据流分析全程记录
  2. 不仅是微软和诺基亚,谁都无法 fork Android,因为它就没法 fork
  3. Android:微软的金钱机器(更新)
  4. 在设置里面增加关闭和打开GPS ,数据流量的接口
  5. Android打开/关闭数据流量
  6. Android 传感器的 数据流和框架
  7. 五成Android设备要向微软支付专利费
  8. Android Camera 数据流

随机推荐

  1. 京东app首页布局
  2. 第一篇php博客hehe
  3. 好玩、有趣的 Linux 命令学习神器 kmdr!
  4. 百分点认知智能实验室:基于不完全标注样本
  5. 软件开发之交付清单详解
  6. kubernetes ingress 原理、部署
  7. 一个Python程序,可以帮助您自动备份文件
  8. 系统之眼!Linux系统性能监控工具Glances
  9. 介绍Dynamics 365/Power Apps生成PDF的功
  10. MuleSoft网站的架构变迁