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
101
votes
18 answers

No Retrofit annotation found. (parameter #1)

I want get RSS code from a URL with Retrofit and if I enter url staticly in the get annotation everything is OK but with dynamic url I get an error. My interface service : public interface AllNewsService { @GET("/fa/rss/{url}") void getRss(…
mk72
  • 1,307
  • 2
  • 8
  • 15
97
votes
4 answers

How to return 404 response status in Spring Boot @ResponseBody - method return type is Response?

I'm using Spring Boot with @ResponseBody based approach like the following: @RequestMapping(value = VIDEO_DATA_PATH, method = RequestMethod.GET) public @ResponseBody Response getData(@PathVariable(ID_PARAMETER) long id, HttpServletResponse res) { …
catch23
  • 17,519
  • 42
  • 144
  • 217
96
votes
15 answers

How to retry HTTP requests with OkHttp/Retrofit?

I am using Retrofit/OkHttp (1.6) in my Android project. I don't find any request retry mechanism built-in to either of them. On searching more, I read OkHttp seems to have silent-retries. I don't see that happening on any of my connections (HTTP or…
dev
  • 11,071
  • 22
  • 74
  • 122
94
votes
9 answers

@DELETE method is not supporting(Non-body HTTP method cannot contain @Body or @TypedOutput.)

@DELETE("/job/deletejob") Observable jobDelete( @Body JobDeleteRequestModel model); am getting this error: Non-body HTTP method cannot contain @Body or @TypedOutput
sandy
  • 943
  • 1
  • 6
  • 6
93
votes
7 answers

Retrofit GSON serialize Date from json string into java.util.date

I am using the Retrofit library for my REST calls. Most of what I have done has been smooth as butter but for some reason I am having issues converting JSON timestamp strings into java.util.Date objects. The JSON that is coming in looks like this.…
jpotts18
  • 4,951
  • 5
  • 31
  • 31
90
votes
7 answers

(Retrofit) Could not locate converter for class crashing app

So Retrofit 2.0.0 was recently released and theres not really any updated examples on how to use it, but im trying to implement it for a basic API call. Im getting a java.lang.IllegalArgumentException: Unable to create converter for class` caused…
Orbit
  • 2,985
  • 9
  • 49
  • 106
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
89
votes
12 answers

How to handle Dynamic JSON in Retrofit?

I am using the retrofit efficient networking library, but I am unable to handle Dynamic JSON which contains single prefix responseMessage which changes to object randomly, the same prefix ( responseMessage) changes to String in some cases…
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
88
votes
7 answers

Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY

I'm fairly new to JSON parsing, I'm using the Retrofit library of Square and ran into this problem. I'm trying to parse this JSON reponse: [ { "id": 3, "username": "jezer", "regid": "oiqwueoiwqueoiwqueoiwq", …
Jezer Crespo
  • 2,152
  • 3
  • 24
  • 27
88
votes
10 answers

Does OkHttp support accepting self-signed SSL certs?

I'm working for a customer who has a server with self-signed SSL cert. I'm using Retrofit + CustomClient using wrapped OkHttp client: RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Config.BASE_URL + Config.API_VERSION) …
cesards
  • 15,882
  • 11
  • 70
  • 65
87
votes
4 answers

Android Retrofit Parameterized @Headers

I am using OAuth and I need to put the OAuth token in my header every time I make a request. I see the @Header annotation, but is there a way to make it parameterized so i can pass in at run time? Here is the concept @Header({Authorization:'OAuth…
jpotts18
  • 4,951
  • 5
  • 31
  • 31
85
votes
4 answers

IllegalArgumentException in Retrofit / must not have replace block

I have the following code : @GET("api.php?company_name={name}") Call getRoms_center(@Query("name") String name); According to the official docs, I must use @Query, and i'm using it, but i'm getting the following error :…
Jaeger
  • 1,646
  • 8
  • 27
  • 59
85
votes
1 answer

Retrofit with Rxjava Schedulers.newThread() vs Schedulers.io()

What are the benefits to use Schedulers.newThread() vs Schedulers.io() in Retrofit network request. I have seen many examples that use io(), but I want to understand why. Example…
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
82
votes
6 answers

How to unit test Retrofit api calls?

I am trying to integrate Unit test cases for every chunk of code possible. But I am facing issues while adding test cases for api calls that are made through retrofit. The JUnit compiler never executes the code in the CallBack functions. There is…
AabidMulani
  • 2,325
  • 1
  • 28
  • 47
82
votes
5 answers

How to dynamically set headers in Retrofit (Android)

I am using an API that uses an authorization scheme that requires a special "X-Authorization" header to be set to authenticate the request. For example, this Retrofit setup works perfectly for the user whose auth token is…
johncorser
  • 9,262
  • 17
  • 57
  • 102