RXJAVA2 and retrofit encapsulation of the RetrofitClient2 finally came! __java

Source: Internet
Author: User

tamic:http://blog.csdn.net/sk719887916/article/details/51958010

RXJAVA1 and Retrofit2 combination of good retrofitclient author last year wrote a, follow-up has friends DMS request Upgrade second version, this plan does not maintain, but many people want to use RXJAVA2, so they have to support RXJAVA2 upgrade, Upgrade process encountered some trouble, after a period of testing, wrote this article, if you want to more complete function please refer to novate. RXJAVA1:

Rxjava+retrofit please refer to:
The Android Retrofitclient Tool class based on the Retrofit2.0+rxjava package is written at the top:

Advantages of this package:

Avoid creating retrofit solid columns repeatedly.

Call convenient and concise.

You do not have to repeat the steps to set properties.

Fixed configuration Host can also dynamically configure URLs, request headers, parameters, and so on.

Support file downloads and uploads.

Support for JSON, form submission.

Support Extended Apiservice

Unified handling without network conditions and support for load progress

Caching mechanism supported, cookie synchronization

Optimization cancellation feature

This time I am using the latest Rxjava2.1, retrofit 2.3, okhttp3.8 developed. Please see the official instructions for the other version differences.

The process of implementation can be seen in the first detailed introduction. Here is not a drag, but it is worth noting that the biggest advantage of RXJAVA2 is increased by the pressure, and flowable, obserable permissions, etc., in addition to change in the name of the package, some class names have also been changed, such as: Func became funtion, The action becomes customer, Transformer becomes Flowabletransformer, and the array generics support Maparray that supports the map operator upgrades.

Other advantages can go to Rxaava's GitHub reading:
Https://github.com/ReactiveX/RxJava

Retrofit has also changed in the latest version, @PartMap will not support annotation key refers to provide another way @partlist, etc., this way can make multiple file uploads more flexible:

Public interface Uploadapiservice {

    @GET ("Service/upload")
    flowable<baseresponse<ipresult>> Upload (
     @PartList   list<requstbody> List);
use:

Project address: https://github.com/Tamicer/RetrofitClient/commits/2.x API Posture

Android based on the Retrofit2.0+rxjava package of the Super Good retrofitclient tool class get

Retrofitclient.getinstance.
                    Createbaseapi (). Get
                    ("url", maps).
                    Subscribe (New Basesubscriber <T> (context) {});
POST
Retrofitclient.getinstance (Mainactivity.this)
                    . Createbaseapi ().
                    post (URL, maps)
                    . Subscribe (NEW Basesubscriber<t> (context) {});
JSON
   Requestbody jsonbody = 
               requestbody.create (okhttp3. Mediatype.parse ("Application/json; Charset=utf-8 "), New Gson (). Tojson (user));

   Retrofitclient.getinstance (Mainactivity.this)
                  . Createbaseapi ().
                  json ("url", jsonbody).
                  Subscribe (New basesubscriber<t> (context) {});
UpLoad
    Requestbody requestfile =
                    requestbody.create (mediatype.parse ("Image/jpg"), New File (MPath));

    Retrofitclient.getinstance (Mainactivity.this)
                   . Createbaseapi ().
                   upload (URL, requestfile).
                   Subscribe (New basesubscriber<t> (context) {
                    });
Download
  Retrofitclient.getinstance (Mainactivity.this)
  . Createbaseapi ()
  . Download (URL1, new CallBack () {


            );
Execute you apiservice define Myapiservice
Public interface Myapiservice {

    @GET ("service/getipinfo.php")
    flowable<baseresponse<ipresult> > GetData (@Query ("IP") String IP);
Create Apiservice
            Create you  apiservice
            myapiservice service = Retrofitclient.getinstance (). Create (Myapiservice.class);
Call
            Execute and add observable
            retrofitclient.getinstance (mainactivity.this)
                    . Switchschedulersmain ( Service.getdata ("21.22.11.33"))
                    . Subscribe (New basesubscriber<baseresponse<ipresult>> ( mainactivity.this) {

                        });

# more Rxjava

If you want to manipulate rxjava more operators:

  Retrofitclient.getinstance
                        . Createbaseapi () (
                        "service/getipinfo.php", maps).
                        Compose ( New Flowabletransformer () {
                            @Override public
                            Publisher apply (@NonNull flowable upstream) {return
                                null;
                            }
                        })
                        . Map (new Function () {
                            @Override public
                            Object apply (@NonNull Object o) throws Exception {return
                                null;
  }
                        })
                        . XXXXX ()//More
                        . Subscribe (New basesubscriber<baseresponse<ipresult>> () {

                            })
Cancel:
Flowable flowable = Retrofitclient.getinstance (). Createbaseapi (). Get ().
                   ubscription (
                   ). Unsubscribe ();

Rxjava combined with retrofit, how to gracefully cancel the request.
Elegant cancellation please see: http://blog.csdn.net/sk719887916/article/details/54575137 not gorgeous end

The same simple package for some getting started Rxjava friends, upload progress what can go to see me another project, this version of design is not necessarily perfect, just used to learn design ideas, like friends can direct source code to increase their own function, If you want to learn more posture friend recommend a I write frame: novate.

Novate
Https://github.com/Tamicer/Novate

Author Tamic
Original: http://blog.csdn.net/sk719887916/article/details/51958010

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.