Problems in the Retrofit2 + Rxjava __java

Source: Internet
Author: User

Environment Description:

Compile ' io.reactivex:rxandroid:1.1.0 '
compile ' io.reactivex:rxjava:1.1.0 '
compile ' Com.squareup.retrofit2:retrofit:2.0.0-beta4 '
compile ' com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4 '
compile ' com.squareup.okhttp3:logging-interceptor:3.2.0 '

Use Rxjava, rxandroid version 1.1.0
Using the Retrofit2 2.0.0-beta4 version

1, also need to increase OKHTTP3, and Okio dependence?
No, Retrofit2 has added Okhttp3,okio dependencies by default, so our project does not need to declare OKHTTP3, Okio dependencies.
2, how to increase debugging information.
Previous versions, there was a listener method on the retrofit object that could listen to retrofit and print some debug information, but in the BETA4 version there was no way to increase the monitoring, So you can only set up a interceptor on the OKHTTP3 to print some of the required logs.

Httplogginginterceptor logging = new Httplogginginterceptor ();
Set your desired log level
logging.setlevel (HttpLoggingInterceptor.Level.BODY);

Add your other interceptors
... Add logging as last Interceptor
okhttpclient client = new Okhttpclient.builder ()
           . Addinterceptor (logging)
           . Build ();

Retrofit RETROFIT = new Retrofit.builder ()
                . BaseURL (base_url).
                Client (client).
                Addcalladapterfactory ( Rxjavacalladapterfactory.create ())
                . Addconverterfactory (Fastjsonconverterfactory.create ())
                . Build ();

3, print out the log, only send, no receipts, with the browser can see the return value.

03-09 09:11:34.609 13471-13491/com.xxxxxx.carcare d/okhttp: <--OK http://192.168.0.46:8080/users/ 56c2c40d77ce6f9a3c73a09e (2692ms)
03-09 09:11:34.639 13471-13491/com.xxxxxx.carcare d/okhttp: <--End HTTP (82 -byte body)

This is because, for Rx's sake, RX will default to thread scheduling. The default thread is the RX line Cheng Chili, and the network request needs to be placed in the IO thread, and the returned results need to be manipulated in the main thread.
Need to configure thread scheduling on observable

. Observeon (Androidschedulers.mainthread ())
. Subscribeon (Schedulers.io ())

4, using Fastjson as a serialization tool
See my other article.

http://blog.csdn.net/soslinken/article/details/50832958

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.