retrofit / retrofit2 / Retrofit / create

create

fun <T : Any!> create(service: Class<T>!): T

Create an implementation of the API endpoints defined by the service interface.

The relative path for a given method is obtained from an annotation on the method describing the request type. The built-in methods are GET, , POST, PATCH, HEAD, DELETE and . You can use a custom HTTP method with @HTTP. For a dynamic URL, omit the path on the annotation and annotate the first parameter with .

Method parameters can be used to replace parts of the URL by annotating them with . Replacement sections are denoted by an identifier surrounded by curly braces (e.g., "{foo}"). To add items to the query string of a URL use .

The body of a request is denoted by the @Body annotation. The object will be converted to request representation by one of the Converter.Factory instances. A RequestBody can also be used for a raw representation.

Alternative request body formats are supported by method annotations and corresponding parameter annotations:

Additional static headers can be added for an endpoint using the method annotation. For per-request control over a header annotate a parameter with @Header.

By default, methods return a Call which represents the HTTP request. The generic parameter of the call is the response body type and will be converted by one of the instances. ResponseBody can also be used for a raw representation. Void can be used if you do not care about the body contents.

For example:


    public interface CategoryService {
      @POST("category/{cat}/")
      Call<List<Item>> categoryList(@Path("cat") String a, @Query("page") int b);
    }