Xutils3使用全解析

初始化及注解:
x.Ext. init ( this );
//是否开启log,开启后将影响性能
x.Ext. setDebug ( true );

注解的使用:
①//绑定空间及布局文件
@ContentView (R.layout. activity_main )
public class MainActivity extends AppCompatActivity {
@ViewInject (R.id. pgb )
ProgressBar bar ;

@ViewInject (R.id. img_test )
ImageView imgTest ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
x. view ().inject( this );
}

②//绑定事件
/**
* 给每个按钮设置点击事件
*/
@Event (value = {R.id. btn_get , R.id. btn_post , R.id. btn_download , R.id. btn_upload , R.id. load_img , R.id. btn_db })
private void getEvent(View v) {
switch (v.getId()) {
case R.id. btn_get :
getMethod();
break ;

case R.id. btn_post :
postMethod();
break ;

case R.id. btn_download :
download();
break ;

case R.id. btn_upload :
upload();
break ;

case R.id. load_img :
loadImage();
break ;

case R.id. btn_db :
useDb();
break ;
}
}

一:get请求:
/**
* get的请求方法
*/
public void getMethod() {
RequestParams params = new RequestParams( "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" );
x.http().get(params, new Callback.CommonCallback()......}



二:post请求: (上传表单或者上传json)
/**
* 使用xutils完成post请求
*/
public void postMethod() {
RequestParams params = new RequestParams( "https://www.baidu.com/?tn=62095104_oem_dg" );
//上传表单
params.addBodyParameter( "age" , "25" );

//上传json数据----------------------------------------------------------------------------------
JSONObject object = new JSONObject();
try {
object.put( "name" , "23" );
} catch (JSONException e) {
e.printStackTrace();
}
params.setAsJsonContent( true );
params.setBodyContent(object.toString());
//--------------------------------------------------------------------------------------------------
x. http ().post(params, new Callback.CommonCallback() {............}


三:断点续传(下载):
public void download() {
RequestParams params = new RequestParams( "https://github.com/XXApple/AndroidLibs.git" );
//下载文件完成放置的路径
params.setSaveFilePath(Environment. getExternalStorageDirectory () + "/me/a.zip" );
//是否自动命名
params.setAutoRename( false );
//是否断点续传
params.setAutoResume( true );
//开三个子线程进行下载,会更快
params.setExecutor( new PriorityExecutor( 3 , true ));
//是否可以停止
params.setCancelFast( true );
x. http ().get(params, new Callback.ProgressCallback() {........}


四:上传文件:
//上传的路径
RequestParams params = new RequestParams( "xxxxxxx" );
//是否是上传文件
params.setMultipart( true );
//本地文件的路径
params.addBodyParameter( "file" , Environment. getExternalStorageDirectory () + "sdcard/a.mp4" );
x. http ().get(params, new Callback.CommonCallback() {......}

五:图片加载:
public void loadImage() {
ImageOptions options = new ImageOptions.Builder().
setCrop( true ).
setLoadingDrawableId(R.mipmap. ic_launcher ).
setUseMemCache( true ).
build();
x. image ().bind( imgTest , "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" , options);
}

六:数据库:
一:对象中指定表名和列名,如下
@Table (name = "person" )
public class Person {
@Column (name = "id" , autoGen = true , isId = true )
private int id ;

@Column (name = "age" )
private int age ;

@Column (name = "name" )
private String name ;

@Column (name = "price" )
private int price ;

二:初始化DbManager
private DbManager.DaoConfig getConfig() {
DbManager.DaoConfig config = new DbManager.DaoConfig();
//数据库的路径
config.setDbDir( new File( "sdcard/360" )).
//是否允许事物
setAllowTransaction( true ).
//数据库名
setDbName( "test.db" ).
//数据库版本
setDbVersion( 1 ).
setDbOpenListener( new DbManager.DbOpenListener() {
@Override
public void onDbOpened(DbManager db) {
// 开启WAL, 对写入加速提升巨大
db.getDatabase().enableWriteAheadLogging();
}
}).setDbUpgradeListener( new DbManager.DbUpgradeListener() {
@Override
public void onUpgrade(DbManager db, int oldVersion, int newVersion) {

}
}).setTableCreateListener( new DbManager.TableCreateListener() {
@Override
public void onTableCreated(DbManager db, TableEntity table) {

}
});
return config;
}

三:增
List persons = new ArrayList<>();
for ( int i = 0 ; i < 5 ; i++) {
Person person = new Person(i, 20 + i, "路人" + i, 10 + i);
persons.add(person);
}
try {
db .saveBindingId(persons);
} catch (DbException e) {
e.printStackTrace();
}

四:删
try {
//删除所有元素
db .delete(Person. class );
//删除指定ID的元素
db .deleteById(Person. class , 1 );
//删除指定元素
db .delete( new Object());
//删除特定条件的元素
WhereBuilder builder = WhereBuilder. b ();
builder.and( "id" , "=" , "2" );
db .delete(Person. class , builder);
} catch (DbException e) {
e.printStackTrace();
}

五:改
WhereBuilder builder = WhereBuilder. b ();
builder.and( "id" , "=" , "1" );
try {
db .update(Person. class , builder, new KeyValue( "id" , 10 ), new KeyValue( "price" , "999999999" ));
} catch (DbException e) {
e.printStackTrace();
}

六:查
try {
long a = db .selector(Person. class ).count();
//查个数
Log. e ( "a" , "useDb: " );
//查找所有元素
List list = db .findAll(Person. class );
Log. e ( "list" , "" + list.size());
//查找指定ID的元素
Person person1 = db .findById(Person. class , 3 );
Log. e ( "person1" , "" + person1.toString());
//按指定条件查找
List person2 = db .selector(Person. class ).where( "id" , "=" , "4" ).findAll();
Log. e ( "person2" , "" + person2.get( 0 ).toString());
} catch (DbException e) {
e.printStackTrace();
}

你可能感兴趣的:(Android应用开发)