#Retrofit
A type-safe REST client for Android and Java
Android中非常有名的网络框架
官网 :https://square.github.io/retrofit/
参考项目: https://square.github.io/okhttp/
Android 示例项目: https://github.com/goodev/RetrofitDemo
Retrofit vs Volley
https://instructure.github.io/blog/2013/12/09/volley-vs-retrofit/
提取一张图片
1 | //API |
1 | //RestClient |
1 | //MySSLTrust 的SSL信任 |
官网的几个例子:
API Declaration
Annotations on the interface methods and its parameters indicate how a request will be handled.
REQUEST METHOD
Every method must have an HTTP annotation that provides the request method and relative URL. There are five built-in annotations: GET, POST, PUT, DELETE, and HEAD. The relative URL of the resource is specified in the annotation.
1 | @GET("/users/list") |
You can also specify query parameters in the URL.
1 | @GET("/users/list?sort=desc") |
URL MANIPULATION
A request URL can be updated dynamically using replacement blocks and parameters on the method. A replacement block is an alphanumeric string surrounded by { and }. A corresponding parameter must be annotated with @Path using the same string.
@GET(“/group/{id}/users”)
List
Query parameters can also be added.
1 | @GET("/group/{id}/users") |
For complex query parameter combinations a Map can be used.
1 | @GET("/group/{id}/users") |
List
REQUEST BODY
An object can be specified for use as an HTTP request body with the @Body annotation.
@POST(“/users/new”)
void createUser(@Body User user, Callback
The object will also be converted using the RestAdapter’s converter.
FORM ENCODED AND MULTIPART
Methods can also be declared to send form-encoded and multipart data.
Form-encoded data is sent when @FormUrlEncoded is present on the method. Each key-value pair is annotated with @Field containing the name and the object providing the value.
@FormUrlEncoded
@POST(“/user/edit”)
User updateUser(@Field(“first_name”) String first, @Field(“last_name”) String last);
Multipart requests are used when @Multipart is present on the method. Parts are declared using the @Part annotation.
@Multipart
@PUT(“/user/photo”)
User updateUser(@Part(“photo”) TypedFile photo, @Part(“description”) TypedString description);
Multipart parts use the RestAdapter’s converter or they can implement TypedOutput to handle their own serialization.
HEADER MANIPULATION
You can set static headers for a method using the @Headers annotation.
1 | @Headers("Cache-Control: max-age=640000") |
1 | @GET("/users/{username}") |
User getUser(@Path(“username”) String username);
Note that headers do not overwrite each other. All headers with the same name will be included in the request.
A request Header can be updated dynamically using the @Header annotation. A corresponding parameter must be provided to the @Header. If the value is null, the header will be omitted. Otherwise, toString will be called on the value, and the result used.
1 | @GET("/user") |
Headers that need to be added to every request can be specified using a RequestInterceptor. The following code creates a RequestInterceptor that will add a User-Agent header to every request.
1 | RequestInterceptor requestInterceptor = new RequestInterceptor() { |
SYNCHRONOUS VS. ASYNCHRONOUS VS. OBSERVABLE
Methods can be declared for either synchronous or asynchronous execution.
A method with a return type will be executed synchronously.
1 | @GET("/user/{id}/photo") |
Photo getUserPhoto(@Path(“id”) int id);
Asynchronous execution requires the last parameter of the method be a Callback.
1 | @GET("/user/{id}/photo") |
void getUserPhoto(@Path(“id”) int id, Callback
On Android, callbacks will be executed on the main thread. For desktop applications callbacks will happen on the same thread that executed the HTTP request.
Retrofit also integrates RxJava to support methods with a return type of rx.Observable
1 | @GET("/user/{id}/photo") |
Observable
Observable requests are subscribed asynchronously and observed on the same thread that executed the HTTP request. To observe on a different thread (e.g. Android’s main thread) call observeOn(Scheduler) on the returned Observable.
RESPONSE OBJECT TYPE
HTTP responses are automatically converted to a specified type using the RestAdapter’s converter which defaults to JSON. The desired type is declared as the method return type or using the Callback or Observable.
1 | @GET("/users/list") |
1 | @GET("/users/list") |
1 | @GET("/users/list") |
For access to the raw HTTP response use the Response type.
1 | @GET("/users/list") |
1 | @GET("/users/list") |
1 | @GET("/users/list") |