Questions tagged [retrofit2]

Retrofit2 is the second version of the type-safe REST client Retrofit for Android and Java. It is developed by Square, Inc.

To use it with Maven:

<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>retrofit</artifactId>
  <version>2.3.0</version>
</dependency>

Or with Gradle:

compile 'com.squareup.retrofit2:retrofit:2.X.X'

It requires minimum Android 2.3 (Gingerbread) and Java 7 to use it.

If you are using ProGuard add following options:

# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain service method parameters.
-keepclassmembernames,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

How to use it:

Retrofit turns your HTTP API into a Java interface.

public interface GitHubService {
  @GET("users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);
}

This Retrofit class generates an implementation of the GitHubService interface.

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

GitHubService service = retrofit.create(GitHubService.class);

Each call from the created GitHubService can make a synchronous or asynchronous HTTP request to the remote web server.

Call<List<Repo>> repos = service.listRepos("octocat");

Use annotations to describe the HTTP request:

  • URL parameter replacement and query parameter support
  • Object conversion to request body (e.g., JSON, protocol buffers)
  • Multipart request body and file upload
7553 questions
70
votes
5 answers

How to make multiple request and wait until data is come from all the requests in retrofit 2.0 - android

current code: Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constant.BASEURL) .addConverterFactory(GsonConverterFactory.create()) .build(); APIService service =…
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
66
votes
1 answer

Retrofit error-Missing either @GET URL or @Url parameter

I am working on Youtube API. The base URL is Request :GET https://www.googleapis.com/youtube/v3/search?part=snippet&q={search_keyword}&key={API_KEY} ApiService Interface code- public interface…
ankushbbbr
  • 867
  • 1
  • 6
  • 15
65
votes
4 answers

How to get Retrofit success response status codes

I am not able to get success response status code from response like 200,201.. etc. As we can easily get error codes from RetrofitError class like error.isNetworkError() and error.getResponse().getStatus(). Is there any workaround for getting status…
Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
64
votes
9 answers

How to get string response from Retrofit2?

I am doing android, looking for a way to do a super basic http GET/POST request. I keep getting an error: java.lang.IllegalArgumentException: Unable to create converter for class java.lang.String Webservice: public interface WebService { …
Matthew Darnell
  • 1,315
  • 2
  • 12
  • 24
59
votes
7 answers

How to Exponential Backoff retry on kotlin coroutines

I am using kotlin coroutines for network request using extension method to call class in retrofit like this public suspend fun Call.await(): T { return suspendCancellableCoroutine { continuation -> enqueue(object : Callback
shakil.k
  • 1,623
  • 5
  • 17
  • 27
59
votes
3 answers

Unsupported operation: Android, Retrofit, OkHttp. Adding interceptor in OkHttpClient

I am trying to add token based authentication via Retrofit 2.0-beta3 and OkHttpClient in Android using interceptors. But I get UnsupportedOperationException when I add my interceptor in OkHttpClient. Here is my code: In ApiClient.java public…
Usman khan
  • 841
  • 1
  • 9
  • 12
58
votes
4 answers

Retrofit 2.0 throwing "IllegalArgumentException: @Field parameters can only be used with form encoding". How to do right API query and fix it?

My problem is that I don't know how to start using Retrofit 2.0 with received API - mentioned below... Firstly, I need to username, password, fbID (optional), gmailID (optional), twitID (optional), gender, birthDate, location (not required - if long…
y07k2
  • 1,898
  • 4
  • 20
  • 36
58
votes
8 answers

Retrofit 2 with only form-data

I am trying to make POST request using the Retrofit 2. The request type is form-data NOT application/x-www-form-urlencoded. I am only posting data not the files in the request and the response is in the form of JSON. I have tried @FormUrlEncoded,…
AkshayT
  • 2,901
  • 2
  • 28
  • 32
54
votes
2 answers

NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/Platform

I'm using Retrofit2 library. I already tried to update latest version : Retrofit2, Gson, Rxjava, OKHttp, HttpLoggingInterceptor ... in build.gradle file build.grade in application dependencies { compile fileTree(dir: 'libs', include:…
Huy Tower
  • 7,769
  • 16
  • 61
  • 86
53
votes
3 answers

Android Unit Test with Retrofit2 and Mockito or Robolectric

Can I test real response from retrofit2beta4? Do i need Mockito or Robolectic? I don't have activities in my project, it will be a library and I need to test is server responding correctly. Now I have such code and stuck... @Mock ApiManager…
AndrewS
  • 7,418
  • 8
  • 35
  • 50
52
votes
4 answers

Retrofit and OkHttp basic authentication

I am trying to add basic authentication (username and password) to a Retrofit OkHttp client. This is the code I have so far: private static Retrofit createMMSATService(String baseUrl, String user, String pass) { HttpLoggingInterceptor…
4ndro1d
  • 2,926
  • 7
  • 35
  • 65
52
votes
2 answers

Android Retrofit 2, differences between addInterceptor & addNetworkInterceptor for editing responses

I've been trying to implement an interceptor ( OkHttp 3.2 & Retrofit 2 ) for editing the JSON response before is returned as response. The server we request data returns different data dependes on success or error and that makes difficult to map the…
Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60
51
votes
2 answers

Why is RxJava often used with Retrofit?

What is the benefit of using Retrofit in combination with Rxjava?
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
50
votes
5 answers

How to handle 204 response in Retrofit using Kotlin Coroutines?

I'm using Retrofit 2.7.1 with Kotlin coroutines. I have a Retrofit service defined as such: @PUT("/users/{userId}.json") suspend fun updateUserProfile( @Path("userId") userId: String, @Query("display_name") displayName: String) :…
PhillyTheThrilly
  • 1,562
  • 2
  • 16
  • 21
49
votes
5 answers

How to set timeout in Retrofit-2.0+ android

I referred this link but I can't seem to implement for mine I am using compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2' I am using the below code, How to set timeout for this ! public class…
Devrath
  • 42,072
  • 54
  • 195
  • 297