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
46
votes
6 answers

Retrofit Post Parameter

I am implementing login feature and for that using Post request but i am getting error saying "retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS" Below is my code import java.util.HashMap; import…
Dipen Patel
  • 911
  • 2
  • 9
  • 22
46
votes
6 answers

How can I return String or JSONObject from asynchronous callback using Retrofit?

For example, calling api.getUserName(userId, new Callback() {...}); cause: retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was…
lordmegamax
  • 2,684
  • 2
  • 26
  • 29
45
votes
5 answers

Retrofit Uploading multiple images to a single key

I am using Retrofit to upload images to my server. Here I need to upload multiple images for a single key. I have tried with Postman web client it is working well. Here is a screenshot. Here are the key value pairs for the request. SurveyImage :…
Kartheek
  • 7,104
  • 3
  • 30
  • 44
44
votes
5 answers

Retrofit API call receives "HTTP FAILED: java.io.IOException: Canceled"

Can't figure out why is this happening. Neither one of rx callbacks (onCompleted(), onError(), onNext()) not gets triggered by my call. The only thing i receive is this okhttp output: D/OkHttp: --> GET…
oleg.v
  • 1,065
  • 3
  • 11
  • 20
42
votes
3 answers

How can I debug my retrofit API call?

I'm using retrofit to get some data from the Flickr api. The method I'm making the call in looks like this: public static List getImageIds(int size) { Call call = flickrService.getPhotos(apiKey, format, "1"); …
intA
  • 2,513
  • 12
  • 41
  • 66
41
votes
8 answers

Retrofit-2 Content-Type Issue

My Api is accepting Content-Type application/json as headers. I set Header perfectly as mentioned in Retrofit Docs. @Headers("Content-Type: application/json") @POST("user/classes") Call addToPlaylist(@Body PlaylistParm parm); But in…
40
votes
2 answers

java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method ,when initializing Retrofit

I am using retrofit to get sample data from "https://jsonplaceholder.typicode.com/comments" but I am getting this error: 2020-03-31 16:33:12.011 8140-8140/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.retrofit_tutorial, PID:…
Pranav Choudhary
  • 2,726
  • 3
  • 18
  • 38
40
votes
11 answers

How to fix Expected Android API level 21+ but was 19 in Android

In my application i want get data from server, for get connect to server i used Retrofit, OkHttp. But when running application, show me force close error. In android api 21+ is not error, but below api 21 show me force close error. ApiClient class…
Jake warton
  • 2,163
  • 4
  • 11
  • 20
39
votes
4 answers

Parse JSON array response using Retrofit & Gson

Here is my JSONArray Response from Web service: [ { "sponsors": [ { "leg_id": "NYL000067", "type": "primary", "name": "AUBRY" }, { "leg_id": "NYL000171", "type": "cosponsor", …
Deep Shah
  • 1,334
  • 3
  • 20
  • 41
38
votes
5 answers

IllegalArgumentException: Could not locate call adapter for rx.Observable RxJava, Retrofit2

I am getting the above error while calling the rest api. I am using both retrofit2 and RxJava. ServiceFactory.java public class ServiceFactory { public static T createRetrofitService(final Class clazz, final String endpoint){ Retrofit…
Hemendra Sethi
  • 383
  • 1
  • 3
  • 5
37
votes
5 answers

Retrofit optional and required fields

When using Retrofit, I know you can use @FieldMap Map options to specify optional queries. Say that I have a api call that had 2 required fields and 3 optional fields. How would I format these calls? Would it be something like…
Orbit
  • 2,985
  • 9
  • 49
  • 106
36
votes
3 answers

How use Kotlin enum with Retrofit?

How can I parse JSON to model with enum? Here is my enum class: enum class VehicleEnumEntity(val value: String) { CAR("vehicle"), MOTORCYCLE("motorcycle"), VAN("van"), MOTORHOME("motorhome"), OTHER("other") } and I need to parse type…
Pasha Shkaran
  • 1,433
  • 2
  • 23
  • 41
35
votes
4 answers

Dagger + Retrofit. Adding auth headers at runtime

I'm wondering if there is a way for Dagger to know that it should recreate an object when new data is available. The instance I am speaking of is with the request headers I have for retrofit. At some point (when the user logs in) I get a token that…
AIntel
  • 1,087
  • 5
  • 14
  • 27
35
votes
7 answers

Add cookies to retrofit 2 request

I need to add cookies with retrofit 2.0. If i understand correct, cookies - the same as headers. this cookies must be added: private HashMap cookies = new HashMap(); cookies.put("sessionid", "sessionId"); cookies.put("token",…
Stan Malcolm
  • 2,740
  • 5
  • 32
  • 53
35
votes
3 answers

Retrofit2.0 gets MalformedJsonException while the json seems correct?

I am using retrofit:2.0.0-beta4 for my android app. I tried to add a user with Retrofit, the user is correctly created in Database, however I got the following error: 03-14 06:04:27.731 30572-30600/com.lehuo.lehuoandroid D/OkHttp: CALLING POST…
seaguest
  • 2,510
  • 5
  • 27
  • 45