Tutorial by Examples

We are going to be showing how to make a GET request to an API that responds with a JSON object or a JSON array. The first thing we need to do is add the Retrofit and GSON Converter dependencies to our module's gradle file. Add the dependencies for retrofit library as described in the Remarks secti...
Retrofit requests can be logged using an intercepter. There are several levels of detail available: NONE, BASIC, HEADERS, BODY. See Github project here. Add dependency to build.gradle: compile 'com.squareup.okhttp3:logging-interceptor:3.8.1' Add logging interceptor when creating Retrofit:...
Declare your interface with Retrofit2 annotations: public interface BackendApiClient { @Multipart @POST("/uploadFile") Call<RestApiDefaultResponse> uploadPhoto(@Part("file\"; filename=\"photo.jpg\" ") RequestBody photo); } Where RestApiDef...
This example shows how to use a request interceptor with OkHttp. This has numerous use cases such as: Adding universal header to the request. E.g. authenticating a request Debugging networked applications Retrieving raw response Logging network transaction etc. Set custom user agent Retrof...
The @Header and @Body annotations can be placed into the method signatures and Retrofit will automatically create them based on your models. public interface MyService { @POST("authentication/user") Call<AuthenticationResponse> authenticateUser(@Body AuthenticationReques...
Once you have setup the Retrofit environment in your project, you can use the following example that demonstrates how to upload multiple files using Retrofit: private void mulipleFileUploadFile(Uri[] fileUri) { OkHttpClient okHttpClient = new OkHttpClient(); OkHttpClient clientWith30sTime...
Interface declaration for downloading a file public interface ApiInterface { @GET("movie/now_playing") Call<MovieResponse> getNowPlayingMovies(@Query("api_key") String apiKey, @Query("page") int page); // option 1: a resource relative to your bas...
Add the following dependencies to your application. compile 'com.facebook.stetho:stetho:1.5.0' compile 'com.facebook.stetho:stetho-okhttp3:1.5.0' In your Application class' onCreate method, call the following. Stetho.initializeWithDefaults(this); When creating your Retrofit instance, crea...
Adding dependencies into the build.gradle file. dependencies { .... compile 'com.squareup.retrofit2:retrofit:2.1.0' compile ('com.thoughtworks.xstream:xstream:1.4.7') { exclude group: 'xmlpull', module: 'xmlpull' } .... } Then create Converter Factory public c...
Sample JSON: { "id": "12345", "type": "android" } Define your request: public class GetDeviceRequest { @SerializedName("deviceId") private String mDeviceId; public GetDeviceRequest(String deviceId) { this....
We will use retrofit 2 and SimpleXmlConverter to get xml data from url and parse to Java class. Add dependency to Gradle script: compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-simplexml:2.1.0' Create interface Also create xml class wrapper in our cas...

Page 1 of 1