# Introduction to xutils
* Xutils contains many practical Android tools.
* Xutils was originally originated from the afinal framework and has undergone a large number of reconstruction, enabling xutils to support large file uploads, more comprehensive HTTP request protocol support, and more flexible Orm, more event annotations are supported and are not affected by obfuscation.
* Xuitls is least compatible with Android 2.2 (API Level 8)
# Currently, xutils has four main modules:
* Dbutils module:
> * The ORM framework in Android allows you to add, delete, modify, and query a line of code;
> * Supports transactions, which are disabled by default;
> * You can use annotations to customize table names, column names, foreign keys, uniqueness constraints, not null constraints, and check constraints. (note the table names and column names when obfuscation is required );
> * Supports binding foreign keys. when an object is saved, the foreign key associated with the object is automatically saved or updated;
> * Automatically loads objects associated with foreign keys and supports delayed loading;
> * Supports chained expression query for more intuitive query semantics. For more information, see the following introduction or examples in the sample.
* Viewutils module:
> * The IOC framework in android can be fully annotated to bind the UI, resources, and events;
> * The new event binding method still works normally after obfuscation using obfuscation tools;
> * Currently, You can bind 20 common events. For more information, see the viewcommoneventlistener class and package com. lidroid. xutils. View. annotation. event.
* Httputils module:
> * Supports synchronous and asynchronous requests;
> * Supports uploading large files. Uploading large files does not involve oom;
> * Supports get, post, put, move, copy, delete, Head, options, trace, and connect requests;
> * Download supports 301/302 redirection and allows you to set whether to rename the downloaded file based on content-disposition;
> * Requests that return text content (only get requests are enabled by default) Support caching. You can set the default expiration time and the expiration time for the current request.
* Bitmaputils module:
> * When loading bitmap, you do not need to consider the OOM and Android container quickly slide during bitmap loading;
> * Supports loading network images and local images;
> * Memory Management uses the LRU algorithm to better manage bitmap memory;
> * You can configure the number of threads to load, cache size, cache path, and display animation...
# The following permissions are required to use the xutils quick development framework:
<Uses-Permission Android: Name = "android. Permission. Internet"/>
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
# Notes for obfuscation:
* Add Android default obfuscation configuration $ {SDK. dir}/tools/proguard/proguard-android.txt
* Do not confuse the annotation type in xutils. Add the obfuscation configuration:-keep class * extends java. Lang. annotation. annotation {*;}
* Do not confuse object classes that are persistent using the dbutils module, or annotate all tables and column names @ table (name = "XXX"), @ ID (column = "XXX "),
@ Column (column = "XXX"), @ foreign (column = "XXX", foreign = "XXX ");
# Dbutils usage:
Dbutils DB = dbutils. Create (this );
User user = new user (); // note that the user object must have an id attribute or an attribute annotated with @ ID.
User. setemail ("[email protected]");
User. setname ("wyouflf ");
DB. Save (User); // when savebindingid is used to save an object, the Object ID is assigned a value.
...
// Search
Parent entity = dB. findbyid (parent. Class, parent. GETID ());
Parent entity = dB. findfirst (entity); // search by the entity attribute
List <parent> List = dB. findall (entity); // search by the entity attribute
List <parent> List = dB. findall (parent. Class); // query by type
Parent = dB. findfirst (selector. From (parent. Class). Where ("name", "=", "test "));
// Is null
Parent = dB. findfirst (selector. From (parent. Class). Where ("name", "=", null ));
// Is not null
Parent = dB. findfirst (selector. From (parent. Class). Where ("name ","! = ", Null ));
// Where id <54 and (age> 20 or age <30) order by ID limit pagesize offset pageoffset
List <parent> List = dB. findall (selector. From (parent. Class)
. Where ("ID", "<", 54)
. And (wherebuilder. B ("Age", ">", 20). Or ("Age", "<", 30 ))
. Orderby ("ID ")
. Limit (pagesize)
. Offset (pagesize * pageindex ));
// When op is "in", the last parameter must be an array or iterable implementation class (such as list)
Parent test = dB. findfirst (selector. From (parent. Class). Where ("ID", "in", new int [] {1, 2, 3 }));
// When op is "between", the last parameter must be an array or iterable implementation class (such as list)
Parent test = dB. findfirst (selector. from (parent. class ). where ("ID", "between", new string [] {"1", "5 "}));
Dbmodel = dB. finddbmodelall (selector. From (parent. Class). Select ("name"); // select ("name") Only retrieves the name column
List <dbmodel> dbmodels = dB. finddbmodelall (selector. from (parent. class ). groupby ("name "). select ("name", "Count (name )"));
...
List <dbmodel> dbmodels = dB. finddbmodelall (SQL); // custom SQL query
Db.exe cnonquery (SQL) // execute custom SQL
# Viewutils usage
* UI binding and event binding can be performed in full annotation mode.
* Findviewbyid and setclicklistener are not required.
// The View annotation of xutils requires the ID to be provided, so that code obfuscation is not affected.
@ Viewinject (R. Id. textview)
Textview;
// @ Viewinject (Vale = R. Id. textview, parentid = R. Id. parentview)
// Textview;
@ Resinject (ID = R. String. Label, type = restype. String)
Private string label;
// The method of binding events with method names is canceled, and Id binding is not affected by confusion.
// Supports binding multiple IDs @ onclick ({R. Id. id1, R. Id. Id2, R. Id. ID3 })
// Or @ onclick (value = {R. id. id1, R. id. id2, R. id. ID3}, parentid = {R. id. pid1, R. id. pid2, R. id. pid3 })
// For more event support, see viewcommoneventlistener class and package com. lidroid. xutils. View. annotation. event.
@ Onclick (R. Id. test_button)
Public void testbuttonclick (view v) {// The method signature must be consistent with the requirements in the interface
...
}
...
// Inject in activity:
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Viewutils. Inject (this); // inject views and events
...
Textview. settext ("some text ...");
...
}
// Inject in fragment:
@ Override
Public View oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate ){
View view = Inflater. Inflate (R. layout. bitmap_fragment, container, false); // load the fragment Layout
Viewutils. Inject (this, view); // inject view and event
...
}
// Inject in preferencefragment:
Public void onactivitycreated (bundle savedinstancestate ){
Super. onactivitycreated (savedinstancestate );
Viewutils. Inject (this, getpreferencescreen (); // inject views and events
...
}
// Other Overloading
// Inject (view );
// Inject (activity)
// Inject (preferenceactivity)
// Inject (Object Handler, view)
// Inject (Object Handler, activity)
// Inject (Object Handler, preferencegroup)
// Inject (Object Handler, preferenceactivity)
# Httputils usage:
### Common get Method
'''Java
Httputils HTTP = new httputils ();
HTTP. Send (httprequest. httpmethod. Get,
"Http://www.lidroid.com ",
New requestcallback <string> (){
@ Override
Public void onloading (long total, long current, Boolean isuploading ){
Testtextview. settext (current + "/" + total );
}
@ Override
Public void onsuccess (responseinfo <string> responseinfo ){
Textview. settext (responseinfo. Result );
}
@ Override
Public void onstart (){
}
@ Override
Public void onfailure (httpexception error, string MSG ){
}
});
### Use httputils to upload files or submit data to the server (POST method)
Requestparams Params = new requestparams ();
Params. addheader ("name", "value ");
Params. addquerystringparameter ("name", "value ");
// Bodyparamsentity is used by default when only string parameters are included,
// Similar to urlencodedformentity ("application/X-WWW-form-urlencoded ").
Params. addbodyparameter ("name", "value ");
// After file parameters are added, the default value is multipartentity ("multipart/form-Data "),
// If you need "multipart/related", the multipartentity provided in xutils supports setting subtype to "related ".
// Use Params. setbodyentity (httpentity) to set more types of httpentity (for example:
// Multipartentity, bodyparamsentity, fileuploadentity, inputstreamuploadentity, stringentity ).
// For example, Params. setbodyentity (New stringentity (jsonstr, charset ));
Params. addbodyparameter ("file", new file ("path "));
...
Httputils HTTP = new httputils ();
HTTP. Send (httprequest. httpmethod. Post,
"Uploadurl ....",
Params,
New requestcallback <string> (){
@ Override
Public void onstart (){
Testtextview. settext ("conn ...");
}
@ Override
Public void onloading (long total, long current, Boolean isuploading ){
If (isuploading ){
Testtextview. settext ("upload:" + current + "/" + total );
} Else {
Testtextview. settext ("reply:" + current + "/" + total );
}
}
@ Override
Public void onsuccess (responseinfo <string> responseinfo ){
Testtextview. settext ("reply:" + responseinfo. Result );
}
@ Override
Public void onfailure (httpexception error, string MSG ){
Testtextview. settext (error. getexceptioncode () + ":" + MSG );
}
});
### Use httputils to download an object:
* Supports resumable download, stops download tasks at any time, and starts tasks.
Httputils HTTP = new httputils ();
Httphandler handler = http. Download ("http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip ",
"/SDK/httpcomponents-client-4.2.5-src.zip ",
True, // if the target file exists, continue to download the unfinished part. When the server does not support range, it will be downloaded again.
True, // if the file name is obtained from the returned information of the request, it is automatically renamed after the download is complete.
New requestcallback <File> (){
@ Override
Public void onstart (){
Testtextview. settext ("conn ...");
}
@ Override
Public void onloading (long total, long current, Boolean isuploading ){
Testtextview. settext (current + "/" + total );
}
@ Override
Public void onsuccess (responseinfo <File> responseinfo ){
Testtextview. settext ("downloaded:" + responseinfo. Result. getpath ());
}
@ Override
Public void onfailure (httpexception error, string MSG ){
Testtextview. settext (MSG );
}
});
// Call the stop () method to stop download
Handler. Stop ();
# Bitmaputils usage
Bitmaputils = new bitmaputils (this );
// Load the network image
Bitmaputils. Display (testimageview, "http://bbs.lidroid.com/static/image/common/logo.png ");
// Load the local image (path starts with "/", absolute path)
Bitmaputils. Display (testimageview, "/sdcard/test.jpg ");
// Load the image in assets (the path starts with Assets)
Bitmaputils. Display (testimageview, "assets/img/wallpaper.jpg ");
// You can use pauseonscrolllistener to control the loading of images during slide and quick slide by using listview and other containers.
Listview. setonscrolllistener (New pauseonscrolllistener (bitmaputils, false, true ));
Listview. setonscrolllistener (New pauseonscrolllistener (bitmaputils, false, true, customlistener ));
# Others (*** for more sample code, see the code in the sample folder ***)
### Output log logutils
// Automatically add tags in the format of classname. methodname (L: linenumber)
// You can set global logutils. allowd = false, logutils. allowi = false... to control whether to output logs.
// Custom log output logutils. customlogger = new xxxlogger ();
Logutils. D ("xutils ");
Summary of xutils