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. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. //绑定对象
  15. ViewUtils. inject(this);
  16. }
  17. //绑定按钮的监听器
  18. @OnChildClick({ R.id.Button01, R.id.Button02, R.id. button3 })
  19. private void onClick(View v) {
  20. switch (v.getId()) {
  21. case R.id.Button01 :
  22. Toast. makeText(MainActivity.this, "Button1",Toast.LENGTH_SHORT).show();
  23. break;
  24. case R.id.Button02 :
  25. Toast. makeText(MainActivity.this, "Button2",Toast.LENGTH_SHORT).show();
  26. break;
  27. case R.id.button3 :
  28. startActivity( new Intent(MainActivity.this ,HttpActivity.class));
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. }
//绑定布局文件 @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. @ViewInject(R.id.pregress)
  4. private TextView pregress;
  5. @ViewInject(R.id.post)
  6. private Button post;
  7. @ViewInject(R.id.download)
  8. private Button downLoad;
  9. @ViewInject(R.id.upload)
  10. private Button upload;
  11. HttpUtils httpUtils;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. ViewUtils.inject(this);
  16. httpUtils = new HttpUtils();
  17. loadData();
  18. }
  19. private void loadData() {
  20. // 请求数据
  21. httpUtils.send(HttpMethod.GET, "", new RequestCallBack() {
  22. @Override
  23. public void onSuccess(ResponseInfo responseInfo) {
  24. String result = responseInfo.result;
  25. System.out.println(result.toString());
  26. }
  27. @Override
  28. public void onFailure(HttpException error, String msg) {
  29. }
  30. });
  31. }
  32. /**
  33. * 下载数据
  34. *
  35. * @Title: downLoad
  36. * @说 明:
  37. * @参 数:
  38. * @return void 返回类型
  39. * @throws
  40. */
  41. public void downLoad() {
  42. String url = "https://github.com/wyouflf/xUtils/archive/master.zip";
  43. HttpHandler httpHandler = httpUtils.download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "master.zip",
  44. new RequestCallBack() {
  45. @Override
  46. public void onSuccess(ResponseInfo responseInfo) {
  47. System.out.println("");
  48. }
  49. @Override
  50. public void onFailure(HttpException error, String msg) {
  51. }
  52. /**
  53. * 第一个参数文件的长度 , 第二 当前下载的进度 第三 判断是否是上传还是下载
  54. */
  55. @Override
  56. public void onLoading(long total, long current, boolean isUploading) {
  57. super.onLoading(total, current, isUploading);
  58. pregress.setText(String.valueOf(current / total * 100));
  59. }
  60. });
  61. httpHandler.cancel();
  62. }
  63. @OnClick({ R.id.upload })
  64. public void onClickListener(View view) {
  65. switch (view.getId()) {
  66. case R.id.download:
  67. downLoad();
  68. break;
  69. case R.id.post:
  70. downLoad();
  71. break;
  72. case R.id.upload:
  73. uploadFile();
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. //上传图片到服务器(也可以上传对象、String都可以)
  80. public void uploadFile() {
  81. String url = "http://10.2.108.35:8080/app/uplaodFile?method=upload";
  82. RequestParams params = new RequestParams();
  83. String path =FileUtils.getImagePath();
  84. File file = new File(path);
  85. File[] files = file.listFiles();
  86. for (int i = 0; i < files.length; i++) {
  87. params.addBodyParameter("" + i, files[i]);
  88. }
  89. httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack() {
  90. @Override
  91. public void onSuccess(ResponseInfo responseInfo) {
  92. System.out.println(responseInfo.result);
  93. }
  94. @Override
  95. public void onFailure(HttpException error, String msg) {
  96. System.out.println(msg);
  97. }
  98. });
  99. }
  100. /**
  101. * 获取提交的参数
  102. *
  103. * @Title: getParams
  104. * @说 明:
  105. * @参 数: @return
  106. * @return List 返回类型
  107. * @throws
  108. */
  109. private List getParams() {
  110. List list = new ArrayList();
  111. NameValuePair userName = new BasicNameValuePair("name", "test");
  112. list.add(userName);
  113. NameValuePair pswNameValuePair = new BasicNameValuePair("psw", "123456");
  114. list.add(pswNameValuePair);
  115. return list;
  116. }
  117. }
