【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!");
	}
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。