Build a url with retrofit - android

I'm using retrofit to access data about tv channels.
The url that i have is this:
http://ott.online.meo.pt/catalog/v7/Channels?UserAgent=AND&$filter=substringof(%27MEO_Mobile%27,AvailableOnChannels)%20and%20IsAdult%20eq%20false&$orderby=ChannelPosition%20asc&$inlinecount=allpages
In Retrofit.Builder() i put "the main url" (http://ott.online.meo.pt) and in interface Endpoint the rest of url.
I do this, but i don't kwon how put the complet url
interface Endpoint {
#Headers("User-Agent: AND")
#GET("catalog/v7/Channels" )
fun getChannels() : Call<SerializeChannels>
}

Your retrofit client :
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://ott.online.meo.pt/")
.build();
You can define endpoint in multiple way:
Following is hard codded way:
interface Endpoint {
#GET("catalog/v7/Channels?UserAgent=AND&filter=substringof('MEO_Mobile',AvailableOnChannels)&IsAdult=false&orderby=ChannelPosition asc&inlinecount=allpages" )
fun getChannels() : Call<SerializeChannels>
}
You can also use Query parameter as follows:
interface Endpoint {
#GET("catalog/v7/Channels")
fun getChannels( #Query("UserAgent") String agent, #Query("filter") String filters,#Query("IsAdult") String isAdult,#Query("orderby") String sort,#Query("inlinecount") String count) : Call<SerializeChannels>
}

Related

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() )

How to add URL parameter in a Retrofit #GET request in Kotlin

I am currently trying to fetch a JSONArray from a server using Retrofit in Kotlin. Here is the interface I am using:
interface TripsService {
#GET("/coordsOfTrip{id}")
fun getTripCoord(
#Header("Authorization") token: String,
#Query("id") id: Int
): Deferred<JSONArray>
companion object{
operator fun invoke(
connectivityInterceptor: ConnectivityInterceptor
):TripsService{
val okHttpClient = OkHttpClient.Builder().addInterceptor(connectivityInterceptor).build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://someurl.com/")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(TripsService::class.java)
}
}
}
the desired url is: https://someurl.com/coordsOfTrip?id=201
I am getting the following error message:
retrofit2.HttpException: HTTP 405 Method Not Allowed
I know the URL is working because I can access it via a browser.
Can someone please help me identify what I am doing wrong?
Just change the parameter from
#GET("/coordsOfTrip{id}")
to
#GET("/coordsOfTrip") // remove {id} part that's it
And you'd get the desired URL https://someurl.com/coordsOfTrip?id=201
If you want to use {id} in GET() then you've to use it like below
#GET("/coordsOfTrip{id}")
fun getTripCoord(
#Header("Authorization") token: String,
#Path("id") id: Int // use #Path() instead of #Query()
): Deferred<JSONArray>
But in your case it doesn't require. Follow the first method I mentioned.
For more check Retorfit's official documentation URL Manipulation part
Replace
#GET("/coordsOfTrip{id}")
with:
#GET("/coordsOfTrip?id={id}")

How i change the retrofit URL at runtime

I need change the URL base in retrofit, i'm using koin to create a retrofit module on app startup and i want change this url in runtime.
I already tried change the baseUrl("http://192.168.192.168/") to baseUrl("http://")and change the url on retrofit call but my app crashs and return illegal URL error.
This is my fun to create the builder
fun createRetrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl("http://192.168.192.168/")//i need change this at runtime
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
create a bean to my module
val retrofitModule: Module = applicationContext {
bean { createRetrofit(get()) }
}
and start the koin:
startKoin(application = this,
modules = listOf(retrofitModule, ...)
)
someone can i help me with this?
you must have to add these lines in your code:
First Step:
Add the new CallAdapter RxJavaCallAdapterFactory.create() when building a Retrofit instance.
public static final String BASE_URL = "http://google.com/";
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
Next step:
Update the APIService for example:-> savePost(String title, String body, String userId) method to become an Observable.
public interface APIService {
#GET
Call<ResponseBody> list(#Url String url);
//or
#POST("/posts")
#FormUrlEncoded
Observable<Post> savePost(#Field("title") String title,
#Field("body") String body,
#Field("userId") long userId);
}
Final step:
When making the requests, our anonymous subscriber responds to the observable's stream which emits event.
public void sendPost(String title, String body) {
// RxJava
mAPIService.savePost(title, body, 1).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Post>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(Post post) {
showResponse(post.toString());
}
});
}
this is way you build your dynamic urls: want to learn more details full description link: Sending Data With Retrofit 2 HTTP Client for Android
and See base URL for details of how the value will be resolved against a base URL to create the full endpoint URL.
if you are doing using kotlin: follow this link. dynamic urls at Runtime with Retrofit 2
I already tried change the baseUrl("http://192.168.192.168/") to baseUrl("http://")and change the url on retrofit call but my app crashs and return illegal URL error.
You can leave it as a baseUrl if you use #URL it will overwrite the one on yout Retrofit.Builder()
You can use #URL parameter to change the endpoint dynamically.
#GET
fun getUsers(#Url String url) : Observable<UserResponse>

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>>

How to send a retrofit 2 post call with no parameters

I have to call an api using retrofit 2 in android. but with no values. When I does that, it shows that there must be at least 1 #field. Below is the code I am using.
public interface gitAPI {
#FormUrlEncoded
#POST("/MembersWS.svc/GetMemberAvailability/MBR0011581")
Call<Questions[]> loadQuestions();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.1.99:82")
.addConverterFactory(GsonConverterFactory.create())
.build();
// prepare call in Retrofit 2.0
gitAPI stackOverflowAPI = retrofit.create(gitAPI.class);
Call<Questions[]> call = stackOverflowAPI.loadQuestions();
call.execute();
Declare body value in your interface with next:
#Body RequestBody body and wrap String JSON object:
RequestBody body = RequestBody.create(MediaType.parse("application/json"), (new JsonObject()).toString());

Categories

Resources