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
371
votes
22 answers

Logging with Retrofit 2

I'm trying to get the exact JSON that is being sent in the request. Here is my code: OkHttpClient client = new OkHttpClient(); client.interceptors().add(new Interceptor(){ @Override public com.squareup.okhttp.Response intercept(Chain chain)…
Gabor
  • 7,352
  • 4
  • 35
  • 56
321
votes
32 answers

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

I am trying to consume an API using Retrofit and Jackson to deserialize. I am getting the onFailure error No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator.
José Nobre
  • 4,407
  • 6
  • 20
  • 40
204
votes
31 answers

Retrofit 2.0 how to get deserialised error response.body

I'm using Retrofit 2.0.0-beta1. In tests i have an alternate scenario and expect error HTTP 400 I would like to have retrofit.Response response but response.body() == null MyError is not deserialised - i see it only…
Piotr Boho
  • 2,650
  • 2
  • 13
  • 20
198
votes
13 answers

POST Multipart Form Data using Retrofit 2.0 including image

I am trying to do a HTTP POST to server using Retrofit 2.0 MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain"); MediaType MEDIA_TYPE_IMAGE = MediaType.parse("image/*"); ByteArrayOutputStream byteArrayOutputStream = new…
JayVDiyk
  • 4,277
  • 22
  • 70
  • 135
165
votes
8 answers

How can I handle empty response body with Retrofit 2?

Recently I started using Retrofit 2 and I faced an issue with parsing empty response body. I have a server which responds only with http code without any content inside the response body. How can I handle only meta information about server response…
Yevgen Derkach
  • 1,688
  • 2
  • 11
  • 10
152
votes
13 answers

How to log request and response body with Retrofit-Android?

I can't find relevant methods in the Retrofit API for logging complete request/response bodies. I was expecting some help in the Profiler (but it only offers meta-data about response). I tried setting the log level in the Builder, but this doesn't…
dev
  • 11,071
  • 22
  • 74
  • 122
112
votes
4 answers

Unable to create call adapter for io.reactivex.Observable

I'm going to send a simple get method to my server(it is Rails app) and get the result using RxJava and Retrofit. The thing that I did is: My interface: public interface ApiCall { String SERVICE_ENDPOINT = "https://198.50.214.15"; …
Hussein Ojaghi
  • 2,260
  • 3
  • 23
  • 41
102
votes
3 answers

Retrofit 2 - URL Query Parameter

I am using a query parameters to set the values needed by the Google Maps API. The issue is I do not need the & sign for the first query parameter. @GET("/maps/api/geocode/json?") Call getLocationInfo(@Query("address") String…
Alan
  • 9,331
  • 14
  • 52
  • 97
101
votes
8 answers

retrofit 2 @path Vs @query

I am new to retrofit 2 library.I read several articles to get started as a beginner and I managed to fetch XML data from my RESTful API without specifying parameters.In my method that generated the XML resource is below.…
Mwesigye John Bosco
  • 1,148
  • 2
  • 11
  • 14
96
votes
17 answers

Is it possible to show progress bar when upload image via Retrofit 2?

I'am currently using Retrofit 2 and i want to upload some photo at my server. I know, that older version uses TypedFile class for uploading. And if we want to use progress bar with it we should override writeTo method in TypedFile class. Is it…
Yuriy Kolbasinskiy
  • 3,791
  • 3
  • 16
  • 33
94
votes
8 answers

How to upload an image file in Retrofit 2

I have an image of postman like below. How can I do the same thing in Retrofit 2? I've declared the interface like this: @Multipart @POST("/api/Pharmarcy/UploadImage") Call uploadPrescriptionImage( @Query("accessToken") String…
Azlan Jamal
  • 2,660
  • 3
  • 22
  • 29
91
votes
8 answers

Set dynamic base url using Retrofit 2.0 and Dagger 2

I'm trying to perform a login action using Retrofit 2.0 using Dagger 2 Here's how I set up Retrofit dependency @Provides @Singleton Retrofit provideRetrofit(Gson gson, OkHttpClient client) { Retrofit retrofit = new Retrofit.Builder() …
Renjith
  • 3,457
  • 5
  • 46
  • 67
89
votes
3 answers

Retrofit2 Android: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

I know this is not the first time someone asking about this problem but with Retrofit2 I can't find the right solution to my problem. I followed a online tutorial and it worked just fine. When I applied same code to my own endpoint i get this…
oxyt
  • 1,816
  • 3
  • 23
  • 33
82
votes
7 answers

Retrofit2 error java.io.EOFException: End of input at line 1 column 1

I called PATCH web service using Retrofit2 but onResponse is not called and the onFailure is called Despite of the operation of the service is succeeded on the server side perfectly And whenever,I tried to use fiddler to check the service its…
Mina Farid
  • 5,041
  • 4
  • 39
  • 46
78
votes
14 answers

Retrofit 2: Get JSON from Response body

I want to get string json from my api using retrofit 2, I have no problem when using retrofit 1 to get this json but using retrofit 2 returns null for me. This is what my json looks like {"id":1,"Username":"admin","Level":"Administrator"} This is…
Dirus
  • 1,033
  • 2
  • 14
  • 21
1
2 3
99 100