Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Redirected from
/square/retrofit
Retrofit
https://github.com/lysine-dev/retrofit
Admin
Retrofit is a type-safe HTTP client for Android and Java.
Tokens:
87,468
Snippets:
632
Trust Score:
7.3
License:
Apache-2.0
Update:
1 week ago
Context
Chat
Benchmark
77.17
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Build Retrofit with RxJavaCallAdapterFactory Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/README.md Shows how to supply an adapter instance when building a Retrofit instance. The example uses RxJavaCallAdapterFactory.create() as the call adapter factory. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.example.com") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); ``` -------------------------------- ### Using GsonConverterFactory to build Retrofit Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/configuration.md Example of creating a Retrofit instance with GsonConverterFactory and generating a GitHubService implementation. Requires the converter-gson dependency. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); GitHubService service = retrofit.create(GitHubService.class); ``` -------------------------------- ### @GET("users/list") Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Declares a GET request to the relative URL 'users/list'. The HTTP method and relative URL are specified in the annotation. ```APIDOC ## GET users/list ### Description Declares a GET request to the relative URL 'users/list'. Static query parameters can be added directly to the URL. ### Method GET ### Endpoint users/list ### Parameters #### Query Parameters - **sort** (string) - Optional - Static query parameter specified directly in the URL. ### Request Example ```java @GET("users/list?sort=desc") ``` ### Response No response schema is specified in the source. ``` ```APIDOC ## [GET] group/{id}/users ### Description Dynamically updates the request URL using replacement blocks and path parameters. Query parameters can be added with @Query or @QueryMap. ### Method GET ### Endpoint `group/{id}/users` ### Parameters #### Path Parameters - **id** (int) - Required - The group ID used in the URL path. #### Query Parameters - **sort** (String) - Optional - Sort order for the user list. ### Request Example ```java @GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort); ``` ### Response Returns a `Call<List<User>>` object representing the asynchronous request. The response body will be a list of `User` objects when the request succeeds. ``` -------------------------------- ### Define a GitHubService interface with Retrofit annotations Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/index.md Defines an interface for the GitHub API using Retrofit annotations. The @GET annotation specifies the HTTP method and path, with @Path for URL parameter replacement. The method returns a Call<List<Repo>>. ```java public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); } ``` -------------------------------- ### Static query parameters in URL Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Specifies static query parameters directly in the relative URL of the GET annotation. ```java @GET("users/list?sort=desc") ``` -------------------------------- ### Define service method with Observable return type Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava2/README.md Example of a service interface method that returns an Observable<User>. This is possible after adding the RxJava2CallAdapterFactory. ```java interface MyService { @GET("/user") Observable<User> getUser(); } ``` -------------------------------- ### HTTP method annotation with relative URL Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Declares a GET request method with a relative URL. The URL is specified in the annotation. ```java @GET("users/list") ``` -------------------------------- ### Retrofit service method returning Call<User> Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-response-type-keeper/README.md Example Retrofit service method that returns a Call<User>. If the returned User instance is not used, R8 may remove it and replace the return type with Call<?>, causing runtime validation failure. ```java @GET("users/{id}") Call<User> getUser( @Path("id") String id); ``` -------------------------------- ### Generated keep rule for simple type Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-response-type-keeper/README.md Generated keep rule for the simple example. The annotation processor generates this rule for the type mentioned in the generic parameter position. ```proguard -keep com.example.User ``` -------------------------------- ### Add RxJavaCallAdapterFactory to Retrofit Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava/README.md Shows how to configure a Retrofit instance with RxJavaCallAdapterFactory.create() so that service methods can return RxJava types. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); ``` -------------------------------- ### Create Retrofit instance and generate service implementation Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/index.md Builds a Retrofit instance with a base URL and creates an implementation of the GitHubService interface. The generated service can be used to make HTTP requests. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); GitHubService service = retrofit.create(GitHubService.class); ``` -------------------------------- ### License Header Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/contributing.md Standard Apache License 2.0 header required for all contributed files. Copy this notice verbatim into each new source file. ```text Copyright 2013 Square, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` -------------------------------- ### Tag release version Source: https://github.com/lysine-dev/retrofit/blob/trunk/RELEASING.md Create an annotated tag for the release version. Replace X.Y.Z with the actual version number. ```bash $ git tag -am "Version X.Y.Z" X.Y.Z ``` -------------------------------- ### Add RxJava3CallAdapterFactory to Retrofit Builder Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava3/README.md Shows how to configure a Retrofit instance with RxJava3CallAdapterFactory.create() as a call adapter factory, enabling RxJava 3.x return types in service methods. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .build(); ``` -------------------------------- ### Create a Call for a specific user Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/index.md Creates a Call<List<Repo>> by invoking the listRepos method on the service. This Call can be used for synchronous or asynchronous HTTP requests. ```java Call<List<Repo>> repos = service.listRepos("octocat"); ``` -------------------------------- ### Add converter-jaxb3 dependency (Maven and Gradle) Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/jaxb3/README.md Add the converter-jaxb3 dependency to your project. The XML snippet is for Maven, the Groovy snippet is for Gradle. Replace 'latest.version' with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-jaxb3</artifactId> <version>latest.version</version> </dependency> ``` ```groovy implementation 'com.squareup.retrofit2:converter-jaxb3:latest.version' ``` -------------------------------- ### Maven dependency for retrofit-mock Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-mock/README.md Add this dependency to your Maven project to use the Retrofit Mock Web Server. Replace 'see.latest.version' with the actual latest version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit-mock</artifactId> <version>see.latest.version</version> </dependency> ``` -------------------------------- ### Add converter-kotlinx-serialization dependency via Gradle Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/kotlinx-serialization/README.md Gradle dependency for the converter-kotlinx-serialization artifact. Replace 'latest.version' with the actual version. ```groovy implementation 'com.squareup.retrofit2:converter-kotlinx-serialization:latest.version' ``` -------------------------------- ### Commit next development version Source: https://github.com/lysine-dev/retrofit/blob/trunk/RELEASING.md Commit the changes for the next development version after updating VERSION_NAME to a SNAPSHOT version. ```bash $ git commit -am "Prepare next development version" ``` -------------------------------- ### Gradle dependency for retrofit-mock Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-mock/README.md Add this dependency to your Gradle project to use the Retrofit Mock Web Server. Replace 'see.latest.version' with the actual latest version. ```groovy implementation 'com.squareup.retrofit2:retrofit-mock:see.latest.version' ``` -------------------------------- ### Create Retrofit with kotlinx.serialization converter Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/kotlinx-serialization/README.md Use Json.asConverterFactory() with a MediaType to add the converter to a Retrofit instance. The MediaType is used as the Content-Type header for request bodies. ```kotlin val retrofit = Retrofit.Builder() .baseUrl("https://example.com/") .addConverterFactory( Json.asConverterFactory( "application/json; charset=utf-8".toMediaType())) .build() ``` -------------------------------- ### Gradle dependency for adapter-rxjava2 Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava2/README.md Gradle dependency notation to include the adapter-rxjava2 artifact. Replace 'latest.version' with the actual version. ```groovy implementation 'com.squareup.retrofit2:adapter-rxjava2:latest.version' ``` -------------------------------- ### Gradle dependency for converter-moshi Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/moshi/README.md Add this dependency to your Gradle build to use the Moshi Converter. Replace 'latest.version' with the actual version. ```groovy implementation 'com.squareup.retrofit2:converter-moshi:latest.version' ``` -------------------------------- ### Maven dependency for adapter-rxjava2 Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava2/README.md Maven dependency XML to include the adapter-rxjava2 artifact. Replace 'latest.version' with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>adapter-rxjava2</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Maven dependency for adapter-rxjava Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava/README.md Maven dependency XML for adding the adapter-rxjava artifact. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>adapter-rxjava</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Gradle dependency for adapter-rxjava Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava/README.md Gradle dependency for adding the adapter-rxjava artifact. ```groovy implementation 'com.squareup.retrofit2:adapter-rxjava:latest.version' ``` -------------------------------- ### Multiple static headers with @Headers Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Sets multiple static headers using @Headers with an array. Headers do not overwrite each other. ```java @Headers({ "Accept: application/vnd.github.v3.full+json", "User-Agent: Retrofit-Sample-App" }) @GET("users/{username}") Call<User> getUser(@Path("username") String username); ``` -------------------------------- ### Path parameter replacement with @Path Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Uses a replacement block {id} in the URL and a corresponding @Path parameter to dynamically update the request URL. ```java @GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId); ``` -------------------------------- ### Push commits and tags Source: https://github.com/lysine-dev/retrofit/blob/trunk/RELEASING.md Push the commits and tags to the remote repository. This triggers a GitHub Action workflow that creates a GitHub release and uploads artifacts to Maven Central. ```bash $ git push && git push --tags ``` -------------------------------- ### Add Retrofit dependency with Gradle Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/download.mdx Add the Retrofit dependency to your Gradle build. The version is dynamically inserted from the site's environment variable. ```kotlin implementation("com.squareup.retrofit2:retrofit: ``` -------------------------------- ### Query parameter map with @QueryMap Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Uses a Map with @QueryMap for complex query parameter combinations. ```java @GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options); ``` -------------------------------- ### Gradle dependency for adapter-rxjava3 Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava3/README.md Gradle Groovy snippet to add the adapter-rxjava3 dependency using implementation 'com.squareup.retrofit2:adapter-rxjava3:latest.version'. ```groovy implementation 'com.squareup.retrofit2:adapter-rxjava3:latest.version' ``` -------------------------------- ### Maven dependency for adapter-rxjava3 Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava3/README.md Maven XML snippet to add the adapter-rxjava3 dependency with groupId com.squareup.retrofit2 and artifactId adapter-rxjava3. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>adapter-rxjava3</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Gradle dependency for converter-wire Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/wire/README.md Add this dependency to your Gradle build to use the Wire converter. Replace 'latest.version' with the actual version. ```groovy implementation 'com.squareup.retrofit2:converter-wire:latest.version' ``` -------------------------------- ### Add Retrofit dependency with Maven Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/download.mdx Add the Retrofit dependency to your Maven pom.xml. The version is dynamically inserted from the site's environment variable. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version> ``` -------------------------------- ### Add converter-kotlinx-serialization dependency via Maven Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/kotlinx-serialization/README.md Maven dependency for the converter-kotlinx-serialization artifact. Replace 'latest.version' with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-kotlinx-serialization</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Query parameter with @Query Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Adds a query parameter using @Query alongside a path parameter. ```java @GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort); ``` -------------------------------- ### Gradle Dependency for adapter-guava Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/guava/README.md Gradle dependency for the adapter-guava artifact. Replace latest.version with the actual version. ```groovy implementation 'com.squareup.retrofit2:adapter-guava:latest.version' ``` -------------------------------- ### Static headers with @Headers Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Sets a static header for a method using @Headers. Headers do not overwrite each other; all headers with the same name are included. ```java @Headers("Cache-Control: max-age=640000") @GET("widget/list") Call<List<Widget>> widgetList(); ``` -------------------------------- ### Commit release version Source: https://github.com/lysine-dev/retrofit/blob/trunk/RELEASING.md Commit the changes for the release version. Replace X.Y.Z with the actual version number. ```bash $ git commit -am "Prepare version X.Y.Z" ``` -------------------------------- ### Add RxJava2CallAdapterFactory to Retrofit Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/rxjava2/README.md Shows how to add RxJava2CallAdapterFactory as a Call adapter when building a Retrofit instance. This enables the use of RxJava 2.x types as return types in service methods. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); ``` -------------------------------- ### Gradle Dependency for adapter-scala Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/scala/README.md Gradle dependency for the adapter-scala artifact. Replace latest.version with the actual version. ```groovy implementation 'com.squareup.retrofit2:adapter-scala:latest.version' ``` -------------------------------- ### Maven dependency for converter-moshi Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/moshi/README.md Add this dependency to your Maven pom.xml to use the Moshi Converter. Replace 'latest.version' with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-moshi</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Header map with @HeaderMap Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Uses a Map with @HeaderMap for complex header combinations. ```java @GET("user") Call<User> getUser(@HeaderMap Map<String, String> headers) ``` -------------------------------- ### Maven Dependency for adapter-scala Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/scala/README.md Maven dependency for the adapter-scala artifact. Replace latest.version with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>adapter-scala</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Gradle dependency for converter-gson Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/gson/README.md Add this dependency to your Gradle build to use the Gson converter. Replace 'latest.version' with the actual version. ```groovy implementation 'com.squareup.retrofit2:converter-gson:latest.version' ``` -------------------------------- ### Maven dependency for converter-wire Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/wire/README.md Add this dependency to your Maven pom.xml to use the Wire converter. Replace 'latest.version' with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-wire</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Define Service Method with ListenableFuture Return Type Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/guava/README.md Defines a Retrofit service interface with a method returning ListenableFuture<User> for asynchronous requests. ```java interface MyService { @GET("/user") ListenableFuture<User> getUser(); } ``` -------------------------------- ### Gradle dependency for converter-jackson Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/jackson/README.md Gradle dependency for the converter-jackson artifact. Replace latest.version with the actual version. ```groovy implementation 'com.squareup.retrofit2:converter-jackson:latest.version' ``` -------------------------------- ### Maven Dependency for adapter-guava Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/guava/README.md Maven dependency for the adapter-guava artifact. Replace latest.version with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>adapter-guava</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Maven dependency for converter-protobuf Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/protobuf/README.md Add the converter-protobuf dependency to your Maven project to use Protocol Buffer binary serialization with Retrofit. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-protobuf</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Gradle dependency for converter-protobuf Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/protobuf/README.md Add the converter-protobuf dependency to your Gradle project to use Protocol Buffer binary serialization with Retrofit. ```groovy implementation 'com.squareup.retrofit2:converter-protobuf:latest.version' ``` -------------------------------- ### Gradle dependency for converter-jaxb Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/jaxb/README.md Add the converter-jaxb dependency to your Gradle build. Replace 'latest.version' with the actual version. ```groovy implementation 'com.squareup.retrofit2:converter-jaxb:latest.version' ``` -------------------------------- ### Gradle Java dependency for response-type-keeper Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-response-type-keeper/README.md Add the annotation processor to a Gradle Java project. Replace <version> with the actual version. ```groovy annotationProcessor 'com.squareup.retrofit2:response-type-keeper:<version>' ``` -------------------------------- ### Gradle dependency for converter-guava Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/guava/README.md Add the converter-guava dependency to a Gradle project. Replace latest.version with the actual version number. ```groovy implementation 'com.squareup.retrofit2:converter-guava:latest.version' ``` -------------------------------- ### Gradle Kotlin kapt dependency for response-type-keeper Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-response-type-keeper/README.md Add the annotation processor to a Gradle Kotlin project using kapt. Replace <version> with the actual version. ```groovy kapt 'com.squareup.retrofit2:response-type-keeper:<version>' ``` -------------------------------- ### XML format with XmlMapper Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/jackson/README.md Configure Retrofit to use Jackson's XmlMapper for XML serialization. Requires the jackson-dataformat-xml dependency. ```java import com.fasterxml.jackson.dataformat.xml.XmlMapper; Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .addConverterFactory( JacksonConverterFactory.create( new XmlMapper(), MediaType.get("application/xml"))) .build(); ``` -------------------------------- ### Gradle dependency for converter-simplexml Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/simplexml/README.md Add the converter-simplexml dependency to your Gradle build. Replace 'latest.version' with the actual version. On Android, exclude stax:stax-api, stax:stax, and xpp3:xpp3. ```groovy implementation 'com.squareup.retrofit2:converter-simplexml:latest.version' ``` -------------------------------- ### Maven dependency for converter-gson Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/gson/README.md Add this dependency to your Maven pom.xml to use the Gson converter. Replace 'latest.version' with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-gson</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Define Service Method Returning Future Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/scala/README.md Defines a Retrofit service interface with a method returning a Scala Future, made possible by the ScalaCallAdapterFactory. ```java interface MyService { @GET("/user") Future<User> getUser(); } ``` -------------------------------- ### CBOR format with CBORMapper Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/jackson/README.md Configure Retrofit to use Jackson's CBORMapper for CBOR serialization. Requires the jackson-dataformat-cbor dependency. ```java import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper; Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .addConverterFactory( JacksonConverterFactory.create( new CBORMapper(), MediaType.get("application/cbor"))) .build(); ``` -------------------------------- ### Maven dependency for converter-guava Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/guava/README.md Add the converter-guava dependency to a Maven project. Replace latest.version with the actual version number. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-guava</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Multipart request with @Multipart and @Part Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Sends multipart requests when @Multipart is present. Parts are declared with @Part and use Retrofit's converters or implement RequestBody for custom serialization. ```java @Multipart @PUT("user/photo") Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description); ``` -------------------------------- ### Maven dependency for converter-jackson Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/jackson/README.md Maven dependency for the converter-jackson artifact. Replace latest.version with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-jackson</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Maven dependency for converter-simplexml Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/simplexml/README.md Add the converter-simplexml dependency to your Maven pom.xml. Replace 'latest.version' with the actual version. Note that on Android, exclude stax:stax-api, stax:stax, and xpp3:xpp3. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-simplexml</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Add GsonConverterFactory to Retrofit Builder Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/README.md Use this snippet to add a converter factory (here GsonConverterFactory) when building a Retrofit instance. The converter must be supplied to the Retrofit.Builder via addConverterFactory(). ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.example.com") .addConverterFactory(GsonConverterFactory.create()) .build(); ``` -------------------------------- ### Maven dependency for converter-jaxb Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/jaxb/README.md Add the converter-jaxb dependency to your Maven pom.xml. Replace 'latest.version' with the actual version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-jaxb</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Dynamic header with @Header Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Dynamically updates a request header using @Header. If the value is null, the header is omitted; otherwise toString is called on the value. ```java @GET("user") Call<User> getUser(@Header("Authorization") String authorization) ``` -------------------------------- ### Kotlin suspend function returning Response Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Kotlin suspend function that directly returns a Response object, creating and asynchronously executing the call while suspending the current function. ```kotlin @GET("users") suspend fun getUser(): Response<User> ``` -------------------------------- ### Request body with @Body Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Specifies an object as the HTTP request body using @Body. The object is converted using a converter on the Retrofit instance; if no converter is added, only RequestBody can be used. ```java @POST("users/new") Call<User> createUser(@Body User user); ``` -------------------------------- ### Gradle dependency for converter-scalars Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/scalars/README.md Add the converter-scalars dependency to your Gradle build. Replace latest.version with the current release version. ```groovy implementation 'com.squareup.retrofit2:converter-scalars:latest.version' ``` -------------------------------- ### Kotlin suspend function returning body Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Kotlin suspend function that directly returns the body. If a non-2XX status is returned, an HttpException is thrown containing the response. ```kotlin @GET("users") suspend fun getUser(): User ``` -------------------------------- ### Add ScalaCallAdapterFactory to Retrofit Builder Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/scala/README.md Adds ScalaCallAdapterFactory as a Call adapter when building a Retrofit instance, enabling Scala Future return types in service methods. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .addCallAdapterFactory(ScalaCallAdapterFactory.create()) .build(); ``` -------------------------------- ### Add GuavaCallAdapterFactory to Retrofit Builder Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-adapters/guava/README.md Adds GuavaCallAdapterFactory as a Call adapter when building a Retrofit instance, enabling ListenableFuture return types in service methods. ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/") .addCallAdapterFactory(GuavaCallAdapterFactory.create()) .build(); ``` -------------------------------- ### Maven dependency for converter-scalars Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-converters/scalars/README.md Add the converter-scalars dependency to your Maven pom.xml. Replace latest.version with the current release version. ```xml <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-scalars</artifactId> <version>latest.version</version> </dependency> ``` -------------------------------- ### Generated keep rules for nested generics Source: https://github.com/lysine-dev/retrofit/blob/trunk/retrofit-response-type-keeper/README.md Generated keep rules for nested generics like Call<ApiResponse<User>>. Both the outer and inner types are kept. ```proguard -keep com.example.ApiResponse -keep com.example.User ``` -------------------------------- ### Form-encoded request with @FormUrlEncoded and @Field Source: https://github.com/lysine-dev/retrofit/blob/trunk/website/src/content/docs/declarations.md Sends form-encoded data when @FormUrlEncoded is present. Each key-value pair is annotated with @Field. ```java @FormUrlEncoded @POST("user/edit") Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last); ```