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
3
votes
1 answer

What is the reasoning for separating the RestAdapter.build() and .create() methods when using Dagger?

I have been using Dagger/Retrofit for the past few months and have seen a common pattern of implementing an ApiModule class for an api. These ApiModules typically look something like this: @Provides @Singleton Client provideClient(OkHttpClient…
IZI_Shadow_IZI
  • 1,921
  • 4
  • 30
  • 59
3
votes
3 answers

Access retrofit server request and response time Android

I can see that whenever I make a web svc call, I get the response in log. But I am finding it difficult to actually access that information in my code. For eg how do I find out the server request time and response time that I can see from the…
SoH
  • 2,180
  • 2
  • 24
  • 53
3
votes
1 answer

Injecting mock Retrofit API service instance into ActivityInstrumentTestCase2

I'd like to write functional tests with Espresso for an Activity using a mock Retrofit API service instance created with a MockRestAdapter…
Matt Logan
  • 5,886
  • 5
  • 31
  • 48
3
votes
2 answers

Using Retrofit to Access an API with key/id

I haven't really found clear documentation on this, but I was wondering if anyone could point me towards something on using Retrofit (or OKHTTP or something if that's more appropriate) in order to pull down a JSON stream from an API that requires an…
Paul Ruiz
  • 2,396
  • 3
  • 27
  • 47
3
votes
2 answers

How can I avoid repeating common observable configuration?

I'm writing an API client in Android using Retrofit and this sort of code gets repeated a lot: myObservableFromRetrofit .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnError(... print stack trace ...) I'm…
ktusznio
  • 3,585
  • 3
  • 22
  • 21
3
votes
1 answer

Robospice-retrofit testing

I'm trying to do some unit-testing using this robospice-retofit example. The problem is i got some NullPointerException when I call the loadDataFromNetwork() method. Here's the testing code public class SampleRetrofitSpiceRequestTest extends…
3
votes
1 answer

Retrofit and persistent cookie store

What is the most simple way to implement persistent cookie store on retrofit? Now I am using this: cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); but…
Jonas
  • 4,683
  • 4
  • 45
  • 81
3
votes
2 answers

Convert json structure to array with Retrofit

I'm in trouble with Retrofit and a ugly json object in the Trakt.tv API: { "season": 1, "episodes": { "1": true, "2": true, "3": false, "4": false, "5": false, "6": false, "7": false …
Giorgio Gelardi
  • 993
  • 4
  • 13
  • 29
3
votes
1 answer

Retrofit POST with custom body param names

I'm consuming an API which has "private" as a param on the body on some POST action. @PATCH("/users/{facebookId}/plan/{myPlanId}") void updatePlan(@Path("facebookId") String facebookId, @Path("myPlanId") Integer myPlanId, @Body PlanParamUpdate…
Javier Manzano
  • 4,761
  • 16
  • 56
  • 86
3
votes
1 answer

Simple custom mapping of JSON property to object property with Retrofit

What is the simplest way to define a custom mapping of a JSON property to a particular object property in RetroFit? Example JSON response for a set of "rewards": [ { "name" : "$5 Voucher", "description" : "Get $5 off your next purchase", …
Ted Avery
  • 5,639
  • 3
  • 30
  • 29
3
votes
2 answers

Difference in ThreadPoolExecutor usage in OkHttp and Retrofit for async operations

OkHttp 2.0.0-RC1 uses ThreadPoolExecutor defined in Dispatcher#getExecutorService: executorService = new ThreadPoolExecutor( 0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), Util.threadFactory("OkHttp…
Wiktor Gworek
  • 486
  • 3
  • 7
3
votes
1 answer

Retrofit: on failure status code return null pointer.

I am using Retrofit for my backend communication: If the status code is not 200 then the callback call failure method. But I want to get the status code inside the failure method for further code conditioning @Override public void…
AabidMulani
  • 2,325
  • 1
  • 28
  • 47
3
votes
1 answer

How can I send a "fire and forget" async call with Retrofit?

I'd like to send async requests that never return a response (logging, business events, etc). Is this supported by Retrofit?
Chris Stewart
  • 3,303
  • 9
  • 36
  • 55
3
votes
1 answer

Handle gzip response with retrofit

I am calling a simple GET with retrofit to: https://api.stackexchange.com/2.2/sites I get following response: ---> HTTP GET https://api.stackexchange.com/2.2/sites ---> END HTTP (0-byte body) <--- HTTP 200 https://api.stackexchange.com/2.2/sites…
hsz
  • 148,279
  • 62
  • 259
  • 315
3
votes
1 answer

Format of POJO for nested JSON?

So lets say the JSON response is: [{ "data" : { "item1": value1, "item2:" value2 }}] How do you get the values 'value1' and 'value2' when you must first access data? If the fields were at the root then I could just have the method return a POJO…
rodly
  • 149
  • 3
  • 14