XUtils3, xutils3github
1. Introduction
XUtils3 is an upgraded version of xUtils with improved functions and performance. xUtils3 has four main modules: annotation module, networking module, image loading module, and database module;
Annotation module:
It is used to initialize layout files in Activity or Fragment to simplify the code;
Networking module:
XUtils3 supports uploading large files (more than 2 GB), more comprehensive http request protocol support (11 predicates), and more flexible ORM, more event annotations are supported and are not affected by obfuscation;
Image loading module:
It is convenient to load images without worrying about memory overflow. You can also bind images to support gif (affected by system compatibility, some gif files can only be displayed statically) and webp; rounded corners and circles are supported, auto Rotation is supported for square cropping.
Database module:
Database api simplification improves performance to achieve the same performance as greenDao
2. annotation Module
① Use annotation in Activity
1. Add the following code to the oncreate method of Application: x. Ext. init (this );
2. Add the following code to the oncreate method of the Activity: x. view (). inject (this );
3. The following annotation is required to load the current Activity layout: @ ContentView is added to the top of the Activity.
4. The following annotation is required to initialize the View: @ InjectView
5. The following annotations are required to process various response events of the control: @ Envent
@ Event (value = R. id. btn_database) private void database (View view) {Toast. makeText (XUtils3Activity. this, "Enter the database module separately", Toast. LENGTH_SHORT ). show ();}
② Use annotations in Fragment
In onCreateView (), return super. onCreateView (inflater, container, savedInstanceState); change to return x. view (). inject (this, inflater, container );
@ContentView(R.layout.fragment_demo)public class DemoFragment extends Fragment {
@ViewInject(R.id.btn_fragment) private Button button; private Context context; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context =getContext(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// return super.onCreateView(inflater, container, savedInstanceState); return x.view().inject(this,inflater,container); }}
3. Networking Module
① Get and Post request text of xUtils3
// 1. get request // 2. post request RequestParams params = new RequestParams ("http://api.m.mtime.cn/PageSubArea/TrailerList.api"); x. http (). post (params, new Callback. commonCallback <String> () {@ Overridepublic void onSuccess (String result) {Log. e ("TAG", "xUtis3 networking request succeeded =" + result); // textView. setText ("Get request result ------" + result); textView. setText ("Post request result ------" + result) ;}@ Overridepublic void onError (Throwable ex, boolean isOnCallback) {Log. e ("TAG", "xUtis3 network connection request failed =" + ex. getMessage (); textView. setText ("xUtis3 network connection request failed =" + ex. getMessage () ;}@ Overridepublic void onCancelled (CancelledException cex) {Log. e ("TAG", "onCancelled =" + cex. getMessage () ;}@ Overridepublic void onFinished () {Log. e ("TAG", "onFinished = ");}});
② XUtils3 File Download
RequestParams params = new RequestParams ("http://vfx.mtime.cn/Video/2016/09/15/mp4/160915092608935956_480.mp4"); // sets the params for saving data. setSaveFilePath (Environment. getExternalStorageDirectory () + "/atguigu/480.mp4"); // sets whether params can be downloaded immediately. setCancelFast (true); // sets whether to automatically name params Based on the header information. setAutoRename (false); // sets the params for resumable upload. setAutoResume (true); params. setExecutor (new PriorityExecutor (3, true); // custom thread pool, valid value range: [1, 3]. When set to 3, it may block image loading. x. http (). get (params, new Callback. progressCallback <File> () {/*** calls back this method when the download is successful, and return the downloaded path * @ param file */@ Overridepublic void onSuccess (File file) {Log. e ("TAG", "onSuccess =" + file. toString (); Toast. makeText (XUtils3NetActivity. this, "onSuccess =" + file. toString (), Toast. LENGTH_SHORT ). show () ;}@ Overridepublic void onError (Throwable ex, boolean isOnCallback) {Log. e ("TAG", "onError =" + ex. getMessage () ;}@ Overridepublic void onCancelled (CancelledException cex) {Log. e ("TAG", "onCancelled =" + cex. getMessage () ;}@ Overridepublic void onFinished () {Log. e ("TAG", "onFinished =") ;}@ Overridepublic void onWaiting () {Log. e ("TAG", "onWaiting =") ;}@ Overridepublic void onStarted () {Log. e ("TAG", "onStarted =") ;}@ Overridepublic void onLoading (long total, long current, boolean isDownloading) {progressbar. setMax (int) total); progressbar. setProgress (int) current); Log. e ("TAG", "onLoading =" + current + "/" + total + ", isDownloading =" + isDownloading );}});
③ XUtils3 large file upload
RequestParams params = new RequestParams ("http: // 192.168.1.16: 8080/FileUpload/FileUploadServlet"); // upload params in a form. setMultipart (true); // set the path of the uploaded file to params. addBodyParameter ("File", new File (Environment. getExternalStorageDirectory () + "/atguigu/480.mp4"), null, "oppo.mp4"); x. http (). post (params, new Callback. progressCallback <File> () {/*** calls back this method when the download is successful, and return the downloaded path * @ param file */@ Overridepublic void onSuccess (File file) {Log. e ("TAG", "onSuccess =" + file. toString (); Toast. makeText (XUtils3NetActivity. this, "onSuccess =" + file. toString (), Toast. LENGTH_SHORT ). show () ;}@ Overridepublic void onError (Throwable ex, boolean isOnCallback) {Log. e ("TAG", "onError =" + ex. getMessage () ;}@ Overridepublic void onCancelled (CancelledException cex) {Log. e ("TAG", "onCancelled =" + cex. getMessage () ;}@ Overridepublic void onFinished () {Log. e ("TAG", "onFinished =") ;}@ Overridepublic void onWaiting () {Log. e ("TAG", "onWaiting =") ;}@ Overridepublic void onStarted () {Log. e ("TAG", "onStarted =") ;}@ Overridepublic void onLoading (long total, long current, boolean isDownloading) {progressbar. setMax (int) total); progressbar. setProgress (int) current); Log. e ("TAG", "onLoading =" + current + "/" + total + ", isDownloading =" + isDownloading );}});