Retrofit--a type-safe HTTP Client for Android and Java (the first part)

Source: Internet
Author: User


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)

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.