码迷,mamicode.com
首页 > 移动开发 > 详细

【Android实战】Afinal框架的使用大全案例

时间:2015-02-20 23:13:24      阅读:551      评论:0      收藏:0      [点我收藏+]

标签:android   框架   afinal   网络   通信   

Afinal这个超级强大框架,可以满足下面功能,并且基本覆盖了手机应用中的核心部分

数据库(几句话就能实现增删改查)

注解 (省去findById和setOnclickListener)

网络通信(get请求 / post请求 / 文件上传 / 文件下载)

图片获取(一句话解决)


下面是实现的例子,并且放上服务器的代码:

public class MainAct extends FinalActivity {

	@ViewInject(id = R.id.aBt, click = "dbClick")//实现注解
	Button abButton;
	@ViewInject(id = R.id.getBt, click = "getClick")
	Button getButton;
	@ViewInject(id = R.id.postBt, click = "postClick")
	Button postButton;
	@ViewInject(id = R.id.tv)
	TextView textView;
	@ViewInject(id = R.id.fileBt, click = "fileClick")
	Button fileButton;
	@ViewInject(id = R.id.fileBt2, click = "fileClick2")
	Button fileButton2;
	@ViewInject(id = R.id.picBt, click = "picClick")
	Button picButton;
	@ViewInject(id = R.id.img)
	ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.afinallay);

	}

	public void picClick(View v) {
		Toast.makeText(getApplicationContext(), "in", 2000).show();
		FinalBitmap finalBitmap = FinalBitmap.create(this);
		finalBitmap
				.display(
						imageView,
						"http://e.hiphotos.baidu.com/image/pic/item/b999a9014c086e06d1925b1300087bf40ad1cbb4.jpg");

	}

	public void fileClick2(View v) {//文件下载
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		try {

			HttpHandler<File> handler = finalHttp.download(
					"http://192.168.191.1:8080/androidtest/a.doc",
					Environment.getExternalStorageDirectory() + "/im.doc", true,
					new AjaxCallBack<File>() {
						@Override
						public void onSuccess(File t) {
							// TODO Auto-generated method stub

							textView.setText("File down Success");
						}
					});

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void fileClick(View v) {//文件上传
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		try {
			ajaxParams.put("file",
					new File(Environment.getExternalStorageDirectory()
							+ "/n.jpg"));
			finalHttp.post("http://192.168.191.1:8080/androidtest/test3",
					ajaxParams, new AjaxCallBack<Object>() {
						@Override
						public void onSuccess(Object t) {
							// TODO Auto-generated method stub
							textView.setText((t == null) ? "null" : (String) t);
						}
					});

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void getClick(View v) {    //get请求

		FinalHttp finalHttp = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("id", "1");
		params.put("name", "duerGet");
		params.put("age", "521");
		finalHttp.get("http://192.168.191.1:8080/androidtest/test2", params,
				new AjaxCallBack<Object>() {

					@Override
					public void onSuccess(Object t) {
						// TODO Auto-generated method stub
						if (textView == null)
							Toast.makeText(getApplicationContext(),
									"(textView==null)", 2000).show();
						else
							textView.setText((t == null) ? "null" : (String) t);
					}
				});

	}

	public void postClick(View v) {    //post请求

		FinalHttp finalHttp = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("name", "duerPost");
		params.put("pass", "125");
		finalHttp.post("http://192.168.191.1:8080/androidtest/test2", params,
				new AjaxCallBack<Object>() {
					@Override
					public void onSuccess(Object t) {
						// TODO Auto-generated method stub
						textView.setText((t == null) ? "null" : (String) t);
					}
				});

	}

	public void dbClick(View v) {//创建数据库 在bean前加@Table(name="tablename")实现自定义表名
		Toast.makeText(getApplicationContext(), "in", 2000).show();
		FinalDb finalDb = FinalDb.create(this, "UserDB");
		UserBean bean1 = new UserBean();
		bean1.setAge("12");
		bean1.setName("duzi");
		finalDb.save(bean1);

		UserBean bean2 = new UserBean();
		bean2.setAge("12");
		bean2.setName("duzi");
		finalDb.save(bean2);
	}
}

服务器上传接收代码:

public class FileUpload extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean isUpload = ServletFileUpload.isMultipartContent(request);
		if (isUpload) {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			factory.setSizeThreshold(10);
			factory.setRepository(new File("E:\\upload"));
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setSizeMax(10);
			ServletFileUpload sfu = new ServletFileUpload(factory);
			try {
				List<FileItem> DiskFileItems = sfu.parseRequest(request);
				for (FileItem item : DiskFileItems) {
					System.out.println("-------------key:"
							+ item.getFieldName());
					System.out.println(item.getName());
					File myFile=new File("E:\\upload\\"+item.getName());
					try {
						item.write(myFile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// System.out.println("-------------value:"+
					// item.getString());
					// System.out.println("-------------inputSream:"+
					// item.getInputStream());
				}
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
		}

		response.getWriter().print("okok!");
	}
}



【Android实战】Afinal框架的使用大全案例

标签:android   框架   afinal   网络   通信   

原文地址:http://blog.csdn.net/scboyhj__/article/details/43890923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!