[Android practice] Use Case Of The Afinal framework, androidafinal
Afinal is a super powerful framework that can meet the following features and covers the core parts of mobile apps.
Database (add, delete, modify, and query can be implemented in a few words)
Annotation (saving the findById and setOnclickListener)
Network Communication (get request/post request/file upload/File Download)
Image Retrieval (one-sentence solution)
The following is an example of implementation and the server code is put:
Public class MainAct extends FinalActivity {@ ViewInject (id = R. id. aBt, click = "dbClick") // implement annotation 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 = "fileCl Ick2 ") Button fileButton2; @ ViewInject (id = R. id. picBt, click = "picClick") Button picButton; @ ViewInject (id = R. id. img) ImageView imageView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. afinallay);} public void picClick (View v) {Toast. makeText (getApplicationContext (), "in", 2000 ). show (); FinalBitm Ap finalBitmap = FinalBitmap. create (this); finalBitmap. display (imageView, "http://e.hiphotos.baidu.com/image/pic/item/b999a9014c086e06d1925b1300087bf40ad1cbb4.jpg");} public void fileClick2 (View v) {// File Download FinalHttp finalHttp = new FinalHttp (); AjaxParams ajaxParams = new AjaxParams (); try {HttpHandler <File> handler = finalHttp. download ("http: // 192.168.191.1: 8080/androidtest/a.doc", Environment. getExtern AlStorageDirectory () + "/im.doc", true, new AjaxCallBack <File> () {@ Overridepublic void onSuccess (File t) {// TODO Auto-generated method stubtextView. setText ("File down Success") ;}});} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public void fileClick (View v) {// File Upload FinalHttp finalHttp = new FinalHttp (); AjaxParams ajaxParams = new AjaxParams (); try {ajaxParams. put ("fil E ", new File (Environment. getExternalStorageDirectory () + "/n.jpg"); finalHttp. post ("http: // 192.168.191.1: 8080/androidtest/test3", ajaxParams, new AjaxCallBack <Object> () {@ Overridepublic void onSuccess (Object t) {// TODO Auto-generated method stubtextView. setText (t = null )? "Null": (String) t) ;}}) ;}catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public void getClick (View v) {// get request 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> () {@ Overridepublic void onSuccess (Object t) {// TODO Auto-generated method stubif (textView = null) Toast. makeText (getApplicationContext (), "(textView = null)", 2000 ). show (); elsetextView. setText (t = null )? "Null": (String) t) ;}}) ;}public void postClick (View v) {// post request FinalHttp finalHttp = new FinalHttp (); ajaxParams params = new AjaxParams (); params. put ("name", "duerPost"); params. put ("passed", "125"); finalHttp. post ("http: // 192.168.191.1: 8080/androidtest/test2", params, new AjaxCallBack <Object> () {@ Overridepublic void onSuccess (Object t) {// TODO Auto-generated method stubtextView. setText (t = null)? "Null": (String) t) ;}}) ;}public void dbClick (View v) {// create a database and add @ Table (name = "tablename") to the bean ") implement custom table name 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 );}}
The server uploads and receives the Code:
public class FileUpload extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubboolean 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 blocke.printStackTrace();}// System.out.println("-------------value:"+// item.getString());// System.out.println("-------------inputSream:"+// item.getInputStream());}} catch (FileUploadException e) {e.printStackTrace();}}response.getWriter().print("okok!");}}