Android toolkit xUtils 3.3.16, xutils3.3.16

Source: Internet
Author: User
Tags call back webp

Android toolkit xUtils 3.3.16, xutils3.3.16

(Reprinted)

# XUtils3 Overview
* XUtils contains many practical android tools.
* XUtils supports uploading large files (more than 2 GB), more comprehensive support for http request protocols (11 predicates), and more flexible ORM, more event annotations are supported and are not affected by obfuscation...
* XUtils is least compatible with Android 4.0 (api level 14). ([Android 2.3?] (Https://github.com/wyouflf/xUtils3/issues/8 ))
* XUtils3 has many changes, so a new project is not maintained on the old version (github.com/wyouflf/xUtils). Compared with the old version:
1. The HTTP implementation replaces HttpClient with UrlConnection, automatically parses the callback generic type, and provides a safer resumable data transfer policy.
2. Supports standard Cookie policies to distinguish domain, path...
3. Remove uncommon functions with event annotations to improve performance.
4. Simplified database APIs to improve performance and achieve the same performance as greenDao.
5. Image binding supports gif (subject to system compatibility, some gif files can only be displayed statically) and webp; supports cropping rounded corners, circles, and squares, and supports automatic rotation...


#### Add a dependency when using Gradle for building:
'''Javascript
Compile 'org. xutils: 3.3.4'
'''
##### If you use eclipse, You Can [Click here to download the aar file] (http://dl.bintray.com/wyouflf/maven/org/xutils/xutils/), and then decompress the package with zip to retrieve the jar package and so file.
##### For mixed configuration, refer to the configuration of sample in the example project.




#### FAQs:
1. Better Management image cache: https://github.com/wyouflf/xUtils3/issues/149
2. Cookie use: https://github.com/wyouflf/xUtils3/issues/125
3. query parameters? Http requests can pass parameters through header, url, and body. The query parameter is the question mark (?) in the url (?) Parameters.
4. About the body parameter? The body parameter only supports PUT, POST, PATCH, and DELETE requests. (The RFC2616 document of the old version does not explicitly indicate whether it supports the request.


#### Pre-Configuration
##### Required permissions
'''Xml
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
'''
##### Initialization
'''Java
// Initialize in onCreate of application
@ Override
Public void onCreate (){
Super. onCreate ();
X. Ext. init (this );
X. Ext. setDebug (true); // whether to output the debug log
...
}
'''


### Use @ Event annotation (For more examples, such as @ ContentView and @ ViewInject, refer to the sample project)
'''Java
/**
* 1. The method must be private,
* 2. The method parameter format must be consistent with the Listener interface corresponding to the type.
* 3. The annotation parameter value supports Arrays: value = {id1, id2, id3}
* 4. For other parameter descriptions, see descriptions of the {@ link org. xutils. event. annotation. Event} class.
**/
@ Event (value = R. id. btn_test_baidu1,
Type = View. OnClickListener. class/* optional parameter. Default Value: View. OnClickListener. class */)
Private void onTestBaidu1Click (View view ){
...
}
'''


### Access the network (For more examples, refer to the sample project)
'''Java
/**
* For custom object parameter classes, see:
* Request annotation {@ link org. xutils. http. annotation. HttpRequest}
* Request annotation processing template interface {@ link org. xutils. http. app. ParamsBuilder}
*
* When you need to use a custom type as the callback generic, refer:
* Response annotation {@ link org. xutils. http. annotation. HttpResponse}
* Response annotation processing template interface {@ link org. xutils. http. app. ResponseParser}
*
* Example: view the code in the org. xutils. sample. http package.
*/
BaiduParams params = new BaiduParams ();
Params. wd = "xUtils ";
// Use the multipart form when uploading files. Otherwise, upload the original file stream.
// Params. setMultipart (true );
// File Upload method 1
// Params. uploadFile = new File ("/sdcard/test.txt ");
// File Upload method 2
// Params. addBodyParameter ("uploadFile", new File ("/sdcard/test.txt "));
Callback. Cancelable cancelable
= X. http (). get (params,
/**
* 1. Generic callback:
* For the generic types supported by the callback parameter by default, see {@ link org. xutils. http. loader. LoaderFactory },
* For example, if the generic type is File, the File can be downloaded. Use params. setSaveFilePath (path) to specify the full path for saving the File.
* Resumable upload is supported by default (the consistency between the filelock and the end-end verification is adopted ).
* You can register other common types in LoaderFactory,
* You can also use {@ link org. xutils. http. annotation. HttpResponse}
* Add the annotation HttpResponse to the custom return value type to implement the custom ResponseParser interface for unified conversion.
* If the returned value is in json format, it is very easy to define your own ResponseParser using a third-party json tool.
* For example, the sample code {@ link org. xutils. sample. http. BaiduResponse} can be directly used
* Generic callback.
*
* 2. callback combination:
* You can use the base class or interface to combine the Callback of each type. For details, see {@ link org. xutils. common. Callback }.
* Example:
* A. Using CacheCallback in combination will make the request detection cache or store the results in the cache (only GET requests take effect ).
* B. The combination of PrepareCallback prepare methods will provide callback with an opportunity to execute time-consuming tasks in the background,
* Then, the result is sent to onCache or onSuccess.
* C. You can use ProgressCallback to call back the progress.
*... (For details, refer to {@ link org. xutils. image. ImageLoader}
* Or {@ link org. xutils. sample. download. DownloadCallback} in the sample code })
*
* 3. Request Process interception or logging: Refer to {@ link org. xutils. http. app. RequestTracker}
*
* 4. Request Header retrieval: Refer to {@ link org. xutils. http. app. RequestInterceptListener}
*
* 5. Others (thread pool, timeout, redirection, retry, proxy, etc.): see {@ link org. xutils. http. RequestParams}
*
**/
New Callback. CommonCallback <String> (){
@ Override
Public void onSuccess (String result ){
Toast. makeText (x. app (), result, Toast. LENGTH_LONG). show ();
}


@ Override
Public void onError (Throwable ex, boolean isOnCallback ){
// Toast. makeText (x. app (), ex. getMessage (), Toast. LENGTH_LONG). show ();
If (ex instanceof HttpException) {// network error
HttpException httpEx = (HttpException) ex;
Int responseCode = httpEx. getCode ();
String responseMsg = httpEx. getMessage ();
String errorResult = httpEx. getResult ();
//...
} Else {// other errors
//...
}
Toast. makeText (x. app (), ex. getMessage (), Toast. LENGTH_LONG). show ();
}


@ Override
Public void onCancelled (CancelledException cex ){
Toast. makeText (x. app (), "canceled", Toast. LENGTH_LONG). show ();
}


@ Override
Public void onFinished (){


}
});


// Cancelable. cancel (); // cancel the request
'''
#### If you only need a simple version:
'''Java
@ Event (value = R. id. btn_test_baidu2)
Private void onTestBaidu2Click (View view ){
RequestParams params = new RequestParams ("https://www.baidu.com/s ");
Params. setSslSocketFactory (...); // set ssl
Params. addQueryStringParameter ("wd", "xUtils ");
X. http (). get (params, new Callback. CommonCallback <String> (){
@ Override
Public void onSuccess (String result ){
Toast. makeText (x. app (), result, Toast. LENGTH_LONG). show ();
}


@ Override
Public void onError (Throwable ex, boolean isOnCallback ){
Toast. makeText (x. app (), ex. getMessage (), Toast. LENGTH_LONG). show ();
}


@ Override
Public void onCancelled (CancelledException cex ){
Toast. makeText (x. app (), "canceled", Toast. LENGTH_LONG). show ();
}


@ Override
Public void onFinished (){


}
});
}
''''
#### Request example with cache:
'''Java
BaiduParams params = new BaiduParams ();
Params. wd = "xUtils ";
// The default cache survival time, in milliseconds. (If the service does not return a valid max-age or Expires)
Params. setCacheMaxAge (1000*60 );
Callback. Cancelable cancelable
// With CacheCallback, xUtils caches data for this request.
= X. http (). get (params, new Callback. CacheCallback <String> (){


Private boolean hasError = false;
Private String result = null;


@ Override
Public boolean onCache (String result ){
// Obtain the cached data. This method will not be used after the cache expires.
// If the server does not return the expiration time, refer to the params. setCacheMaxAge (maxAge) method.
//
// * The client determines whether the local cache is assigned to the onCache Method Based on max-age or expires in the header returned by the server.
// If the server does not return max-age or expires, the cache will be saved all the time, unless you have defined that false is returned here.
// Logic, then xUtils will request new data to overwrite it.
//
// * If you trust the cache and return true, the network is no longer requested;
// Return false to continue the request network, but the ETag, Last-Modified, and other information will be added to the request header,
// If the server returns 304, the data is not updated and the data is not loaded.
//
This. result = result;
Return false; // true: the cached data is trusted, and no network request is initiated. false: the cached data is not trusted.
}


@ Override
Public void onSuccess (String result ){
// Note: if the service returns 304 or the onCache selects the trust cache, it will not be called here,
// But onFinished will always be called.
This. result = result;
}


@ Override
Public void onError (Throwable ex, boolean isOnCallback ){
HasError = true;
Toast. makeText (x. app (), ex. getMessage (), Toast. LENGTH_LONG). show ();
If (ex instanceof HttpException) {// network error
HttpException httpEx = (HttpException) ex;
Int responseCode = httpEx. getCode ();
String responseMsg = httpEx. getMessage ();
String errorResult = httpEx. getResult ();
//...
} Else {// other errors
//...
}
}


@ Override
Public void onCancelled (CancelledException cex ){
Toast. makeText (x. app (), "canceled", Toast. LENGTH_LONG). show ();
}


@ Override
Public void onFinished (){
If (! HasError & result! = Null ){
// Data retrieved successfully
Toast. makeText (x. app (), result, Toast. LENGTH_LONG). show ();
}
}
});
'''


### Use a database (For more examples, refer to the sample project)
'''Java
Parent test = db. selector (Parent. class). where ("id", "in", new int [] {1, 3, 6}). findFirst ();
Long count = db. selector (Parent. class ). where ("name", "LIKE", "w % "). and ("age", ">", 32 ). count ();
List <Parent> testList = db. selector (Parent. class ). where ("id", "between", new String [] {"1", "5 "}). findAll ();
'''


### Bind an image (For more examples, refer to the sample project)
'''Java
X. image (). bind (imageView, url, imageOptions );


// Assets file
X. image (). bind (imageView, "assets: // test.gif", imageOptions );


// Local file
X. image (). bind (imageView, new File ("/sdcard/test.gif"). toURI (). toString (), imageOptions );
X. image (). bind (imageView, "/sdcard/test.gif", imageOptions );
X. image (). bind (imageView, "file: // sdcard/test.gif", imageOptions );
X. image (). bind (imageView, "file:/sdcard/test.gif", imageOptions );


X. image (). bind (imageView, url, imageOptions, new Callback. CommonCallback <Drawable> (){...});
X. image (). loadDrawable (url, imageOptions, new Callback. CommonCallback <Drawable> (){...});
X. image (). loadFile (url, imageOptions, new Callback. CommonCallback <File> (){...});
'''


____
### About libwebpbackport
* Some 4.x models still have problems with support for the webp format. We need to use webp.
* Webp from: https://github.com/webmproject/libwebp
* Webpbackport from: https://github.com/alexey-pelykh/webp-android-backport
* Added the nativeDecodeFile implementation for webpbackport and fixed the bug in Android 5.0 or later systems:
'''Cpp
// Android_backport_webp.cpp
// Modify:
JclassRef = jniEnv-> FindClass (...);
//:
Jclass temp = jniEnv-> FindClass (...);
JclassRef = (jclass) jniEnv-> NewGlobalRef (temp );
JniEnv-> DeleteLocalRef (temp );
// For other jni code modifications, see: http://my.oschina.net/u/1171837/blog/533153

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.