@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. * BitmpUtils BitmapGlobalConfig 配置缓存大小,储存位置 大小
  5. *BitmapdisplayConfig 配置图片的大小
  6. * 以及设置默认图片,下载错误的图片
  7. *
  8. *
  9. */
  10. private BitmapGlobalConfig globalConfig;
  11. private BitmapDisplayConfig displayConfig;
  12. private static BaseApplication application;
  13. @Override
  14. public void onCreate() {
  15. super.onCreate();
  16. application = this;
  17. configBitmapParams();
  18. }
  19. //得到这个类的对象
  20. public static BaseApplication getApplication() {
  21. return application;
  22. }
  23. public void configBitmapParams() {
  24. /**
  25. * 单利模式初始化,并设置缓存的路径
  26. */
  27. globalConfig = BitmapGlobalConfig.getInstance(this, FileUtils.getAppImageCache());
  28. int cacheSize = (int) Runtime.getRuntime().maxMemory() / 8;
  29. // 设置缓存大小
  30. globalConfig.setMemoryCacheSize(cacheSize);
  31. }
  32. public BitmapGlobalConfig getGlobalConfig() {
  33. return globalConfig;
  34. }
  35. public void setGlobalConfig(BitmapGlobalConfig globalConfig) {
  36. this.globalConfig = globalConfig;
  37. }
  38. public BitmapDisplayConfig getDisplayConfig() {
  39. return displayConfig;
  40. }
  41. public void setDisplayConfig(BitmapDisplayConfig displayConfig) {
  42. this.displayConfig = displayConfig;
  43. }
  44. }
  45. 第二步:加载数据、通知adapter更新、在adapter里面进行加载图片
  46. private void loadData() {
  47. httpUtils.send(HttpMethod.GET, CommonURL.FRONT_IMAGE_TEST, new RequestCallBack() {
  48. @Override
  49. public void onSuccess(ResponseInfo responseInfo) {
  50. String result = responseInfo.result;
  51. try {
  52. JSONObject jsonObject = new JSONObject(result);
  53. JSONArray jsonArray = jsonObject.getJSONArray("items");
  54. if (jsonArray != null && jsonArray.length() > 0) {
  55. for (int j = 0; j < jsonArray.length(); j++) {
  56. Items items = Items.getFromJSON(jsonArray.getJSONObject(j));
  57. list.add(items);
  58. }
  59. adapter.notifyDataSetChanged();
  60. }
  61. } catch (JSONException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. @Override
  66. public void onFailure(HttpException error, String msg) {
  67. LogUtils.e(msg);
  68. }
  69. });
  70. }
  71. 第三步
  72. package com.qianfeng.bitmaputils.adapter;
  73. import java.util.List;
  74. import android.content.Context;
  75. import android.view.View;
  76. import android.widget.ImageView;
  77. import android.widget.TextView;
  78. import com.lidroid.xutils.BitmapUtils;
  79. import com.lidroid.xutils.ViewUtils;
  80. import com.lidroid.xutils.view.annotation.ViewInject;
  81. import com.qianfeng.bitmaputils.R;
  82. import com.qianfeng.bitmaputils.bean.Items;
  83. public class MainAdapter extends AppBaseAdapter {
  84. private BitmapUtils bitmapUtils;
  85. public MainAdapter(List list, Context context) {
  86. super(list, context);
  87. }
  88. public MainAdapter(List list, Context context, BitmapUtils bitmapUtils) {
  89. this(list, context);
  90. this.bitmapUtils = bitmapUtils;
  91. }
  92. @Override
  93. public View createView(int position, View convertView) {
  94. ViewHolder vh = null;
  95. if (convertView == null) {
  96. convertView = inflater.inflate(R.layout.fm_listview_item_layout, null);
  97. vh = new ViewHolder();
  98. ViewUtils.inject(vh, convertView);
  99. convertView.setTag(vh);
  100. } else {
  101. vh = (ViewHolder) convertView.getTag();
  102. }
  103. vh.contentTv.setText(list.get(position).getContent());
  104. vh.otherNameTv.setText(list.get(position).getLogin());
  105. bitmapUtils.display(vh.headIv, list.get(position).getImage());
  106. //bitmapUtils.display(container, uri, displayConfig, new ),这个方法里面有个回调监听器、可在这里设置进度条相关东西
  107. return convertView;
  108. }
  109. private static class ViewHolder {
  110. @ViewInject(R.id.headIv)
  111. ImageView headIv;
  112. @ViewInject(R.id.otherName)
  113. TextView otherNameTv;
  114. @ViewInject(R.id.content)
  115. private TextView contentTv;
  116. }
  117. }
第一:首先自定义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下载
【android xUtils 2.6.14 jar 下载 使用 和混淆】



    推荐阅读