XUtils的使用、混淆

  xUtils 包含了很多实用的android工具。xUtils 源于Afinal框架,对Afinal进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持,拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响。同时需要注意的是,xUitls最低兼容android 2.2 (api level 8)  。今天我们的主题是整体介绍下xUtils,主要介绍它重要的四大组件。具体各个组件的使用,会在之后几天陆续为大家奉上。下面开始:

一、ViewUtils

        你受够了重复冗长的findViewById了嘛?你受够了各种监听事件的绑定了嘛?在这里,你只需要一句注解,如@ViewInject、@OnClick,就能轻松摆脱小白似的代码,大大的上了一个档次。

二、HttpUtils

       支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求。

三、BitmapUtils

       你的程序因OOM强制关闭过嘛?你在为加在网络图片头疼嘛?有了组件,你将永久摆脱前面的问题。

四、DbUtils

       简单易用又出色的ORM框架,真的是谁用谁知道,直接轻松存储各种对象到sqlite数据库中,同时也能非常方便的进行各种条件查询,甚至分页查询,还有对表中数据的更新删除等操作,真正的实现。一行代码就可以进行增删改查。并且可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等,支持事务。

    由于xUtils是基于aFinal的,这个开源框架是国内的某位大神写的,所以了解了aFinal之后再回头看xUtils,才会更有收获。同时,也要向这位大神以及众多的开源贡献者致敬,有了他们的奉献和开源的精神,才涌现出一个个耳熟能详的更加优秀的更加稳定的框架。我们众所周知的Linux就是这么诞生的。

        aFinal学习地址:http://www.afinal.org

先来看一看ViewUtils的代码:

[java]  view plain  copy
  1. //绑定布局文件  
  2. @ContentView(R.layout.activity_main)  
  3. public class MainActivity extends Activity {  
  4.      //绑定btn1的id  
  5.        @(R.id.Button01 )  
  6.        private Button btn1 ;  
  7.        @ViewInject(R.id.Button02 )  
  8.        private Button btn2 ;  
  9.        @ViewInject(R.id.button3 )  
  10.        private Button btn3 ;  
  11.   
  12.        @Override  
  13.        protected void onCreate(Bundle savedInstanceState) {  
  14.              super.onCreate(savedInstanceState);  
  15.              //绑定对象  
  16.             ViewUtils. inject(this);  
  17.       }  
  18.      //绑定按钮的监听器  
  19.        @OnChildClick({ R.id.Button01, R.id.Button02, R.id. button3 })  
  20.        private void onClick(View v) {  
  21.              switch (v.getId()) {  
  22.              case R.id.Button01 :  
  23.                   Toast. makeText(MainActivity.this"Button1",Toast.LENGTH_SHORT).show();  
  24.                    break;  
  25.              case R.id.Button02 :  
  26.                   Toast. makeText(MainActivity.this"Button2",Toast.LENGTH_SHORT).show();  
  27.                    break;  
  28.              case R.id.button3 :  
  29.                   startActivity( new Intent(MainActivity.this ,HttpActivity.class));  
  30.                    break;  
  31.              default:  
  32.                    break;  
  33.             }  
  34.       }  
  35.   
  36. }  


二、HttpUtils

       支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求
