使用 Android快速开发框架 Afinal 0.3 快速开发网络应用相关APK

这里介绍了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","[email protected]");
  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. });

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

你可能感兴趣的:(android)