public final class Retrofit extends Object
API endpoints are defined as methods on an interface with annotations providing metadata about the form in which the HTTP call should be made.
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
,
PUT
, POST
, PATCH
, HEAD
, and DELETE
. You can use a
custom HTTP method with @HTTP
.
Method parameters can be used to replace parts of the URL by annotating them with
@Path
. 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
@Query
.
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:
@FormUrlEncoded
- Form-encoded data with key-value
pairs specified by the @Field
parameter annotation.
@Multipart
- RFC 2387-compliant multi-part data with parts
specified by the @Part
parameter annotation.
Additional static headers can be added for an endpoint using the
@Headers
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
Converter.Factory
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); }
Calling create()
with CategoryService.class
will validate the
annotations and create a new implementation of the service definition.
Modifier and Type | Class and Description |
---|---|
static class |
Retrofit.Builder
Build a new
Retrofit . |
Modifier and Type | Method and Description |
---|---|
BaseUrl |
baseUrl() |
CallAdapter<?> |
callAdapter(Type returnType,
Annotation[] annotations)
|
List<CallAdapter.Factory> |
callAdapterFactories() |
Executor |
callbackExecutor() |
com.squareup.okhttp.OkHttpClient |
client() |
List<Converter.Factory> |
converterFactories()
TODO
|
<T> T |
create(Class<T> service)
Create an implementation of the API defined by the
service interface. |
CallAdapter<?> |
nextCallAdapter(CallAdapter.Factory skipPast,
Type returnType,
Annotation[] annotations)
|
<T> Converter<T,com.squareup.okhttp.RequestBody> |
requestConverter(Type type,
Annotation[] annotations)
|
<T> Converter<com.squareup.okhttp.ResponseBody,T> |
responseConverter(Type type,
Annotation[] annotations)
|
public <T> T create(Class<T> service)
service
interface.public com.squareup.okhttp.OkHttpClient client()
public BaseUrl baseUrl()
public List<CallAdapter.Factory> callAdapterFactories()
public CallAdapter<?> callAdapter(Type returnType, Annotation[] annotations)
public CallAdapter<?> nextCallAdapter(CallAdapter.Factory skipPast, Type returnType, Annotation[] annotations)
public List<Converter.Factory> converterFactories()
public <T> Converter<T,com.squareup.okhttp.RequestBody> requestConverter(Type type, Annotation[] annotations)
public <T> Converter<com.squareup.okhttp.ResponseBody,T> responseConverter(Type type, Annotation[] annotations)
public Executor callbackExecutor()
Copyright © 2015 Square, Inc.. All Rights Reserved.