[java]  view plain  copy
  1. @ContentView(R.layout.activity_http)  
  2. public class HttpActivity extends ActionBarActivity {  
  3.   
  4.      @ViewInject(R.id.pregress)  
  5.      private TextView pregress;  
  6.      @ViewInject(R.id.post)  
  7.      private Button post;  
  8.      @ViewInject(R.id.download)  
  9.      private Button downLoad;  
  10.      @ViewInject(R.id.upload)  
  11.      private Button upload;  
  12.   
  13.      HttpUtils httpUtils;  
  14.   
  15.      @Override  
  16.      protected void onCreate(Bundle savedInstanceState) {  
  17.           super.onCreate(savedInstanceState);  
  18.           ViewUtils.inject(this);  
  19.           httpUtils = new HttpUtils();  
  20.           loadData();  
  21.      }  
  22.   
  23.      private void loadData() {  
  24.           // 请求数据  
  25.           httpUtils.send(HttpMethod.GET, ""new RequestCallBack<String>() {  
  26.   
  27.                @Override  
  28.                public void onSuccess(ResponseInfo<String> responseInfo) {  
  29.                     String result = responseInfo.result;  
  30.                     System.out.println(result.toString());  
  31.                }  
  32.   
  33.                @Override  
  34.                public void onFailure(HttpException error, String msg) {  
  35.   
  36.                }  
  37.           });  
  38.      }  
  39.   
  40.      /** 
  41.      * 下载数据 
  42.      * 
  43.      * @Title: downLoad 
  44.      * @说 明: 
  45.      * @参 数: 
  46.      * @return void 返回类型 
  47.      * @throws 
  48.      */  
  49.      public void downLoad() {  
  50.           String url = "https://github.com/wyouflf/xUtils/archive/master.zip";  
  51.           HttpHandler<File> httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip",  
  52.                     new RequestCallBack<File>() {  
  53.   
  54.                          @Override  
  55.                          public void onSuccess(ResponseInfo<File> responseInfo) {  
  56.                               System.out.println("");  
  57.                          }  
  58.   
  59.                          @Override  
  60.                          public void onFailure(HttpException error, String msg) {  
  61.   
  62.                          }  
  63.   
  64.                          /** 
  65.                          * 第一个参数文件的长度 , 第二 当前下载的进度 第三 判断是否是上传还是下载 
  66.                          */  
  67.                          @Override  
  68.                          public void onLoading(long total, long current, boolean isUploading) {  
  69.                               super.onLoading(total, current, isUploading);  
  70.                               pregress.setText(String.valueOf(current / total * 100));  
  71.                          }  
  72.   
  73.                     });  
  74.   
  75.           httpHandler.cancel();  
  76.      }  
  77.   
  78.      @OnClick({ R.id.upload })  
  79.      public void onClickListener(View view) {  
  80.           switch (view.getId()) {  
  81.           case R.id.download:  
  82.                downLoad();  
  83.                break;  
  84.           case R.id.post:  
  85.                downLoad();  
  86.                break;  
  87.           case R.id.upload:  
  88.                uploadFile();  
  89.                break;  
  90.           default:  
  91.                break;  
  92.           }  
  93.      }  
  94.        
  95.      //上传图片到服务器(也可以上传对象、String都可以)  
  96.      public void uploadFile() {  
  97.           String url = "http://10.2.108.35:8080/app/uplaodFile?method=upload";  
  98.           RequestParams params = new RequestParams();  
  99.           String path =FileUtils.getImagePath();  
  100.           File file = new File(path);  
  101.           File[] files = file.listFiles();  
  102.           for (int i = 0; i < files.length; i++) {  
  103.                params.addBodyParameter("" + i, files[i]);  
  104.           }  
  105.           httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {  
  106.   
  107.                @Override  
  108.                public void onSuccess(ResponseInfo<String> responseInfo) {  
  109.                     System.out.println(responseInfo.result);  
  110.                }  
  111.   
  112.                @Override  
  113.                public void onFailure(HttpException error, String msg) {  
  114.                     System.out.println(msg);  
  115.                }  
  116.           });  
  117.      }  
  118.   
  119.      /** 
  120.      * 获取提交的参数 
  121.      * 
  122.      * @Title: getParams 
  123.      * @说 明: 
  124.      * @参 数: @return 
  125.      * @return List<NameValuePair> 返回类型 
  126.      * @throws 
  127.      */  
  128.      private List<NameValuePair> getParams() {  
  129.           List<NameValuePair> list = new ArrayList<NameValuePair>();  
  130.           NameValuePair userName = new BasicNameValuePair("name""test");  
  131.           list.add(userName);  
  132.           NameValuePair pswNameValuePair = new BasicNameValuePair("psw""123456");  
  133.           list.add(pswNameValuePair);  
  134.           return list;  
  135.   
  136.      }  
  137.   
  138. }  


三、BitmapUtils

