Quickly get started with Retrofit+rxjava+okhttp+fastjson's network framework [RRO]

Source: Internet
Author: User
Tags throwable

Retrofit is known for its flexible invocation form and strong Extensibility. With the introduction of rxandroid, the pluggable network framework such as retrofit is more popular because it can be flexibly compatible with various data parsers, and the callback form (mostly or rxjava).

But! RETROFIT2 official has a bunch of implementations that integrate Third-party JSON parsing, such as Gson-converter and so On. But there is no fastjson, which makes me very puzzled. So I have to imitate Gson-converter wrote a fastjson-converter, and encapsulated as an easier to use for Android Platform Network framework Rro (name from retrofit+rxjava+okhttp~).

GitHub address: https://github.com/panespanes/RRO/

The Rro framework masks Retrofit's slightly complex configuration while preserving the retrofit features, and a single line of code prepares the network request:

Rro.getapiservice (yourapi.  Class);

next, As with retrofit, the network request can be made by YOURAPI this locally defined Interface.

------

We use practical examples to demonstrate,

Http://api.github.com/is a public restful API that asks for this address to get the json-formatted data returned by github, showing how to complete this network request with Rro and finally get the return Value.

 public StaticString Api_url = "https://api.github.com";//Defining interface Addresses  public InterfaceGitHub {//As with retrofit, define a local interface@GET ("/") ObservableIndex (); } GitHub GitHub= Rro.getapiservice (GitHub).class, api_url);//to get a packaged interface instance, you can then do the network request just as you would call the local interface Method.Pager//consistent with retrofit usage, call local methodCall.enqueue (Newcallback//Asynchronous Execution@Override public voidOnresponse (callResponse) {        //the response, the interface, returns the result of the data being parsed by Fastjson.} @Override public voidOnFailure (callcall , Throwable T) {LOG.D ("RRO", "onfail:" +T.getmessage ()); }  });

Of course, don't forget to quote in Gradle:

Define a MAVEN address in project Build.gradle

Allprojects {    repositories {        ...        Maven {url "https://jitpack.io"}}    }

Add a reference to the module

Dependencies {    compile ' com.github.panespanes:rro:44890e7717 '}

------------

How to use Rxjava ~ ~

1Rro.setapiurl (api_url);//also Firstset the request address (if you have previously set this step can be ignored)2 3  public InterfaceRxgithub {//return value call changed to Rxjava observalbe type4@GET ("/")5observableIndex ();6 }7 8Rxgithub Apiservice = Rro.getapiservice (rxgithub.class);9 Apiservice.index ()Ten. Subscribeon (schedulers.io ())//send thread managed by Rxjava one. Observeon (androidschedulers.mainthread ())//callback in the main thread a. Subscribe (Newsubscriber() { - @Override -                public voidoncompleted () { the  -               } -  - @Override +                public voidonError (throwable E) { -  +               } a  at @Override -                public voidOnNext (hashmap<string, string>HashMap) { -                     //This returns the parse result of Fastjson -               } -});

It's so simple! Let's get it right.

Quickly get started with Retrofit+rxjava+okhttp+fastjson's network framework [RRO]

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.