The use of xUtils framework

Introduction to xUtils

  • xUtils contains a lot of useful android tools. xUtils is derived from the Afinal framework. Afinal has been refactored a lot, so that xUtils supports large file upload, more comprehensive http request protocol support, and has a more flexible ORM.
  • xUitls is at least compatible with android 2.2 (api level 8).
  • The refactoring and extending framework based on aFinal is greatly improved compared to aFinal. Network-based applications, as long as they are handled properly, can completely get rid of the troubles of various tools and repetitive codes.
  •  
  • Java Reflection Technology
    1. Dynamically obtain class, interface or object information in the current Java virtual machine
    2. Decoupling between two classes, that is, in the case of not getting the dependent class, its own application can be compiled by
    3. Dynamic dependency injection (that is, dynamically generate a class instance when a certain class of objects is needed, and set it to the dependent class), reducing the memory overhead at compile time
  • There are four main modules in xUtils

ViewUtils module
  • The ioc framework in android can be fully annotated for UI, resource and event binding ;
  • The new event binding method can still work normally after obfuscation with obfuscation tools ;
  • Currently, 20 commonly used event bindings are supported, see the ViewCommonEventListener class and the package com.lidroid.xutils.view.annotation.event.

BitmapUtils module
  • When loading the bitmap, there is no need to consider the phenomenon such as the oom that occurs during the bitmap loading process and the image misalignment that occurs when the android container slides quickly;
  • Support loading network pictures and local pictures;
  • Memory management uses lru algorithm to better manage bitmap memory;
  • Configurable thread loading thread number, cache size, cache path, loading display animation, etc...

HttpUtils module

  • Support synchronous and asynchronous requests;
  • 支持大文件上传,上传大文件不会oom;
  • 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT请求;
  • 下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件;
  • 返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。

DbUtils模块

 
  • android中的orm框架,一行代码就可以进行增删改查;
  • 支持事务,默认关闭;
  • 可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名);
  • 支持绑定外键,保存实体时外键关联实体自动保存或更新;
  • 自动加载外键关联实体,支持延时加载;
  • 支持链式表达查询,更直观的查询语义

案例效果如图

 

ViewUtils使用方法

@ContentView(R.layout.activity_xutils)//加载布局文件

publicclassMainActivityextends Activity {

   // 初始化控件

   @ViewInject(R.id.btnUtil1)

   Button btn1;

   @Override

   protectedvoidonCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      ViewUtils.inject(this);//注入view和事件

   }

   @OnClick(R.id.btnUtil1)//点击事件

   publicvoidbtnClick(View v) {

      Toast.makeText(getApplicationContext(),"get",1).show();

   }

}


BitmapUtils使用方法

BitmapUtils bitmapUtils = new BitmapUtils(this);

bitmapUtils.display(mImageView,"http://pic.58pic.com/58pic/11/02/26/91F58PICpGw.jpg");


HttpUtils使用方法
  /*

    * 网络请求get

    */

   privatevoid get1() {

      HttpUtils http = newHttpUtils();

      http.send(HttpRequest.HttpMethod.GET,"http://www.lidroid.com",newRequestCallBack<String>() {

                @Override

                publicvoidonLoading(long total,long current,booleanisUploading) {

                   text1.setText(current+ "/" + total);

                }

                @Override

                publicvoidonSuccess(ResponseInfo<String> responseInfo) {

                   text1.setText(responseInfo.result);

                }

                @Override

                publicvoid onStart(){

                }

                @Override

                publicvoidonFailure(HttpException error, String msg) {

                }

            });

   }

 

   privatevoid get2() {

      // TODOAuto-generated method stub

      HttpUtils http = newHttpUtils(10000);//放回html数据和api数据--10000代表10s超时

      http.configCurrentHttpCacheExpiry(5000);//设置缓存5s,5秒内直接返回上次成功请求的结果。

      http.configResponseTextCharset("UTF-8");//配置HTTP响应的文本编码

      http.send( HttpRequest.HttpMethod.GET,

            "https://route.showapi.com/255-1?showapi_appid=11548&type=10&page=1&maxResult=20&"

              + "showapi_timestamp=" +str+ "&showapi_sign=bb1d15ab7ce646ec87cc89d684ca4bcb",

            newRequestCallBack<String>() {

                @Override

                publicvoidonLoading(long total,long current,booleanisUploading) {//更新任务进度(需覆盖重写)

                   text1.setText(current+ "/" + total);

                }

                @Override

                publicvoidonSuccess(ResponseInfo<String> responseInfo) {

                   text1.setText(responseInfo.result);//网络请求执行成功(需覆盖重写)//接口回调返回数据

                }

                @Override

                publicvoid onStart(){//开始发送网络请求(需覆盖重写)

                }

                @Override

                publicvoidonFailure(HttpException error, String msg) {

                }

            });

   }

   /***

    * post请求

    */

   privatevoid post1() {

      RequestParams params = new RequestParams("utf-8");

      params.addBodyParameter("showapi_appid","11548");

      params.addBodyParameter("showapi_sign","bb1d15ab7ce646ec87cc89d684ca4bcb");

      params.addBodyParameter("type","10");

      params.addBodyParameter("page","1");

      params.addBodyParameter("maxResult","20");

      params.addBodyParameter("showapi_timestamp",str);

      HttpUtils http = newHttpUtils();

      http.send(HttpRequest.HttpMethod.POST,"https://route.showapi.com/255-1",                         params,newRequestCallBack<String>() {

                @Override

                publicvoid onStart(){

                   text1.setText("conn...");

                }

                @Override

                publicvoidonLoading(long total,long current,booleanisUploading) {

                   if(isUploading) {

                      text1.setText("upload:" + current + "/" + total);

                   } else {

                      text1.setText("reply:" + current + "/" + total);

                   }

                }

                @Override

                publicvoidonSuccess(ResponseInfo<String> responseInfo) {

                   text1.setText("reply:" + responseInfo.result);

                }

                @Override

                publicvoidonFailure(HttpException error, String msg) {

                   text1.setText(error.getExceptionCode()+ ":" + msg);

                }

            });

   }


 

使用xUtils框架需要在AndroidManifest.xml加以下权限

 

   <uses-permissionandroid:name="android.permission.INTERNET"/>

   <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


 

由于代码太多,完整代码未给出,要源码直接下载即可

 

源码点击下载

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326269868&siteId=291194637