android xUtils 2.6.14 jar 下载 使用 和混淆

一、ViewUtils

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

二、HttpUtils

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

三、BitmapUtils

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

四、DbUtils

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

先来看一看ViewUtils的代码:

[java] view plain copy print ?
  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. }  
//绑定布局文件
@ContentView(R.layout.activity_main)
public class MainActivity extends Activity {
     //绑定btn1的id
       @(R.id.Button01 )
       private Button btn1 ;
       @ViewInject(R.id.Button02 )
       private Button btn2 ;
       @ViewInject(R.id.button3 )
       private Button btn3 ;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             //绑定对象
            ViewUtils. inject(this);
      }
     //绑定按钮的监听器
       @OnChildClick({ R.id.Button01, R.id.Button02, R.id. button3 })
       private void onClick(View v) {
             switch (v.getId()) {
             case R.id.Button01 :
                  Toast. makeText(MainActivity.this, "Button1",Toast.LENGTH_SHORT).show();
                   break;
             case R.id.Button02 :
                  Toast. makeText(MainActivity.this, "Button2",Toast.LENGTH_SHORT).show();
                   break;
             case R.id.button3 :
                  startActivity( new Intent(MainActivity.this ,HttpActivity.class));
                   break;
             default:
                   break;
            }
      }

}


二、HttpUtils

       支持的HTTP七种请求方式,非常便捷的满足你的接口请求的需要。同时还支持大文件上传下载,以及同步异步请求
