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

本文介绍AFinal框架0.3版本中FinalHttp模块的使用方法,包括GET、POST请求及文件上传、下载等操作,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里介绍了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. });
FinalHttp fh = new FinalHttp();
fh.get("http://www.yangfuhai.com", new AjaxCallBack<String>(){

	@Override
	public void onLoading(long count, long current) { //每1秒钟自动被回调一次
			textView.setText(current+"/"+count);
	}

	@Override
	public void onSuccess(String t) {
			textView.setText(t==null?"null":t);
	}

	@Override
	public void onStart() {
		//开始http请求的时候回调
	}

	@Override
	public void onFailure(Throwable t, String strMsg) {
		//加载失败的时候回调
	}
});


  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的频率
FinalHttp fh = new FinalHttp();
fh.get("http://www.yangfuhai.com", new AjaxCallBack<String>(){

	@Override
	public void onLoading(long count, long current) { //每5秒钟自动被回调一次,通过progress是否回调onLoading和回调频率
		textView.setText(current+"/"+count);
	}

	@Override
	public void onSuccess(String t) {
		textView.setText(t==null?"null":t);
		}

}.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. });
 AjaxParams params = new AjaxParams();
  params.put("username", "michael yang");
  params.put("password", "123456");
  params.put("email", "test@tsz.net");
  params.put("profile_picture", new File("/mnt/sdcard/pic.jpg")); // 上传文件
  params.put("profile_picture2", inputStream); // 上传数据流
  params.put("profile_picture3", new ByteArrayInputStream(bytes)); // 提交字节流
 
  FinalHttp fh = new FinalHttp();
  fh.post("http://www.yangfuhai.com", params, new AjaxCallBack<String>(){
  		@Override
 		public void onLoading(long count, long current) {
 				textView.setText(current+"/"+count);
 		}
 
 		@Override
 		public void onSuccess(String t) {
 			textView.setText(t==null?"null":t);
 		}
  });

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. });
FinalHttp fh = new FinalHttp();
fh.download("http://www.xxx.com/下载路径/xxx.apk", "/mnt/sdcard/testapk.apk", new AjaxCallBack<File>() {
				@Override
				public void onLoading(long count, long current) {
					 textView.setText("下载进度:"+current+"/"+count);
				}

				@Override
				public void onSuccess(File t) {
					textView.setText(t==null?"null":t.getAbsoluteFile().toString());
				}

			});

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. });
BasicClientCookie bcc = new BasicClientCookie("", ""); 
        bcc.setDomain("yangfuhai.com");
        bcc.setPath("/");
        bcc.setVersion(1);
        
        PreferencesCookieStore pcs = new PreferencesCookieStore(this);
        pcs.addCookie(bcc);
        
        FinalHttp fh = new FinalHttp();
        hk.setCookieStore(pcs);
	
  fh.post("http://www.yangfuhai.com", new AjaxCallBack<String>(){
  		@Override
 		public void onLoading(long count, long current) {
 				textView.setText(current+"/"+count);
 		}
 
 		@Override
 		public void onSuccess(String t) {
 			textView.setText(t==null?"null":t);
 		}
  });

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值