0

I am using Retrofit 2.0 for my project's api call. Every thing is working fine. Recently I came across a difficult scenario which I am unable to execute.

Working type of Retrofit Callback :

@GET
Call<***CountryPojo***> getCountries(@Header(headerAuthorization) String lang, @Url String url,@QueryMap Map<String, String> limit);

In the above callback "CountryPojo is the pojo class with getter and setter.

My requirement is to commonly keep a pojoclass Type, so that I can pass the particular pojo class into it and get back the callback.

For example:

@GET
Call<***CommonPojoClass***> getCountries(@Header(headerAuthorization) String lang, @Url String url,@QueryMap Map<String, String> limit);

How to do this ?. Any help should be greatly appreciated.

Xstian
  • 8,184
  • 10
  • 42
  • 72
Raghul Sugathan
  • 350
  • 1
  • 7
  • 22
  • What are you trying to do by using that? – Pankaj Kumar Dec 15 '15 at 13:40
  • I am trying to keep a single callback for my entire Api calls from different classes. Inorder to avoid boilerplate code and to enhance Reusablity. – Raghul Sugathan Dec 15 '15 at 13:43
  • Why would want to do anything like that? This is not about re-usability. If two APIs give different structure, they required 2 different POJOs. Mixing up in single POJOs, will only complicate things. And also, this won't avoid boilerplate code whatsoever. You still have to write all the code logics in the single class. – Rohit Jain Dec 15 '15 at 13:45
  • ok. Thanks for ur solutions. – Raghul Sugathan Dec 15 '15 at 13:47
  • From the website: **Retrofit A type-safe HTTP client for Android and Java** - The focus is on type safety. Why would you like to give that up? – LordRaydenMK Dec 15 '15 at 13:48
  • As others said, this is not a good approach. Anyway if you really want to do that you can achieve it with simple java inheritance - 'class CountryPojo extends CommonPojoClass' . Then you can pass your CountryPojo response everywhere you want as a CommonPojoClass – tochkov Dec 16 '15 at 10:59
  • Also have in mind that adding authorization header to **every** request is a great pain and an actual boilerplate. Take a look at [Interceptors](https://github.com/square/okhttp/wiki/Interceptors) and [how to use them](http://stackoverflow.com/questions/32963394/how-to-use-interceptor-to-add-headers-in-retrofit-2-0) – tochkov Dec 16 '15 at 11:02

0 Answers0