Retrofit is a type-safe HTTP client for Android and Java, produced by Square, based on the Okhttp Network service.
RETROFIT2.0: The biggest improvement ever made using Retrofit2.0
1* BaseURL and annotations in URL-connected "/" should preferably be written in the back of BaseURL, rather than in front of the URL in the annotation, otherwise unpredictable errors may occur. * API Declaration
The annotations and parameters of the interface function indicate how to handle the request request method
Each function must have HTTP annotations that provide the request and relative URLs, and retrofit provides 5 built-in annotations: GET, POST, PUT, delete, and head, the relative URL of the resource specified in the annotation
@GET ("Users/list")
1
1 2The object will be converted by the converter specified by the Retroofit instance, and if no converter is added, only requestbody is available. (the addition of converters is described later) FORM encoded and MULTIPART
Functions can also be declared to send form-encoded and multipart data.
When the function has @formurlencoded annotations, the form-encoded data is sent, each key-value pair is annotated with the @field annotation containing the name and the object providing the value.
@FormUrlEncoded
@POST ("User/edit")
call<user> UpdateUser (@Field ("first_name") String First, @Field (" Last_Name ") String last);
1 2 3
1 2 3 4 5 6It is important to note that headers cannot be overwritten with each other. All headers with the same name will be included in the request.
You can use the @header annotation to dynamically update a requested header. The @header must be given the appropriate parameters, if the value of the argument is NULL header will be ignored, otherwise call the parameter value of the ToString () method and use the return result
@GET ("user")
call<user> GetUser (@Header ("Authorization") String Authorization)
1 2
1 2 3 4 5 6 7 8 9 All in one, synchronous VS asynchronousEach call instance can be executed synchronously (Call.excute ()) or asynchronously (Call.enquene (callback<?> CallBack)), and each instance can be used only once, but the clone () function to create a new, available instance.
You can also use callback to define an asynchronous method:
@GET ("User/{id}/photo")
void Getuserphoto (@Path ("id") int ID, callback<photo> CB);
1 2
1 2On Android, the callback is executed on the main thread; on the JVM, the callback is executed on the thread that sent the HTTP request. Retrofit Configuration
The retrofit is a callable object that is converted through the API interface, which has a reasonable configuration and can be customized by default. Converters
By default, retrofit can only deserialize the responsebody type of the HTTP body as Okhttp, and can only accept parameters of the Responsebody type as @body.
Add converters can support other types, retrofit provides six sibling modules for easy adaptation to popular serialization libraries: Gson:com.squareup.retrofit:converter-gson Jackson: Com.squareup.retrofit:converter-jackson Moshi:com.squareup.retrofit:converter-moshi Protobuf: Com.squareup.retrofit:converter-protobuf Wire:com.squareup.retrofit:converter-wire Simple XML: Com.squareup.retrofit:converter-simplexml Custom Converters
You can easily create your own converters if you need to use retrofit to communicate in an out-of-the-box content format (such as YAML, TXT, custom format) and APIs, or if you want to use a different library to implement a format that already exists. How to create: Creates a new class that inherits the Converter.factory class and passes in the converter instance when the retrofit instance is built.
Retrofit Retrofit = new Retrofit.builder ()
. BASEURL (Base_url)
. Addcalladapterfactory ( Rxjavacalladapterfactory.create ())//Use Rxjava as the callback adapter
. Addconverterfactory (Gsonconverterfactory.create ())// Use Gson as the data converter
. Build ();
1 2 3) 4 5
1 2Now you can do a lot of things, not only to get the data but also to change the data.
Retrofit support for observable makes it easy to combine multiple rest calls together. For example, we have a call to get a photo, a second call to get the native data, and we can package the results together.
Observable.zip (
Service.getuserphoto (ID),
service.getphotometadata (ID),
(photo, metadata), Createphotowithdata (photo, metadata))
. Subscribe (Photowithdata-Showphoto (Photowithdata));
1 2 3) 4 5
1 2 3) 4 5Rxjava + Retrofit makes it easier to combine multiple rest calls into one.
Rxjava Project homepage
Rxandroid Project Home RxJava Tutorial: RxJava for Android developers RxJava (i: Basic) Easy to RxJava (ii: operator) in Layman's RxJava three – the benefits of responsiveness In layman's Rxjava four-using responsive programming retrofit code obfuscation configuration in Android
-dontwarn retrofit.**
-keep class retrofit.** {*;}
-keepattributes Signature
-keepattributes Exceptions
1 2 3) 4 5