XUtils3

Xutis3简介
xUtils是基于Afinal开发的目前功能比较完善的一个Android开源框架,最近又发布了xUtil3.0,在增加新功能的同时又提高了框架的性能。

1.xUtils包含了很多实用的android工具; xUtils支持超大文件(超过2G)上传,更全面的http请求协议支持(11种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响;
2.Utils 最低兼容Android 4.0 (api level 14);
3.xUtils3变化较多所以建立了新的项目不在旧版(github.com/wyouflf/xUtils)上继续维护, 相对于旧版本:

HTTP实现替换HttpClient为UrlConnection, 自动解析回调泛型, 更安全的断点续传策略;

支持标准的Cookie策略, 区分domain, path;

事件注解去除不常用的功能, 提高性能;

数据库api简化提高性能, 达到和greenDao一致的性能;

图片绑定支持gif(受系统兼容性影响, 部分gif文件只能静态显示), webp;

支持圆角, 圆形, 方形等裁剪, 支持自动旋转。

1.使用XUtils3需要进行倒依赖
compile ‘org.xutils:xutils:3.3.36’

2.在使用框架使用需要的添加的权限

<-- 网络请求 -->
<uses-permission android:name="android.permission.INTERNET">uses-permission>
<-- 可写权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">uses-permission>
<-- 可读权限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">uses-permission>

3.Xutils3框架功能

package com.bwie.xutils.acivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.xutils.R;
import com.bwie.xutils.app.MyApp;
import com.bwie.xutils.bean.Student;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.tv_zhujie)
    TextView tv_zhujie;
    @ViewInject(R.id.tv_Image)
    ImageView tv_Image;
    private String url = "http://www.93.gov.cn/93app/data.do";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        x.view().inject(this);
        tv_zhujie.setText("这是我使用Xutils3的注解方式来找控件");
    }

    @Event(type = View.OnClickListener.class, value = {R.id.tv_Image, R.id.tv_zhujie, R.id.bt_insert, R.id.bt_find, R.id.bt_updata, R.id.bt_delete, R.id.bt_delete_db, R.id.bt_delete_table, R.id.bt_add})
    private void onClick(View view) throws DbException {
        DbManager db= MyApp.getDb();
        switch (view.getId()) {
            case R.id.tv_Image:
                loadPic();
                getData();
                break;
            //注解获取缓存的数据
            case R.id.tv_zhujie:
                getCacheData();
                break;
            //添加
            case R.id.bt_insert:
                List students=new ArrayList<>();
                students.add(new Student("张三"));
                students.add(new Student("李四"));
                students.add(new Student("王五"));
                students.add(new Student("赵六"));
                try {
                    db.save(students);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            //查询
            case R.id.bt_find:
                try {
                    Student student = db.findFirst(Student.class);
                    Log.i("sss", student.toString());
                    List studentList = db.findAll(Student.class);
                    for (Student st : studentList) {
                        Log.i("sss", st.toString());
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            //修改数据库内容
            case R.id.bt_updata:
                try {
                    Student student=db.findById(Student.class,"2");
                    student.setName("段淇皓");
                    db.update(student,"name");
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            //删除
            case R.id.bt_delete:
                try {
//                    Student student=db.findById(Student.class,"2");
//                    db.delete(student);
                    //删除全部
                    db.delete(Student.class);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            //删除数据库
            case R.id.bt_delete_db:
                db.dropDb();
                break;
            //删除表
            case R.id.bt_delete_table:
                db.dropTable(Student.class);
                break;
            //添加
            case R.id.bt_add:
                Student student=new Student("哈哈哈");
                db.save(student);
                break;

        }
    }

    //图片长按事件
    @Event(type = View.OnLongClickListener.class, value = {R.id.tv_Image,R.id.tv_zhujie})
    private boolean onLongClick(View view) {
        switch (view.getId()) {
            case R.id.tv_Image:
                postData();
                break;
        }
        return true;
    }

    //get方式获取缓存数据
    private void getCacheData() {
        RequestParams params = new RequestParams(url);
        params.addQueryStringParameter("channeId", "0");
        params.addQueryStringParameter("startNum", "0");
        //缓存时间
        params.setCacheMaxAge(1000 * 40);
        x.http().get(params, new Callback.CacheCallback() {
            String result = null;

            @Override
            public void onSuccess(String result) {
                if (result != null) {
                    this.result = result;
                    Toast.makeText(MainActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                this.result = result;
                Toast.makeText(MainActivity.this, "缓存", Toast.LENGTH_SHORT).show();
                return true;
                //true代表支持缓存 如果缓存时间没有过期就不走网络请求,false不支持缓存
            }
        });
    }

    //post获取数据
    private void postData() {
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("channelId", "0");
        params.addBodyParameter("startNum", "0");
        x.http().post(params, new Callback.CommonCallback() {
            @Override
            public void onSuccess(String result) {

                Toast.makeText(MainActivity.this, "post请求成功" + result, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //get获取数据
    private void getData() {
        RequestParams params = new RequestParams(url);
        params.addQueryStringParameter("channelId", "0");
        params.addQueryStringParameter("startNum", "0");
        x.http().get(params, new Callback.CommonCallback() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(MainActivity.this, "get请求成功" + result, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
/**
     * 加载图片属性
     * //通过ImageOptions.Builder().set方法设置图片的属性
     * ImageOptions imageOptions= new ImageOptions.Builder().setFadeIn(true).build(); //淡入效果
     * //ImageOptions.Builder()的一些其他属性:
     * .setCircular(true) //设置图片显示为圆形
     * .setSquare(true) //设置图片显示为正方形
     * .setCrop(true).setSize(200,200) //设置大小
     * .setAnimation(animation) //设置动画
     * .setFailureDrawable(Drawable failureDrawable) //设置加载失败的动画
     * .setFailureDrawableId(int failureDrawable) //以资源id设置加载失败的动画
     * .setLoadingDrawable(Drawable loadingDrawable) //设置加载中的动画
     * .setLoadingDrawableId(int loadingDrawable) //以资源id设置加载中的动画
     * .setIgnoreGif(false) //忽略Gif图片
     * .setParamsBuilder(ParamsBuilder paramsBuilder) //在网络请求中添加一些参数
     * .setRaduis(int raduis) //设置拐角弧度
     * .setUseMemCache(true) //设置使用MemCache,默认true
     */
//加载图片
    private void loadPic() {
        String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1504071880709&di=bfc7425296c71db832e6abf4804cd7dc&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F1f178a82b9014a909461e9baa1773912b31bee5e.jpg";
        ImageOptions options = new ImageOptions.Builder().setLoadingDrawableId(R.mipmap.a).setSize(200, 200).setRadius(20).setCircular(true).build();
        x.image().bind(tv_Image, url, options);
    }
}

4.框架需要进行初始化

package com.bwie.xutils.app;

import android.app.Application;

import org.xutils.DbManager;
import org.xutils.x;

import java.io.File;

/**
 * Created by Love_you on 2017/8/30 0030.
 */
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
    }
    //管理数据库初始化
    public static DbManager getDb() {
        //创建DaoConfig
        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig().setDbName("bw.db").setDbDir(new File("/mnt/sdcard")).setDbVersion(1);
        DbManager db = x.getDb(daoConfig);
        return db;
    }
}

5.数据库bean的类型

package com.bwie.xutils.bean;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

/**
 * Created by Love_you on 2017/8/30 0030.
 */
 //标签数据库
@Table(name = "Student",onCreated = "")
public class Student {
    //创建主键id
    @Column(name = "id",isId = true,autoGen = true,property = "Not null")
    private int id;
    //数据库中的字段
    @Column(name = "name")
    private String name;
    //无参构造器

    public Student() {
    }

   //有参构造器
    public Student(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

你可能感兴趣的:(XUtils3)