Questions tagged [retrofit]

Retrofit is a type-safe REST client for Android and Java by Square, Inc.

Retrofit turns REST API into a Java interface.

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

The RestAdapter class generates an implementation of the GitHubService interface.

RestAdapter restAdapter = new RestAdapter.Builder()
   .setEndpoint("https://api.github.com")
   .build();

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

Each call on the generated GitHubService makes an HTTP request to the remote webserver.

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

Annotations are used 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

The library is open sourced under Apache 2.0 license.

References

8574 questions
596
votes
10 answers

Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

Two-part question from an iOS developer learning Android, working on an Android project that will make a variety of requests from JSON to image to streaming download of audio and video: On iOS I have used the AFNetworking project extensively. Is…
Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74
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
352
votes
27 answers

How to POST raw whole JSON in the body of a Retrofit request?

This question may have been asked before but no it was not definitively answered. How exactly does one post raw whole JSON inside the body of a Retrofit request? See similar question here. Or is this answer correct that it must be form url encoded…
user3243335
  • 3,521
  • 3
  • 12
  • 4
270
votes
9 answers

When should one use RxJava Observable and when simple Callback on Android?

I'm working on networking for my app. So I decided to try out Square's Retrofit. I see that they support simple Callback @GET("/user/{id}/photo") void getUserPhoto(@Path("id") int id, Callback cb); and RxJava's…
Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
262
votes
9 answers

Retrofit 2 - Dynamic URL

With Retrofit 2, you can set a full URL in the annotation of a service method like : public interface APIService { @GET("http://api.mysite.com/user/list") Call getUsers(); } However, in my app, the URL of my webservices are not known at…
pdegand59
  • 12,776
  • 8
  • 51
  • 58
218
votes
11 answers

Refreshing OAuth token using Retrofit without modifying all calls

We are using Retrofit in our Android app, to communicate with an OAuth2 secured server. Everything works great, we use the RequestInterceptor to include the access token with each call. However there will be times, when the access token will expire,…
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
214
votes
10 answers

How to set timeout in Retrofit library?

I am using Retrofit library in my app, and I'd like to set a timeout of 60 seconds. Does Retrofit have some way to do this? I set Retrofit this way: RestAdapter restAdapter = new RestAdapter.Builder() .setServer(BuildConfig.BASE_URL) …
androidevil
  • 9,011
  • 14
  • 41
  • 79
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
183
votes
19 answers

Unable to create converter for my class in Android Retrofit library

Im migrating from using Volley to Retrofit, I already have gson class that I used before for converting JSONObject reponse to a object that implements gson annotations. When I'm trying to make http get request using retrofit but then my app crashes…
Earwin delos Santos
  • 2,965
  • 7
  • 20
  • 29
183
votes
19 answers

Unable to create call adapter for class example.Simple

I am using retrofit 2.0.0-beta1 with SimpleXml. I want the retrieve a Simple (XML) resource from a REST service. Marshalling/Unmarshalling the Simple object with SimpleXML works fine. When using this code (converted form pre 2.0.0 code): final…
rmuller
  • 12,062
  • 4
  • 64
  • 92
171
votes
10 answers

Adding header to all request with Retrofit 2

Retrofit 2's documentation says: Headers that need to be added to every request can be specified using an OkHttp interceptor. It can be done easily using the previous version, here's the related QA. But using retrofit 2, I couldn't find something…
Ashkan Sarlak
  • 7,124
  • 6
  • 39
  • 51
169
votes
16 answers

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

What is this error ? How can I fix this? My app is running but can't load data. And this is my Error: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ This is my fragment : public class news extends Fragment…
Erfan
  • 3,059
  • 3
  • 22
  • 49
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
159
votes
1 answer

Retrofit 2 removes characters after hostname from base url

I'm using Retrofit to access a RESTful api. The base url is: http://api.example.com/service This is the code for the interface: public interface ExampleService { @Headers("Accept: Application/JSON") @POST("/album/featured-albums") …
Ashkan Sarlak
  • 7,124
  • 6
  • 39
  • 51
1
2 3
99 100