First, introduce
Retrofit converts your HTTP interface API into a Java interface in your project.
Public interface Githubservice { @GET ("Users/{user}/repos") call<list<repo>> Listrepos (@Path (" User ") String user);
retrofit automatically generates an implementation class for the service above.
Retrofit Retrofit = new Retrofit.builder () . BASEURL ("https://api.github.com/") . Build (); Githubservice service = retrofit.create (Githubservice.class);
each time a call is made, the implementation class of the created interface produces an asynchronous or synchronous HTTP request to the remote Web server.
call<list<repo>> repos = Service.listrepos ("Octocat");
use annotations to describe HTTP requests:1, Support URL parameters and query parameters2, the object is converted to the request body, that is, support for the JSON object3, a variety of request body, documents and so also support (translation of the Good rustic ah ...) )
two, API declaration
Use the method annotations and request parameters on the interface to describe how a request is handled.
third, the request method
Each method must have an HTTP annotation to provide the request request and the associated URL. There are five types of supported annotations: Get,post,put,dlete,head. The associated request URL is placed in the request note.
@GET ("Users/list")
At the same time, you can also provide the request parameters directly in the URL:
@GET ("Users/list?sort=desc")
Four, URL action
Request parameters can be dynamically updated---placeholders in the by method. A request block is surrounded with ' {} '-this symbol. A corresponding parameter must be @Path annotated and use the same parameter name.
@GET ("Group/{id}/users") call<list<user>> grouplist (@Path ("id") int groupId);
Request parameters can also be added:
@GET ("Group/{id}/users") call<list<user>> grouplist (@Path ("id") int groupId, @Query ("sort") String sort);
For example, use a map below to make chestnuts:
@GET ("Group/{id}/users") call<list<user>> grouplist (@Path ("id") int groupId, @QueryMap map<string, string> options);
Part of the translation, it feels like the above things are written by aliens ... The baby will continue to be more! Hope to endure the translation finished, more hope to see the people do not hit me.
Retrofit--a type-safe HTTP Client for Android and Java (the first part)