Add Header Parameter in Retrofit - android

I'm trying to call an API which requires me to pass in an API key.
My Service call using HttpURLConnection is working perfectly.
url = new URL("https://developers.zomato.com/api/v2.1/search?entity_id=3&entity_type=city&q=" + params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("user-key","9900a9720d31dfd5fdb4352700c");
if (urlConnection.getResponseCode() != 200) {
Toast.makeText(con, "url connection response not 200 | " + urlConnection.getResponseCode(), Toast.LENGTH_SHORT).show();
Log.d("jamian", "url connection response not 200 | " + urlConnection.getResponseCode());
throw new RuntimeException("Failed : HTTP error code : " + urlConnection.getResponseCode());
}
However, I'm not sure how this works with Retrofit as my call in going into Failure at all times.
Here's the code I'm using for the same service call
#GET("search")
Call<String> getRestaurantsBySearch(#Query("entity_id") String entity_id, #Query("entity_type") String entity_type, #Query("q") String query,#Header("Accept") String accept, #Header("user-key") String userkey);
and I'm using this to call it
Call<String> call = endpoint.getRestaurantsBySearch("3","city","mumbai","application/json","9900a9720d31dfd5fdb4352700c");
All these calls are going into the OnFailure Method in RetroFit.
If I send it without the HeaderParameters it goes into Success with a 403 because I obviously need to pass the api key somewhere but I cant figure out how.
#GET("search")
Call<String> getRestaurantsBySearch(#Query("entity_id") String entity_id, #Query("entity_type") String entity_type, #Query("q") String query);
The error I'm getting in OnFailure is
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $

Try this type header for Retrofit 1.9 and 2.0. For the JSON content type.
#Headers({"Accept: application/json"})
#POST("user/classes")
Call<playlist> addToPlaylist(#Body PlaylistParm parm);
You can add many more headers, i.e,
#Headers({
"Accept: application/json",
"User-Agent: Your-App-Name",
"Cache-Control: max-age=640000"
})
Dynamically add to headers:
#POST("user/classes")
Call<ResponseModel> addToPlaylist(#Header("Content-Type") String content_type, #Body RequestModel req);
Call your method, i.e.,
mAPI.addToPlayList("application/json", playListParam);
Or
Want to pass every time, then create an HttpClient object with the HTTP Interceptor:
OkHttpClient httpClient = new OkHttpClient();
httpClient.networkInterceptors().add(new Interceptor() {
#Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request.Builder requestBuilder = chain.request().newBuilder();
requestBuilder.header("Content-Type", "application/json");
return chain.proceed(requestBuilder.build());
}
});
Then add to a Retrofit object
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).client(httpClient).build();
If you are using Kotlin, remove the { }. Else it will not work.

You can use the below
#Headers("user-key: 9900a9720d31dfd5fdb4352700c")
#GET("api/v2.1/search")
Call<String> getRestaurantsBySearch(#Query("entity_id") String entity_id, #Query("entity_type") String entity_type, #Query("q") String query);
and
Call<String> call = endpoint.getRestaurantsBySearch("3","city","cafes");
The above is based in the zomato api which is documented at
https://developers.zomato.com/documentation#!/restaurant/search
Thing to note is the end point change api/v2.1/search and the Header #Headers("user-key: 9900a9720d31dfd5fdb4352700c").
Also check your base url .baseUrl("https://developers.zomato.com/")
Also i tried the above with a api key i generated and it works
and my query was cafes as suggested the zomato documentation.
Note : I hope you have the below
.addConverterFactory(ScalarsConverterFactory.create()) // for string conversion
.build();
and the below in build.gradle file
compile group: 'com.squareup.retrofit2', name: 'converter-scalars', version: '2.2.0'
Edit:
You can also pass header with dynamic value as below
#GET("api/v2.1/search")
Call<String> getRestaurantsBySearch(#Query("entity_id") String entity_id, #Query("entity_type") String entity_type, #Query("q") String query,#Header("user-key") String userkey);
And
Call<String> call = endpoint.getRestaurantsBySearch("3","city","cafes","9900a9720d31dfd5fdb4352700c");

After trying a couple of times i figured out the answer.
The error
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
was coming due the failure of parsing the json.
In the method call I was passing a String instead of a POJO class.
#Headers("user-key: 9900a9720d31dfd5fdb4352700c")
#GET("api/v2.1/search")
Call<String> getRestaurantsBySearch(#Query("entity_id") String entity_id, #Query("entity_type") String entity_type, #Query("q") String query);
I should have passed instead of Call<String> the type of Call<Data>
Data being the Pojo class
something like this
#Headers("user-key: 9900a9720d31dfd5fdb4352700c")
#GET("api/v2.1/search")
Call<Data> getRestaurantsBySearch(#Query("entity_id") String entity_id, #Query("entity_type") String entity_type, #Query("q") String query);

As far as i can see you are passing the data in a wrong way.
Your method getRestaurantsBySearch is accepting the last two parameter as header field i.e accept and user-key. But while calling the method you are passing headers first.
Pass the data as you have declared it in method signature of getRestaurantsBySearch

Please take a look at the response. It clearly shows that the api key you provided is wrong. At first you get the correct api key. Then call the request it will work
.

Let me also comment a little bit (actually a lot) about adding headers in Kotlin focusing on Dependency Injection.
The best approach would be to provide both OkHttpClient and HttpLoggingInterceptor on the same di method making use of the handy Kotlin Scoping function in this case also and apply.
We will be needing these Retrofit (2.9) and OkHttpClient dependencies - this example uses Kotlin DSL but should be more or less the same in Groovy. Of-course you will be needing other dependencies like Hilt if you are using Dependency Injection.
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.7")
implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.7")
Next stop is to create the #Provide function which returns OkHttpClient.
#Provides
#Singleton
fun provideOkHttpClient():OkHttpClient { ...}
Background theory about Interceptors is very vital; to use an interceptor, you need to create a class that implements the Interceptor interface and override the intercept() method.
intercept() receives an Interceptor.Chain object - which represents the current request and allows you to proceed with the request by calling the proceed() method, or cancel the request by throwing an exception. intercept() override function returns a Response object which is exactly what chain.proceed(request) returns.
class MyInterceptor : Interceptor {
//throw an exception to cancel request
#Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
.newBuilder() // returns Request.Builder
.addHeader("Header_1", "value_1")
.build()
//proceed with the request
return chain.proceed(request)
}
}
Thanks to Kotlin Anonymous Function Syntax and Builder Pattern we can skip the above theory steps and start to build OkHttpClient which has the addInterceptor() function.
fun provideOkHttpClient(): OkHttpClient {
//build client
return OkHttpClient.Builder()
//create anonymous interceptor in the lambda and override intercept
// passing in Interceptor.Chain parameter
.addInterceptor { chain ->
//return response
chain.proceed(
//create request
chain.request()
.newBuilder()
//add headers to the request builder
.also {
it.addHeader("Header_1", "value_1")
it.addHeader("Header_2", "value_2")
}
.build()
)
}
.also { okHttpClient ->.... }
In the above code addInterceptor() opens up a lambda where we anonymously override intercept() passing in a chain parameter.
We use chain.proceed(request) to return a Response. It is when constructing the request to pass to chain.proceed() that we modify the actual request to add the headers.
You can also proceed to build up on the OkHttpClient to add timeouts etc.
.also { okHttpClient ->
okHttpClient.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
okHttpClient.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
if (BuildConfig.DEBUG) {
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
okHttpClient.addInterceptor(httpLoggingInterceptor)
}
}
.build()
This is the final code.
#Provides
#Singleton
fun provideOkHttpClient(): OkHttpClient {
//build client
return OkHttpClient.Builder()
//create anonymous interceptor in the lambda and override intercept
// passing in Interceptor.Chain parameter
.addInterceptor { chain ->
//return response
chain.proceed(
//create request
chain.request()
.newBuilder()
//add headers to the request builder
.also {
it.addHeader("Header_1", "value_1")
it.addHeader("Header_2", "value_2")
}.build()
)
}
//add timeouts, logging
.also { okHttpClient ->
okHttpClient.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
okHttpClient.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
//log if in debugging phase
if (BuildConfig.DEBUG) {
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
okHttpClient.addInterceptor(httpLoggingInterceptor)
}
}
.build()
}
This marks my StackOverflow's longest ever post, I'm sorry guys.

Related

Android Retrofit OkHttpClient Interceptor Add Header Gets Error "HTTP 403 Forbidden"

So, my goal is to embed the api key into my Retrofit object so that I don't need to manually append it as query parameter inside each request function, then I did the following (learn from: https://proandroiddev.com/headers-in-retrofit-a8d71ede2f3e):
private val interceptor = Interceptor { chain ->
val newRequest = chain.request().newBuilder().run {
addHeader("api_key", Constants.API_KEY)
build()
}
chain.proceed(newRequest)
}
private val okHttpClient = OkHttpClient.Builder().run {
connectTimeout(15, TimeUnit.SECONDS)
readTimeout(15, TimeUnit.SECONDS)
addInterceptor(interceptor) //<- apply Interceptor
build()
}
//apply the okHttpClient to my Retrofit object...
But it failed and gave this error: HTTP 403 Forbidden.
PS: Before adding this Interceptor everything works fine
Before:
#GET("neo/rest/v1/feed")
suspend fun getAsteroidsResult(
#Query("start_date") startDate: String,
#Query("end_date") endDate: String,
#Query("api_key") apiKey: String = Constants.API_KEY
): ResponseBody
Could you please add log interceptor and set the log level and provide a log?
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
And sth like this :
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC).setLevel
(HttpLoggingInterceptor.Level.BODY).setLevel(HttpLoggingInterceptor.Level.HEADERS))

what is retrofit interceptor , can we have multiple?

I can not understand the retrofit interceptors ,
private val OkHttpClient by lazy {
okhttp3.OkHttpClient.Builder()
.addInterceptor {
onOnIntercept(it)
}
.addInterceptor(LoggingInterceptor())
.addInterceptor(getInterceptor404())
.callTimeout(10, TimeUnit.MILLISECONDS)
// .addInterceptor(TimeoutInterceptor())
.build()
}
and what do these lines do, and If I have, multiple does the speed down?
val response: Response = chain.proceed(chain.request())
return chain.proceed(chain.request())
In Android sometimes you need to add a couple of parameters, like headers, to make a successful request, this is normal behavior from all the Android Apps when you are using Retrofit, you can do it in multiple ways
For example, you can add parameters directly to your request interface using the annotation Headers and putting a plain String, like this:
#Headers("Content-Type:application/json; charset=UTF-8")
#GET("yourwebsite/{someParam}/login")
fun logout(#Path("someParam") someParam: String?): Observable<LoginResponseViewModel>
Another solution is to send the Headers as a parameter to your interface function, using an annotation Header and sending a parameter, this gives you the possibility to have a custom parameter that you can manage from every request:
#Headers("Content-Type:application/json; charset=UTF-8")
#GET("yourwebsite/{someParam}/login")
fun logout(#Header(UUID.randomUUID().toString()) authToken: String?, #Path("someParam") someParam: String?): Observable<LoginResponseViewModel>**
Interceptor
A couple of people using Dagger probably will go for an Interceptor, you can have two types of interceptor:
The first one is using an interceptor directly in your Singleton, this will not give you versatility, but it will solve your problem faster, in this example, you can go for the chain object, get the request of the Retrofit call, get a new Builder and then add the Headers.
#Provides
#Singleton
fun getUnsafeOkHttpClient(): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val builder = OkHttpClient.Builder()
builder.addInterceptor(interceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.followRedirects(true)
.followSslRedirects(true)
.addInterceptor { chain ->
val newRequest = chain.request().newBuilder()
.addHeader("Authorization", UUID.randomUUID().toString())
.build()
chain.proceed(newRequest)
}
}
Yes, you can use multiple interceptors. When you do a request calling interface method using retrofit, your request go to the interceptor and then continue. In the interceptor you can rewrite or retry request. For example, you could add the access token in all request and refresh the token if is necessary, add the headers, another bodies, etc. When you received a response from api, the interceptor intercept the response too. But please, read the documentation to understand how it works. Have a nice coding!

Retrofit - Change BaseUrl - After Web Service Call [duplicate]

I have a scenario where I have to call an API with the same base URL, e.g. www.myAPI.com but with a different baseUrl.
I have an instance of Retrofit 2 which is built via a Builder:
return new Retrofit.Builder()
.baseUrl(FlavourConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build();
The FlavourConstants.BASE_URL looks like this:
public static final String BASE_URL = "http://myApi.development:5000/api/v1/";
For some WebRequests, I must call the same API but on others, I must call it from a completely different BaseUrl. How do I change the Retrofit instance to therefore point to a different URL during runtime?
The Retrofit instance doesn't have a .setBaseUrl or setter or anything similar as it's built via a Builder.
Any ideas?
Lucky for you Retrofit have a simple solution for that:
public interface UserManager {
#GET
public Call<ResponseBody> userName(#Url String url);
}
The url String should specify the full Url you wish to use.
Retrofit 2.4, MAY 2019
Two simple solution for this hassle are:
Hardcode the new URL, while leaving the base URL as it is:
#GET("http://example.com/api/")
Call<JSONObject> callMethodName();
Pass the new URL as an argument, while leaving the base URL as it is:
#GET
Call<JSONObject> callMethodName(#Url String url);
N.B: These methods work for GET or POST. However, this solution is only efficient if you just need to use an exception of one or two different URLs than your base URL. Otherwise, things can get a little messy in terms of code neatness.
If your project demands fully dynamically generated base URLs then you can start reading this.
Also there is a such hack in Kotlin while defining base url
e.g.
#FormUrlEncoded
#Headers("Accept: application/json")
#POST
suspend fun login(
baseUrl: String,
#Field("login") login: String,
#Field("password") password: String
#Url url: String = "$baseUrl/auth"
): ResponseAuth
It's not working. Throws:
java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1)
The only way is suggested by Jake Wharton https://github.com/square/retrofit/issues/2161#issuecomment-274204152
Retrofit.Builder()
.baseUrl("https://localhost/")
.create(ServerApi::class.java)
class DomainInterceptor : Interceptor {
#Throws(Exception::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
return chain.proceed(
request.newBuilder()
.url(
request.url.toString()
.replace("localhost", "yourdomain.com:443")
.toHttpUrlOrNull() ?: request.url
)
// OR
//.url(HttpUrl.parse(request.url().toString().replace("localhost", "yourdomain.com:443")) ?: request.url())
.build()
)
}
}
The easiest (but not the most performant) way to change the Retrofit2 base URL at runtime is to rebuild the retrofit instance with the new url:
private Retrofit retrofitInstance = Retrofit.Builder().baseUrl(FlavourConstants.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient).build();
public void setNewBaseUrl(String url) {
retrofitInstance = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient).build();
}
...
retrofitInstance.create(ApiService.class);
Alternatively, if you are using OkHttp with Retrofit, you can add an OkHttp interceptor like this one when building your OkHttp client:
HostSelectionInterceptor hostInterceptor = new HostSelectionInterceptor();
hostInterceptor.setHost(newBaseUrl);
return new OkHttpClient.Builder()
.addInterceptor(hostInterceptor)
.build();
I just used the below function when i faced this problem. but i was on hurry and i believe that i have to use another and i was using "retrofit2:retrofit:2.0.2"
public static Retrofit getClient(String baseURL) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
} else {
if (!retrofit.baseUrl().equals(baseURL)) {
retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}
return retrofit;
}
[Update]
I have found this link that explain the #Url that can be sent as a parameter and i believe it is more professional than my old solution.
Please find below the scenario:
interface APIService{
#POST
Call<AuthenticationResponse> login(#Url String loginUrl,[other parameters])
}
And below is the method in the class that provide the retrofit object
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl("http://baseurl.com") // example url
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
Then you can call the method as below:
APIInterface apiInterface = ApiClient.getClient2().create(ApiInterface.class);
apiInterface.login("http://tempURL.com").enqueue(......);
You should use interceptor like this:
class HostSelectionInterceptor: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
apiHost?.let { host ->
val request = chain.request()
val newUrl = request.url.newBuilder().host(host).build()
val newRequest = request.newBuilder().url(newUrl).build()
return chain.proceed(newRequest)
}
throw IOException("Unknown Server")
}
}
You just need to change at runtime the apiHost variable (var apiHost = "example.com"). Then add this interceptor to OkHttpClient builder:
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(HostSelectionInterceptor())
.build()
Ok , if I dont remember wrong the docs of Retrofit says you can point to another URL if you just simply add in your interface servicse the full url of the ws, that is different fomr the BASE_URL in Retrofit Builder. One example...
public interface UserManager {
#GET("put here ur entire url of the service")
public Call<ResponseBody> getSomeStuff();
}
A solution is to have two distinct instance of retrofit, one for your FLAVOURED base URL and another for the other base URL.
So just define two functions :
public Retrofit getFlavouredInstance() {
return new Retrofit.Builder().baseUrl(FlavourConstants.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient).build();
}
public Retrofit getOtherBaseUrl() {
return Retrofit.Builder().baseUrl(OTHER_BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient).build();
}
and after you just have to use the right one.
Please try the following code:
private void modify(String url) throws Exception {
Class mClass = retrofit.getClass();
Field privateField = mClass.getDeclaredField("baseUrl");
if (privateField != null) {
privateField.setAccessible(true);
System.out.println("Before Modify:MSG = " + retrofit.baseUrl().url().getHost());
privateField.set(retrofit, HttpUrl.parse(url));
System.out.println("After Modify:MSG = " + retrofit.baseUrl().url().getHost());
}
}
You can regenerate the DaggerAppComponent after changing your apiUrl it will generate a new instance of providerRetrofit with the new url
DaggerAppComponent.builder() .application(this) .build() Log.init( LogConfiguration .Builder() .tag("...") .logLevel(LogLevel.NONE) .build() )

Java - Passing variable to an interface

I am building an android app and i am using Retrofit to retrieve data from API. In this app i have to make 3 calls. The first one is working fine. The code for the first one is below. I have one class
public class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("https://api_app.com")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
Also i have this interface
#Headers({
"AppId: 3a97b932a9d449c981b595",
"Content-Type: application/json",
"appVersion: 5.10.0",
"apiVersion: 3.0.0"
})
#POST("/users/login")
Call<MainUserLogin> logInUser(#Body LoginBody loginBody);
The code of the Actvity is this
call.enqueue(object : Callback<MainUserLogin> {
override fun onResponse(call: Call<MainUserLogin>, response: Response<MainUserLogin>) {
if (response.code().toString().equals("200")){
val resource = response.body()
bearerToken = resource.session.bearerToken
if (bearerToken.isNotEmpty() && bearerToken.isNotBlank()){
val sharedPreferences = getSharedPreferences("Settings", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("bearerToken", bearerToken)
editor.commit()
BearerToken.bearerToken = bearerToken
val i = Intent(this#LoginActivity, UserAccountsActivity::class.java)
i.putExtra("bearerToken", bearerToken)
startActivity(i)
}else{
Toast.makeText(applicationContext, "Please try again.", Toast.LENGTH_LONG).show()
}
}else{
println("edwedw "+response.errorBody().string())
Toast.makeText(applicationContext, "Incorrect email address or password. Please check and try again.", Toast.LENGTH_LONG).show()
}
}
override fun onFailure(call: Call<MainUserLogin>, t: Throwable) {
call.cancel()
}
})
This call is working fine.
With this call i am getting one token. The problem is that i have to pass this token as header to make the second call. So, the second call will be like this.
#Headers({
"AppId: 3a97b932a9d449c981b595",
"Content-Type: application/json",
"appVersion: 5.10.0",
"apiVersion: 3.0.0",
"Authorization: "+***Token***
})
#GET("/products")
Call<MainUserLogin> getUseraccounts ();
Is there any way to pass the variable from the Activity to the interface to make the Api request?
Thank you very much.
Using Retrofit you can call API's with multiple headers as follows
#GET("/products")
Call<MainUserLogin> getUseraccounts(#Header("AppId") String appId, #Header("Content-Type") String contentType, #Header("appVersion") String appVersion, #Header("apiVersion") String apiVersion, #Header("Authorization") String token);
Instead of
#Headers({
"AppId: 3a97b932a9d449c981b595",
"Content-Type: application/json",
"appVersion: 5.10.0",
"apiVersion: 3.0.0",
"Authorization: "+***Token***
})
#GET("/products")
Call<MainUserLogin> getUseraccounts ();
this. When you call getUseraccounts method you can parse the token that you created from the previous endpoint.
Try this and let me know your feedback. Thanks!
Once you receive the token, you should save this token in a global repository since the auth token is something that your app will need in order to make further authenticated api calls.
After that, define a AuthorizationHeaderInterceptor which will extend okhttp3.Interceptor. Override the intercept method of this interceptor to add auth token to your request.
#Override
public Response intercept(#NonNull Chain chain) {
return completeRequest(chain);
}
private Response completeRequest(#NonNull Interceptor.Chain chain) {
AuthToken authToken = authTokenRepository.get();
Request.Builder requestBuilder = chain.request().newBuilder();
if (authToken != null && chain.request().header(Authorization.NAME) == null) {
requestBuilder.addHeader(Authorization.NAME, Authorization.getValue(authToken.getIdToken()));
}
Request request = requestBuilder.build();
try {
return chain.proceed(request);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
The interceptor can be added when you build your okhttpClient.
okHttpClientBuilder.addInterceptor(new AuthorizationHeaderInterceptor(authTokenRepository))
Note that the Authorization class is simple convenience class which encapsulates the authorization header name and value format.
public class Authorization {
public static final String NAME = "Authorization";
#NonNull
public static String getValue(#NonNull String accessToken) {
return String.format("Bearer %s", accessToken);
}
}

Retrofit #Url doesnt override BaseUrl

Hi i am trying to overide the baseUrl from one specific api call and it doesnt seem to work when using #Url as a paraemter to the api method.
Below is my Api class method
#POST
fun getUserDetails(#Body body: request, #Url authUrl : String): Single<Response<ResponseData>>
code that cals and make the request
private fun getApi(): Api {
val gson = GsonBuilder().setLenient().create()
val httpClient = myNetworkHelper.createHttpClient()
val retrofit = Retrofit.Builder()
.baseUrl("http://defaultBaseUrl")
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
return retrofit.create(Api::class.java)
}
override fun getUserDetails(someRequestData, "http://dynamic/getuserDetails): Single<Response<ResponseData>> {
return getApi().getUserDetails(body, url)
}
The above results in making a request to this url
http://defaultBaseUrl/http://dynamic/getuserDetails
Instead of:
http://dynamic/getuserDetails
I did a test in local and actually I've been able to override the base URL set in the Retrofit instance.
Here's the API:
public interface Api {
#POST
Call<Void> fakeService(#Url String url);
}
And here's the "client":
public class Main {
public static void main(String[] args) throws IOException {
// Use the HttpLogginInterceptor to check what's the real call
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.foo.com/")
.client(client)
.build();
Api api = retrofit.create(Api.class);
Call<Void> call = api.fakeService("http://www.example.com");
call.execute();
}
}
The result is:
Jul 18, 2018 12:28:54 PM okhttp3.internal.platform.Platform log
INFO: --> POST http://www.example.com/ (0-byte body)
Jul 18, 2018 12:28:55 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK http://www.example.com/ (366ms, unknown-length body)
This is the document from Retrofit #GET annotation:
#Documented
#Target(METHOD)
#Retention(RUNTIME)
public #interface GET {
/**
* A relative or absolute path, or full URL of the endpoint. This value is optional if the first
* parameter of the method is annotated with {#link Url #Url}.
* <p>
* See {#linkplain retrofit2.Retrofit.Builder#baseUrl(HttpUrl) base URL} for details of how
* this is resolved against a base URL to create the full endpoint URL.
*/
String value() default "";
}
So #Url should be placed as the first parameter, so I think this will work:
#POST
fun getUserDetails(#Url authUrl: String, #Body body: request): Single<Response<ResponseData>>

Categories

Resources