[java]  view plain  copy
  1. 第一:首先自定义application,初始化配置  
  2. public class BaseApplication extends Application {  
  3.   
  4. ***  
  5. * BitmpUtils BitmapGlobalConfig 配置缓存大小,储存位置 大小  
  6. *  BitmapdisplayConfig 配置图片的大小  
  7. * 以及设置默认图片,下载错误的图片  
  8. *  
  9. *  
  10. */  
  11.      private BitmapGlobalConfig globalConfig;  
  12.   
  13.      private BitmapDisplayConfig displayConfig;  
  14.   
  15.      private static BaseApplication application;  
  16.   
  17.      @Override  
  18.      public void onCreate() {  
  19.           super.onCreate();  
  20.           application = this;  
  21.           configBitmapParams();  
  22.      }  
  23.      //得到这个类的对象  
  24.      public static BaseApplication getApplication() {  
  25.           return application;  
  26.      }  
  27.   
  28.      public void configBitmapParams() {  
  29.           /** 
  30.           * 单利模式初始化,并设置缓存的路径 
  31.           */  
  32.           globalConfig = BitmapGlobalConfig.getInstance(this, FileUtils.getAppImageCache());  
  33.           int cacheSize = (int) Runtime.getRuntime().maxMemory() / 8;  
  34.           // 设置缓存大小  
  35.           globalConfig.setMemoryCacheSize(cacheSize);  
  36.   
  37.      }  
  38.   
  39.      public BitmapGlobalConfig getGlobalConfig() {  
  40.           return globalConfig;  
  41.      }  
  42.   
  43.      public void setGlobalConfig(BitmapGlobalConfig globalConfig) {  
  44.           this.globalConfig = globalConfig;  
  45.      }  
  46.   
  47.      public BitmapDisplayConfig getDisplayConfig() {  
  48.           return displayConfig;  
  49.      }  
  50.   
  51.      public void setDisplayConfig(BitmapDisplayConfig displayConfig) {  
  52.           this.displayConfig = displayConfig;  
  53.      }  
  54.   
  55. }  
  56.   
  57.   
  58. 第二步:加载数据、通知adapter更新、在adapter里面进行加载图片  
  59. private void loadData() {  
  60.           httpUtils.send(HttpMethod.GET, CommonURL.FRONT_IMAGE_TEST, new RequestCallBack<String>() {  
  61.   
  62.                @Override  
  63.                public void onSuccess(ResponseInfo<String> responseInfo) {  
  64.                     String result = responseInfo.result;  
  65.                     try {  
  66.                          JSONObject jsonObject = new JSONObject(result);  
  67.                          JSONArray jsonArray = jsonObject.getJSONArray("items");  
  68.                          if (jsonArray != null && jsonArray.length() > 0) {  
  69.                               for (int j = 0; j < jsonArray.length(); j++) {  
  70.                                    Items items = Items.getFromJSON(jsonArray.getJSONObject(j));  
  71.                                    list.add(items);  
  72.                               }  
  73.                               adapter.notifyDataSetChanged();  
  74.                          }  
  75.   
  76.                     } catch (JSONException e) {  
  77.                          e.printStackTrace();  
  78.                     }  
  79.   
  80.                }  
  81.   
  82.                @Override  
  83.                public void onFailure(HttpException error, String msg) {  
  84.                     LogUtils.e(msg);  
  85.   
  86.                }  
  87.           });  
  88.      }  
  89.   
  90.   
  91. 第三步  
  92.   
  93. package com.qianfeng.bitmaputils.adapter;  
  94.   
  95. import java.util.List;  
  96.   
  97. import android.content.Context;  
  98. import android.view.View;  
  99. import android.widget.ImageView;  
  100. import android.widget.TextView;  
  101.   
  102. import com.lidroid.xutils.BitmapUtils;  
  103. import com.lidroid.xutils.ViewUtils;  
  104. import com.lidroid.xutils.view.annotation.ViewInject;  
  105. import com.qianfeng.bitmaputils.R;  
  106. import com.qianfeng.bitmaputils.bean.Items;  
  107.   
  108. public class MainAdapter extends AppBaseAdapter<Items> {  
  109.   
  110.      private BitmapUtils bitmapUtils;  
  111.   
  112.      public MainAdapter(List<Items> list, Context context) {  
  113.           super(list, context);  
  114.      }  
  115.   
  116.      public MainAdapter(List<Items> list, Context context, BitmapUtils bitmapUtils) {  
  117.           this(list, context);  
  118.           this.bitmapUtils = bitmapUtils;  
  119.      }  
  120.   
  121.      @Override  
  122.      public View createView(int position, View convertView) {  
  123.           ViewHolder vh = null;  
  124.           if (convertView == null) {  
  125.                convertView = inflater.inflate(R.layout.fm_listview_item_layout, null);  
  126.                 
  127.                vh = new ViewHolder();  
  128.                ViewUtils.inject(vh, convertView);  
  129.                convertView.setTag(vh);  
  130.           } else {  
  131.                vh = (ViewHolder) convertView.getTag();  
  132.           }  
  133.   
  134.           vh.contentTv.setText(list.get(position).getContent());  
  135.           vh.otherNameTv.setText(list.get(position).getLogin());  
  136.           bitmapUtils.display(vh.headIv, list.get(position).getImage());  
  137.            
  138.            
  139. //          bitmapUtils.display(container, uri, displayConfig, new ),这个方法里面有个回调监听器、可在这里设置进度条相关东西  
  140.            
  141.           return convertView;  
  142.      }  
  143.   
  144.      private static class ViewHolder {  
  145.           @ViewInject(R.id.headIv)  
  146.           ImageView headIv;  
  147.           @ViewInject(R.id.otherName)  
  148.           TextView otherNameTv;  
  149.           @ViewInject(R.id.content)  
  150.           private TextView contentTv;  
  151.            
  152.            
  153.            
  154.   
  155.      }  
  156.   
  157. }  

XUtils混淆

第一:使用了ViewUtil的注解的监听事件,访问修饰符必须是public,否则混淆打包之后监听无效。

第二:在proguard-project中加入: -libraryjars libs/xUtils-2.6.14.jar 

   -keep class com.lidroid.** { *; } 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值