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
158
votes
5 answers

why use Retrofit when we have OkHttp

With OkHttp we can make HTTP request then get response from server: OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); Then with Gson…
Mehrdad Faraji
  • 3,744
  • 3
  • 25
  • 38
158
votes
7 answers

Can Retrofit with OKHttp use cache data when offline

I'm trying to use Retrofit & OKHttp to cache HTTP responses. I followed this gist and, ended up with this code: File httpCacheDirectory = new File(context.getCacheDir(), "responses"); HttpResponseCache httpResponseCache = null; try { …
osrl
  • 8,168
  • 8
  • 36
  • 57
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
139
votes
8 answers

How should I handle "No internet connection" with Retrofit on Android

I'd like to handle situations when there is no internet connection. Usually I'd run: ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork =…
AlexV
  • 3,836
  • 7
  • 31
  • 37
135
votes
1 answer

How to get response as String using retrofit without using GSON or any other library in android

I am trying to get response from the following Api : https://api.github.com/users/username But I don't know how to get response as String so that I can use the String to parse and get the JSONObject. Retrofit version used: retrofit:2.0.0-beta1 I…
Aakash
  • 5,181
  • 5
  • 20
  • 37
124
votes
11 answers

How to add headers to OkHttp request interceptor?

I have this interceptor that i add to my OkHttp client: public class RequestTokenInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // Here where we'll…
Morteza Rastgoo
  • 6,772
  • 7
  • 40
  • 61
121
votes
3 answers

Get response status code using Retrofit 2.0 and RxJava

I'm trying to upgrade to Retrofit 2.0 and add RxJava in my android project. I'm making an api call and want to retrieve the error code in case of an error response from the server. Observable apiCall(@Body body); And in the…
A.B.
  • 1,441
  • 3
  • 12
  • 10
121
votes
4 answers

How to use interceptor to add Headers in Retrofit 2.0?

Our team decide to adopt Retrofit 2.0 and I'm doing some initial research on it. I'm a newbie to this library. I'm wondering how to use interceptor to add customized headers via Retrofits 2.0 in our Android app. There are many tutorials about using…
hackjutsu
  • 8,336
  • 13
  • 47
  • 87
121
votes
5 answers

Retrofit and GET using parameters

I am trying to send a request to the Google GeoCode API using Retrofit. The service interface looks like this: public interface FooService { @GET("/maps/api/geocode/json?address={zipcode}&sensor=false") void…
Perry Hoekstra
  • 2,687
  • 3
  • 33
  • 52
120
votes
10 answers

Call another rest api from my server in Spring-Boot

I want to call another web-api from my backend on a specific request of user. For example, I want to call Google FCM send message api to send a message to a specific user on an event. Does Retrofit have any method to achieve this? If not, how I can…
Mahdi
  • 6,139
  • 9
  • 57
  • 109
120
votes
6 answers

Retrofit2 Authorization - Global Interceptor for access token

I'm trying to use Retrofit2, I want to add Token to my Header Like this: Authorization: Bearer Token but the code below doesn't work: public interface APIService { @Headers({"Authorization", "Bearer "+ token}) …
farshad
  • 1,339
  • 2
  • 9
  • 14
117
votes
13 answers

Get nested JSON object with GSON using retrofit

I'm consuming an API from my android app, and all the JSON responses are like this: { 'status': 'OK', 'reason': 'Everything was fine', 'content': { < some data here > } The problem is that all my POJOs have a status, reason…
mikelar
  • 1,173
  • 3
  • 8
  • 4
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
105
votes
11 answers

Square retrofit server mock for testing

What's the best way to mock a server for testing when using the square retrofit framework. Potential ways: Create a new retrofit client and set it in the RestAdapter.Builder().setClient(). This involves parsing the Request object and returning the…
Alec Holmes
  • 3,625
  • 4
  • 22
  • 23
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