[java] view plain copy print ?
  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() {  
  26.   
  27.                @Override  
  28.                public void onSuccess(ResponseInfo 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 httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip",  
  52.                     new RequestCallBack() {  
  53.   
  54.                          @Override  
  55.                          public void onSuccess(ResponseInfo 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() {  
  106.   
  107.                @Override  
  108.                public void onSuccess(ResponseInfo 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 返回类型 
  126.      * @throws 
  127.      */  
  128.      private List getParams() {  
  129.           List list = new ArrayList();  
  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. }  
@ContentView(R.layout.activity_http)
public class HttpActivity extends ActionBarActivity {

     @ViewInject(R.id.pregress)
     private TextView pregress;
     @ViewInject(R.id.post)
     private Button post;
     @ViewInject(R.id.download)
     private Button downLoad;
     @ViewInject(R.id.upload)
     private Button upload;

     HttpUtils httpUtils;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          ViewUtils.inject(this);
          httpUtils = new HttpUtils();
          loadData();
     }

     private void loadData() {
          // 请求数据
          httpUtils.send(HttpMethod.GET, "", new RequestCallBack() {

               @Override
               public void onSuccess(ResponseInfo responseInfo) {
                    String result = responseInfo.result;
                    System.out.println(result.toString());
               }

               @Override
               public void onFailure(HttpException error, String msg) {

               }
          });
     }

     /**
     * 下载数据
     *
     * @Title: downLoad
     * @说 明:
     * @参 数:
     * @return void 返回类型
     * @throws
     */
     public void downLoad() {
          String url = "https://github.com/wyouflf/xUtils/archive/master.zip";
          HttpHandler httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip",
                    new RequestCallBack() {

                         @Override
                         public void onSuccess(ResponseInfo responseInfo) {
                              System.out.println("");
                         }

                         @Override
                         public void onFailure(HttpException error, String msg) {

                         }

                         /**
                         * 第一个参数文件的长度 , 第二 当前下载的进度 第三 判断是否是上传还是下载
                         */
                         @Override
                         public void onLoading(long total, long current, boolean isUploading) {
                              super.onLoading(total, current, isUploading);
                              pregress.setText(String.valueOf(current / total * 100));
                         }

                    });

          httpHandler.cancel();
     }

     @OnClick({ R.id.upload })
     public void onClickListener(View view) {
          switch (view.getId()) {
          case R.id.download:
               downLoad();
               break;
          case R.id.post:
               downLoad();
               break;
          case R.id.upload:
               uploadFile();
               break;
          default:
               break;
          }
     }
     
     //上传图片到服务器(也可以上传对象、String都可以)
     public void uploadFile() {
          String url = "http://10.2.108.35:8080/app/uplaodFile?method=upload";
          RequestParams params = new RequestParams();
          String path =FileUtils.getImagePath();
          File file = new File(path);
          File[] files = file.listFiles();
          for (int i = 0; i < files.length; i++) {
               params.addBodyParameter("" + i, files[i]);
          }
          httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack() {

               @Override
               public void onSuccess(ResponseInfo responseInfo) {
                    System.out.println(responseInfo.result);
               }

               @Override
               public void onFailure(HttpException error, String msg) {
                    System.out.println(msg);
               }
          });
     }

     /**
     * 获取提交的参数
     *
     * @Title: getParams
     * @说 明:
     * @参 数: @return
     * @return List 返回类型
     * @throws
     */
     private List getParams() {
          List list = new ArrayList();
          NameValuePair userName = new BasicNameValuePair("name", "test");
          list.add(userName);
          NameValuePair pswNameValuePair = new BasicNameValuePair("psw", "123456");
          list.add(pswNameValuePair);
          return list;

     }

}


三、BitmapUtils

[java] view plain copy print ?
  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() {  
  61.   
  62.                @Override  
  63.                public void onSuccess(ResponseInfo 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 {  
  109.   
  110.      private BitmapUtils bitmapUtils;  
  111.   
  112.      public MainAdapter(List list, Context context) {  
  113.           super(list, context);  
  114.      }  
  115.   
  116.      public MainAdapter(List 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. }  
第一:首先自定义application,初始化配置
public class BaseApplication extends Application {

***
* BitmpUtils BitmapGlobalConfig 配置缓存大小,储存位置 大小
*  BitmapdisplayConfig 配置图片的大小
* 以及设置默认图片,下载错误的图片
*
*
*/
     private BitmapGlobalConfig globalConfig;

     private BitmapDisplayConfig displayConfig;

     private static BaseApplication application;

     @Override
     public void onCreate() {
          super.onCreate();
          application = this;
          configBitmapParams();
     }
     //得到这个类的对象
     public static BaseApplication getApplication() {
          return application;
     }

     public void configBitmapParams() {
          /**
          * 单利模式初始化,并设置缓存的路径
          */
          globalConfig = BitmapGlobalConfig.getInstance(this, FileUtils.getAppImageCache());
          int cacheSize = (int) Runtime.getRuntime().maxMemory() / 8;
          // 设置缓存大小
          globalConfig.setMemoryCacheSize(cacheSize);

     }

     public BitmapGlobalConfig getGlobalConfig() {
          return globalConfig;
     }

     public void setGlobalConfig(BitmapGlobalConfig globalConfig) {
          this.globalConfig = globalConfig;
     }

     public BitmapDisplayConfig getDisplayConfig() {
          return displayConfig;
     }

     public void setDisplayConfig(BitmapDisplayConfig displayConfig) {
          this.displayConfig = displayConfig;
     }

}


第二步:加载数据、通知adapter更新、在adapter里面进行加载图片
private void loadData() {
          httpUtils.send(HttpMethod.GET, CommonURL.FRONT_IMAGE_TEST, new RequestCallBack() {

               @Override
               public void onSuccess(ResponseInfo responseInfo) {
                    String result = responseInfo.result;
                    try {
                         JSONObject jsonObject = new JSONObject(result);
                         JSONArray jsonArray = jsonObject.getJSONArray("items");
                         if (jsonArray != null && jsonArray.length() > 0) {
                              for (int j = 0; j < jsonArray.length(); j++) {
                                   Items items = Items.getFromJSON(jsonArray.getJSONObject(j));
                                   list.add(items);
                              }
                              adapter.notifyDataSetChanged();
                         }

                    } catch (JSONException e) {
                         e.printStackTrace();
                    }

               }

               @Override
               public void onFailure(HttpException error, String msg) {
                    LogUtils.e(msg);

               }
          });
     }


第三步

package com.qianfeng.bitmaputils.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.bitmaputils.R;
import com.qianfeng.bitmaputils.bean.Items;

public class MainAdapter extends AppBaseAdapter {

     private BitmapUtils bitmapUtils;

     public MainAdapter(List list, Context context) {
          super(list, context);
     }

     public MainAdapter(List list, Context context, BitmapUtils bitmapUtils) {
          this(list, context);
          this.bitmapUtils = bitmapUtils;
     }

     @Override
     public View createView(int position, View convertView) {
          ViewHolder vh = null;
          if (convertView == null) {
               convertView = inflater.inflate(R.layout.fm_listview_item_layout, null);
              
               vh = new ViewHolder();
               ViewUtils.inject(vh, convertView);
               convertView.setTag(vh);
          } else {
               vh = (ViewHolder) convertView.getTag();
          }

          vh.contentTv.setText(list.get(position).getContent());
          vh.otherNameTv.setText(list.get(position).getLogin());
          bitmapUtils.display(vh.headIv, list.get(position).getImage());
         
         
//          bitmapUtils.display(container, uri, displayConfig, new ),这个方法里面有个回调监听器、可在这里设置进度条相关东西
         
          return convertView;
     }

     private static class ViewHolder {
          @ViewInject(R.id.headIv)
          ImageView headIv;
          @ViewInject(R.id.otherName)
          TextView otherNameTv;
          @ViewInject(R.id.content)
          private TextView contentTv;
         
         
         

     }

}

XUtils混淆

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

第二:在proguard-project中加入:

   -libraryjars libs/xUtils-2.6.14.jar 

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


xUtils2.6.14.jar 和 source以及RecycleView-v7-24.1.1.jar 和source下载



你可能感兴趣的:(Xutils)