public final class Retrofit
extends java.lang.Object
create(java.lang.Class<T>)
to generate an implementation.
For example,
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
MyApi api = retrofit.create(MyApi.class);
Response<User> user = api.getUser().execute();
Modifier and Type | Class and Description |
---|---|
static class |
Retrofit.Builder
Build a new
Retrofit . |
Modifier and Type | Method and Description |
---|---|
okhttp3.HttpUrl |
baseUrl()
The API base URL.
|
CallAdapter<?,?> |
callAdapter(java.lang.reflect.Type returnType,
java.lang.annotation.Annotation[] annotations)
|
java.util.List<CallAdapter.Factory> |
callAdapterFactories()
Returns a list of the factories tried when creating a callAdapter(Type,
Annotation[]) call adapter}.
|
java.util.concurrent.Executor |
callbackExecutor()
|
okhttp3.Call.Factory |
callFactory()
The factory used to create OkHttp calls for sending a HTTP requests.
|
java.util.List<Converter.Factory> |
converterFactories()
Returns an unmodifiable list of the factories tried when creating a request body converter, a response body converter, or a string converter.
|
<T> T |
create(java.lang.Class<T> service)
Create an implementation of the API endpoints defined by the
service interface. |
Retrofit.Builder |
newBuilder() |
CallAdapter<?,?> |
nextCallAdapter(CallAdapter.Factory skipPast,
java.lang.reflect.Type returnType,
java.lang.annotation.Annotation[] annotations)
|
<T> Converter<T,okhttp3.RequestBody> |
nextRequestBodyConverter(Converter.Factory skipPast,
java.lang.reflect.Type type,
java.lang.annotation.Annotation[] parameterAnnotations,
java.lang.annotation.Annotation[] methodAnnotations)
|
<T> Converter<okhttp3.ResponseBody,T> |
nextResponseBodyConverter(Converter.Factory skipPast,
java.lang.reflect.Type type,
java.lang.annotation.Annotation[] annotations)
|
<T> Converter<T,okhttp3.RequestBody> |
requestBodyConverter(java.lang.reflect.Type type,
java.lang.annotation.Annotation[] parameterAnnotations,
java.lang.annotation.Annotation[] methodAnnotations)
|
<T> Converter<okhttp3.ResponseBody,T> |
responseBodyConverter(java.lang.reflect.Type type,
java.lang.annotation.Annotation[] annotations)
|
<T> Converter<T,java.lang.String> |
stringConverter(java.lang.reflect.Type type,
java.lang.annotation.Annotation[] annotations)
|
public <T> T create(java.lang.Class<T> service)
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
, PUT
, POST
, PATCH
,
HEAD
, DELETE
and OPTIONS
. 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 @Url
.
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 2388-compliant multipart 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); }
public okhttp3.Call.Factory callFactory()
OkHttpClient
.public okhttp3.HttpUrl baseUrl()
public java.util.List<CallAdapter.Factory> callAdapterFactories()
public CallAdapter<?,?> callAdapter(java.lang.reflect.Type returnType, java.lang.annotation.Annotation[] annotations)
java.lang.IllegalArgumentException
- if no call adapter available for type
.public CallAdapter<?,?> nextCallAdapter(@Nullable CallAdapter.Factory skipPast, java.lang.reflect.Type returnType, java.lang.annotation.Annotation[] annotations)
java.lang.IllegalArgumentException
- if no call adapter available for type
.public java.util.List<Converter.Factory> converterFactories()
public <T> Converter<T,okhttp3.RequestBody> requestBodyConverter(java.lang.reflect.Type type, java.lang.annotation.Annotation[] parameterAnnotations, java.lang.annotation.Annotation[] methodAnnotations)
java.lang.IllegalArgumentException
- if no converter available for type
.public <T> Converter<T,okhttp3.RequestBody> nextRequestBodyConverter(@Nullable Converter.Factory skipPast, java.lang.reflect.Type type, java.lang.annotation.Annotation[] parameterAnnotations, java.lang.annotation.Annotation[] methodAnnotations)
java.lang.IllegalArgumentException
- if no converter available for type
.public <T> Converter<okhttp3.ResponseBody,T> responseBodyConverter(java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations)
java.lang.IllegalArgumentException
- if no converter available for type
.public <T> Converter<okhttp3.ResponseBody,T> nextResponseBodyConverter(@Nullable Converter.Factory skipPast, java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations)
java.lang.IllegalArgumentException
- if no converter available for type
.public <T> Converter<T,java.lang.String> stringConverter(java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations)
@Nullable public java.util.concurrent.Executor callbackExecutor()
public Retrofit.Builder newBuilder()