GitHub Address
Rxlibrary Project:
1.RXJAVA2 + retrofit2 Package, common request (get,post, file upload, file download), simple and convenient, support custom loading and other properties.
The use of 2.RxBus is exactly the same as Evenbus.
Integration
compile ‘com.bhm.sdk.rxlibrary:RxLibrary:2.3.1‘
Or
<dependency> <groupId>com.bhm.sdk.rxlibrary</groupId> <artifactId>RxLibrary</artifactId> <version>2.3.1</version> <type>pom</type></dependency>
I. Use of RXJAVA2 + retrofit2
Strongly recommended reference demo,mianactivity contains common usage and usage descriptions. &NBSP;
The first step, to inherit rxbaseactivity or rxbasefragment, to add a mechanism for memory management, while acquiring Rxmanager objects, Rxmanager is the class that manages the observer when canceling the request, Interrupt requests, and so on, can call the corresponding method. Note: If the project Baseactivity inherits other activities, you need to add rxappcompatactivity code to baseactivity and generate Rxmanager objects, fragment the same. &NBSP;
Second step, use.
Rxbuilder Builder = Rxbuilder.newbuilder (this)//.setloadingdialog (Rxloadingdialog.getdefaultdialog ()) . Setloadingdialog (New Myloadingdialog ()). Setdialogattribute (True, False, False)//.sethttpti Meout (). Setislogoutput (FALSE)//default is True. Setisdefaulttoast (True, Rxmanager). Bindrx (); Builder.createapi (Httpapi.class, Host). GetData ("Bearer aedfc1246d0b4c3f046be2d50b34d6ff", "1"). C Ompose (Bindtolifecycle ())//Manage life cycle. Compose (Rxmanager.rxschedulerhelper ())//Publish event IO thread. Subscribe (New R Xobserver<object> (builder) {//object can be replaced by an entity class, without having to parse//according to business needs, can inherit Rxobserver rewrite class, Parse Onfail and onsuccess, according to ResultCode for processing @Override public void OnStart (disposable disposable) {} @Override public void onsuccess (Object response) {log.i ("onsuccess-------> ", response.tostring ()); } @Override public void Ondone () {} @Override public void Onfail (Throwable t) {}});
Httpapi is an interface, GetData is a method in Httpapi.
Host is the domain name.
The following describes the use of common annotations in HTTPAPI:
1.get Request: Add @get before method ("url"), parameter @Query, @QueryMap <br> example (URL and domain name stitching up the complete link, of course, we do not need to splice, retrofit will handle) </br> @G ET ("Welfare/10/1") observable<object> GetData (@Header ("token") string token, @Query ("type") of string type); 2.post Request: Add @formurlencoded and @post ("url") observable<object> getdatapost (@Field ("Once") Boolean once_no before the method; 3. File Upload: Method before adding @multipart and @post ("url") observable<object> Upload (@Header ("token") String token, @ Part ("filename") requestbody description, @Part ("id") Requestbody ID, @Part multipartbody.part file); Where Requestbody is generated: Requestbody.create (Mediatype.parse ("Text/plain"), "id" multipartbody.part generation: Requestb Ody requestbody = requestbody.create (Mediatype.parse ("IMAGE/JPEG; Charset=utf-8 "), file); Multipartbody.part part= MultipartBody.Part.createFormData ("File", File.getname (), requestbody); 4. File Download: Add @streaming and @GET @Streaming before method @GET Observable<responsebodY> downLoad (@Url String Url); There is no need to specify a URL after @get, parameter @url specifies the full URL, so there is no need to specify the domain name host.
Ii. use of Rxbus 1. Subscribe to Rxbus.get (). Register (this);
一般在activity/fragment的oncreate方法中添加
2. Unsubscribe Rxbus.get (). UnRegister (this);
一般在activity/fragment的的ondestory方法添加
3. Receive event, process
在activity/fragment中添加如下方法 @Subscribe(code = 1111,threadMode = ThreadMode.MAIN)public void rxBusEvent(Entity entity){ if(null != entity){ Toast.makeText(this, "RxBus改变了MainActivity的标题", Toast.LENGTH_SHORT).show(); }} threadMode:MAIN,NEW_THREAD,CURRENT_THREAD(默认)
4. Send at any one place
RxBus.get().send(1111, entity);
Rxjava Package, Rxbus package (on-line project integration, claim cycle management, no memory overflow memory, support for simultaneous multiple requests.) )