• Xutils


    实体类  用ViewUtils附加了注解  DbUtils可以直接创建数据库根据实体类的注解创建数据库的表

     1 package com.qianfeng.xutilsshow.model;
     2 
     3 import com.lidroid.xutils.db.annotation.Column;
     4 import com.lidroid.xutils.db.annotation.Id;
     5 import com.lidroid.xutils.db.annotation.Table;
     6 
     7 
     8 /**
     9  * Created with IntelliJ IDEA.
    10  * User: vhly[FR]
    11  * Date: 15/10/9
    12  * Email: vhly@163.com
    13  */
    14 
    15 @Table(name = "books")
    16 public class Book {
    17 
    18     @Id   // _id integer primary key autoincrement
    19     private long _id;
    20 
    21     @Column
    22     private String title;
    23 
    24     @Column
    25     private float price;
    26 
    27     @Column
    28     private String author;
    29 
    30     public long getId() {
    31         return _id;
    32     }
    33 
    34     public void setId(long id) {
    35         this._id = id;
    36     }
    37 
    38     public String getTitle() {
    39         return title;
    40     }
    41 
    42     public void setTitle(String title) {
    43         this.title = title;
    44     }
    45 
    46     public float getPrice() {
    47         return price;
    48     }
    49 
    50     public void setPrice(float price) {
    51         this.price = price;
    52     }
    53 
    54     public String getAuthor() {
    55         return author;
    56     }
    57 
    58     public void setAuthor(String author) {
    59         this.author = author;
    60     }
    61 }
    Book

    HttpUtils

      1 package com.bi.BitmapUtils;
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 import android.os.Environment;
      6 import android.view.View;
      7 import android.widget.ProgressBar;
      8 import android.widget.TextView;
      9 import android.widget.Toast;
     10 import com.lidroid.xutils.HttpUtils;
     11 import com.lidroid.xutils.ViewUtils;
     12 import com.lidroid.xutils.exception.HttpException;
     13 import com.lidroid.xutils.http.HttpHandler;
     14 import com.lidroid.xutils.http.RequestParams;
     15 import com.lidroid.xutils.http.ResponseInfo;
     16 import com.lidroid.xutils.http.ResponseStream;
     17 import com.lidroid.xutils.http.callback.RequestCallBack;
     18 import com.lidroid.xutils.http.client.HttpRequest;
     19 import com.lidroid.xutils.view.annotation.ViewInject;
     20 import com.lidroid.xutils.view.annotation.event.OnClick;
     21 
     22 import java.io.File;
     23 import java.io.IOException;
     24 
     25 /**
     26  * Created
     27  * Author:bi[FR]
     28  * Date:15-10-10
     29  */
     30 public class HttpActivity extends Activity {
     31 
     32     @ViewInject(R.id.tv)
     33     private TextView tv;
     34 
     35     @ViewInject(R.id.progressBar)
     36     private ProgressBar progressBar;
     37     @Override
     38     public void onCreate(Bundle savedInstanceState) {
     39     super.onCreate(savedInstanceState);
     40     setContentView(R.layout.http_main);
     41 
     42     ViewUtils.inject(this);
     43 }
     44 
     45     @OnClick(R.id.btn)
     46     public void btnRequestOnclick(View v)
     47     {
     48         //异步
     49         //参数一  联网超时时间  2 联网的useragent 可以随意写
     50         HttpUtils httpUtils=new HttpUtils(5000,"Zhang Sir's App[Aandroid]");
     51         httpUtils.send(HttpRequest.HttpMethod.GET
     52                 , "http://www.baidu.com",
     53                 new RequestCallBack<String>() {
     54                     @Override
     55                     public void onSuccess(ResponseInfo<String> responseInfo) {
     56                         //更新ui
     57                         String str = responseInfo.result;
     58                         tv.setText(str);
     59                     }
     60 
     61                     @Override
     62                     public void onFailure(HttpException error, String msg) {
     63 
     64                     }
     65                 });
     66 
     67         //同步请求   应该会提示不能在主线程中耗时操作(此处不报错是因为使用了ViewInject会产生异步)
     68         try {
     69             ResponseStream stream=httpUtils.sendSync(HttpRequest.HttpMethod.GET, "http://www.163.com");
     70             int statusCode=stream.getStatusCode();
     71         if(statusCode==200)
     72         {
     73             //读写
     74             byte[]buf=new byte[128];
     75             int len=0;
     76             while((len=stream.read(buf))!=-1)
     77             {
     78 
     79             }
     80         }
     81         stream.close();
     82         } catch (HttpException e) {
     83             e.printStackTrace();
     84         } catch (IOException e) {
     85             e.printStackTrace();
     86         }
     87 
     88 
     89     }
     90 
     91     @OnClick(R.id.btn_post)
     92     public void postOnClick(View v)
     93     {
     94     HttpUtils httpUtils=new HttpUtils(5000);
     95         //!!!指定字符编码  服务器需要什么编码客户端就设置什么编码
     96         RequestParams params=new RequestParams("utf-8");
     97         //1、设置参数 POST
     98        // params.addQueryStringParameter("a","123");
     99         //post请求大部分都要使用BodyParameter
    100         params.addBodyParameter("name","admin");
    101         params.addBodyParameter("password","123456");
    102         params.addBodyParameter("xx", "yy");
    103         //如果需要附加Http头字段
    104         params.addHeader("User-Agent","Zhang Sir's Client");
    105         httpUtils.send(HttpRequest.HttpMethod.POST,
    106                 "http://10.10.163.24:8080/post", params,
    107                 new RequestCallBack<Object>() {
    108                     @Override
    109                     public void onSuccess(ResponseInfo<Object> responseInfo) {
    110                         tv.setText(responseInfo.result.toString());
    111                     }
    112                     @Override
    113                     public void onFailure(HttpException error, String msg) {
    114 
    115                     }
    116                 });
    117     }
    118 
    119     @OnClick(R.id.btn_download)
    120     public void download(View v)
    121     {
    122         HttpUtils httpUtils=new HttpUtils(5000);
    123         String state= Environment.getExternalStorageState();
    124         if(state.equals(Environment.MEDIA_MOUNTED)) {
    125             //1、下载文件
    126             File root=Environment.getExternalStorageDirectory();
    127             File  targetFile=new File(root,"xmlyjiekou.apk");
    128             //2 开始文件下载
    129             HttpHandler<File> fileHttpHandler = httpUtils.download("http://10.10.163.24:8080/ximalayatingshu_64.apk", targetFile.getAbsolutePath(), true, true,
    130                     new RequestCallBack<File>() {
    131                         @Override
    132                         public void onSuccess(ResponseInfo<File> responseInfo) {
    133                             Toast.makeText(HttpActivity.this,"chenggong",Toast.LENGTH_SHORT).show();
    134                         }
    135 
    136                         @Override
    137                         public void onFailure(HttpException error, String msg) {
    138                             Toast.makeText(HttpActivity.this,"shibai",Toast.LENGTH_SHORT).show();
    139                         }
    140                         //-------------当前网络请求:上传、下载的进度更新回调
    141 
    142                         @Override
    143                         public void onLoading(long total, long current, boolean isUploading) {
    144                             super.onLoading(total, current, isUploading);
    145                             progressBar.setMax((int) total);
    146                             progressBar.setProgress((int)current);
    147                         }
    148                     });
    149 
    150         }
    151         else   Toast.makeText(HttpActivity.this,"no sd",Toast.LENGTH_SHORT).show();
    152     }
    153 
    154 }
    httpUtils

    BitmapUtils

     1 package com.bi.BitmapUtils;
     2 
     3 import android.app.Activity;
     4 import android.content.res.Resources;
     5 import android.graphics.Bitmap;
     6 import android.os.Bundle;
     7 import android.view.animation.Animation;
     8 import android.view.animation.AnimationUtils;
     9 import android.widget.ImageView;
    10 import com.lidroid.xutils.BitmapUtils;
    11 import com.lidroid.xutils.ViewUtils;
    12 import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
    13 import com.lidroid.xutils.bitmap.core.BitmapSize;
    14 import com.lidroid.xutils.view.ResType;
    15 import com.lidroid.xutils.view.annotation.ResInject;
    16 //加载图片  缓存
    17 public class MyActivity extends Activity {
    18     /**
    19      * Called when the activity is first created.
    20      */
    21     @ResInject(id=R.anim.anim_translate,type= ResType.Animation)
    22     private Animation animation;
    23     private BitmapUtils bitmapUtils;
    24 
    25 
    26     @Override
    27     public void onCreate(Bundle savedInstanceState) {
    28         super.onCreate(savedInstanceState);
    29         setContentView(R.layout.main);
    30 
    31         ViewUtils.inject(this);
    32         //创建BitmapUtils 工具实例 可以认为是一个ImageLoader
    33         //构造方法有很多,包含文件缓存配置,避免内存溢出、提高图片加载效率减少用户流量
    34         ImageView imageView= (ImageView) findViewById(R.id.imageView);
    35         bitmapUtils = new BitmapUtils(this);
    36         BitmapDisplayConfig displayConfig=new BitmapDisplayConfig();
    37 
    38         Resources resource=getResources();
    39         displayConfig.setLoadingDrawable(resource.getDrawable(R.drawable.ic_launcher));
    40         displayConfig.setLoadFailedDrawable(resource.getDrawable(android.R.drawable.ic_delete));
    41         displayConfig.setBitmapConfig(Bitmap.Config.RGB_565);
    42         displayConfig.setBitmapMaxSize(new BitmapSize(100, 50));
    43         displayConfig.setAnimation(animation);
    44 
    45         bitmapUtils.display(imageView, "http://bbs.lidroid.com/static/image/common/logo.png"
    46                 , displayConfig
    47         );
    48 
    49     }
    50 
    51     /**
    52      * 初始化默认的图片加载设置
    53      *
    54      */
    55     private void initBitmapUtils(){
    56        bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
    57        bitmapUtils.configDefaultLoadFailedImage(android.R.drawable.ic_delete);
    58        bitmapUtils.configDefaultBitmapMaxSize(new BitmapSize(100,50));
    59         bitmapUtils.configDefaultConnectTimeout(2000);
    60     }
    61 
    62 }
    BitmapUtils

    ViewUtils

      1 package com.qianfeng.xutilsshow;
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 import android.util.Log;
      6 import android.view.View;
      7 import android.widget.Button;
      8 import android.widget.CompoundButton;
      9 import android.widget.TextView;
     10 import android.widget.Toast;
     11 import com.lidroid.xutils.ViewUtils;
     12 import com.lidroid.xutils.view.ResType;
     13 import com.lidroid.xutils.view.annotation.ContentView;
     14 import com.lidroid.xutils.view.annotation.ResInject;
     15 import com.lidroid.xutils.view.annotation.ViewInject;
     16 import com.lidroid.xutils.view.annotation.event.OnClick;
     17 import com.lidroid.xutils.view.annotation.event.OnCompoundButtonCheckedChange;
     18 import com.qianfeng.xutilsshow.annotations.CodeAuthor;
     19 
     20 import java.lang.reflect.Field;
     21 
     22 @ContentView(R.layout.main)  // activity.setContentView(R.layout.main)
     23 public class MainActivity extends Activity {
     24 
     25     @ViewInject(R.id.main_txt_info)  // textView = (TextView)findViewById(R.id.main_txt_info)
     26     private TextView textView;
     27 
     28     @ViewInject(R.id.btn_click)
     29     private Button btn;
     30 
     31     // 相当于 string 包含了  CodeAuthor 注解内容
     32     @CodeAuthor(R.id.chk_test)
     33     private String string;
     34 
     35     @ResInject(id = R.string.app_name, type = ResType.String)
     36     private String appName;
     37 
     38     /**
     39      * Called when the activity is first created.
     40      *
     41      */
     42     @Override
     43     public void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45 
     46         // 需要使用 ViewUtils 的功能,就要调用一个方法 ViewUtils.inject(Activity)
     47         ViewUtils.inject(this);
     48 
     49         textView.setText("你好 世界 by ViewUtils");
     50 
     51         btn.setText("点你一下");
     52 
     53         // 进行反射的处理,获取 string 成员变量
     54 
     55         Class c = this.getClass();
     56 
     57         try {
     58             // 1. 取变量
     59             Field field = c.getDeclaredField("string");
     60             field.setAccessible(true);
     61 
     62             // 2. 从成员变量声明中获取注解
     63 
     64             CodeAuthor annotation = field.getAnnotation(CodeAuthor.class);
     65 
     66             int v = annotation.value();
     67 
     68 
     69             // 2.1 从资源取字符串
     70             String str = getString(v);
     71 
     72             // 3. 赋值
     73             field.set(this, "Author: " + str);
     74 
     75 
     76         } catch (NoSuchFieldException e) {
     77             e.printStackTrace();
     78         } catch (IllegalAccessException e) {
     79             e.printStackTrace();
     80         }
     81 
     82         Log.d("CodeAuthor", "string = " + string);
     83 
     84         Log.d("CodeAuthor", "appName = " + appName);
     85 
     86     }
     87 
     88     @OnClick(R.id.main_txt_info)
     89     public void clickShowToast(View v){
     90         Toast.makeText(this, "你好", Toast.LENGTH_SHORT).show();
     91     }
     92 
     93     @OnCompoundButtonCheckedChange(R.id.chk_test)
     94     public void checkChange(CompoundButton checkBox, boolean isChecked){
     95         Toast.makeText(this, "选一下", Toast.LENGTH_SHORT).show();
     96     }
     97 
     98 
     99     private void test(){
    100 
    101     }
    102 
    103 }
    ViewUtils

    DbUtils

      1 package com.qianfeng.xutilsshow;
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 import android.util.Log;
      6 import com.lidroid.xutils.DbUtils;
      7 import com.lidroid.xutils.db.sqlite.Selector;
      8 import com.lidroid.xutils.db.sqlite.WhereBuilder;
      9 import com.lidroid.xutils.exception.DbException;
     10 import com.qianfeng.xutilsshow.database.DBManager;
     11 import com.qianfeng.xutilsshow.model.Book;
     12 
     13 import java.util.List;
     14 
     15 /**
     16  * Created with IntelliJ IDEA.
     17  * User: vhly[FR]
     18  * Date: 15/10/9
     19  * Email: vhly@163.com
     20  */
     21 public class BooksActivity extends Activity {
     22 
     23     private DbUtils dbUtils;
     24 
     25     @Override
     26     public void onCreate(Bundle savedInstanceState) {
     27         super.onCreate(savedInstanceState);
     28         setContentView(R.layout.books_activity);
     29 
     30         // 使用 DbUtils 进行数据的存储
     31         // 指定数据对象的内容, 然后直接存储
     32         dbUtils = DbUtils.create(getApplicationContext(), "app.db");
     33 
     34 
     35         // 以下代码 使用 纯手工 的 SQLite 操作,可以使用 xUtils 简化
     36 
     37 //        DBManager.newInstance(getApplicationContext());
     38 //
     39 //        initData();
     40     }
     41 
     42     @Override
     43     protected void onResume() {
     44         super.onResume();
     45 
     46         addBook();
     47 
     48         loadData();
     49 
     50 //        deleteBook();
     51 
     52         try {
     53 
     54             // select * from books where _id = 5
     55             Book book = dbUtils.findById(Book.class, 5L);
     56 
     57             Log.d("DB", "找到 5的 " + book.getTitle());
     58 
     59             // 自定义查询条件,对应就是  select * from books where price = 1000.0f and _id = 7
     60             Selector selector =
     61                     Selector.from(Book.class)
     62                             .where("price", "=", 1000.0f)
     63                             .and("_id", "=", 7L);
     64 
     65             // 自定义查询
     66             List<Book> list = dbUtils.findAll(selector);
     67 
     68             for (Book bk : list) {
     69 
     70                 Log.d("DB", "条件查询 " + bk.getId());
     71 
     72             }
     73 
     74 
     75         } catch (DbException e) {
     76             e.printStackTrace();
     77         }
     78 
     79     }
     80 
     81     /**
     82      * 使用 WhereBuilder 进行复杂条件的删除
     83      */
     84     private void deleteWhere(){
     85         // 1. 准备 WhereBuilder
     86 
     87         WhereBuilder builder =
     88                 WhereBuilder
     89                         .b("price", "=", 1000.0f)
     90                         .expr("price between 900 and 1000");
     91 
     92         // 2. DbUtils.delete(Class, WhereBuilder)
     93         try {
     94             dbUtils.delete(Book.class, builder);
     95         } catch (DbException e) {
     96             e.printStackTrace();
     97         }
     98 
     99     }
    100 
    101     private void addBook(){
    102 
    103         Book book = new Book();
    104 
    105         book.setTitle("10学会Android开发");
    106 
    107         book.setPrice(1000.0f);
    108 
    109         book.setAuthor("张哥");
    110 
    111         try {
    112             // 添加一个记录,对应的  就是 insert 语句
    113             // 内容并没有设置 id , 代表对象直接添加
    114             dbUtils.save(book);
    115         } catch (DbException e) {
    116             e.printStackTrace();
    117         }
    118 
    119     }
    120 
    121     /**
    122      * 使用 DbUtils 删除单一的一个 数据记录
    123      */
    124     private void deleteBook(){
    125 
    126         try {
    127             // 找到所有的记录
    128 
    129             List<Book> books = dbUtils.findAll(Book.class);
    130 
    131             if(books != null && !books.isEmpty()){
    132 
    133                 Book book = books.get(0);
    134 
    135                 Log.d("DB", "book id " + book.getId());
    136 
    137                 // 删除这个数据记录
    138 
    139                 dbUtils.delete(book);
    140 
    141             }
    142 
    143 
    144         } catch (DbException e) {
    145             e.printStackTrace();
    146         }
    147 
    148     }
    149 
    150     /**
    151      * 使用 DbUtils 来加载 表当中的 数据
    152      */
    153     public void loadData(){
    154         try {
    155             List<Book> books = dbUtils.findAll(Book.class);
    156 
    157             for (Book book : books) {
    158                 Log.d("DB", "Book " + book.getId());
    159             }
    160 
    161         } catch (DbException e) {
    162             e.printStackTrace();
    163         }
    164     }
    165 
    166 
    167 
    168 
    169 //
    170 //    private void initData(){
    171 //
    172 //        // 使用 DBManager 封装 SQLite 的数据库操作,避免  每次 都要获取 SQLiteDatabase
    173 //        DBManager dbManager = DBManager.getInstance();
    174 //
    175 //        Book book = new Book();
    176 //
    177 //        book.setTitle("10学会Android开发");
    178 //
    179 //        book.setPrice(1000.0f);
    180 //
    181 //        book.setAuthor("张哥");
    182 //
    183 //        long bid = dbManager.insertBook(book);
    184 //
    185 //        Log.d("DB", "book id " + bid);
    186 //
    187 //    }
    188 
    189 }
    DbUtils
  • 相关阅读:
    python项目实战三个小实例
    Python关键字及其用法
    python目录和文件的列表清单
    python文件的分类
    python文件操作r+,w+,a+,rb+,
    Python初探list
    python列表1
    office之Excel 你会用 Ctrl + E 吗?
    java用swing画可以行走的乌龟
    python删除列表元素
  • 原文地址:https://www.cnblogs.com/bimingcong/p/4868347.html
Copyright © 2020-2023  润新知