android application's retrofit base_url - android

I want to build an api for an android studio application to make the base url of the retrofit. I am having problems to make the base_url. I want to know how I should make the base_url of the retrofit. I am having problems validating the base_url. I want to know the procedure to make a base_url for retrofit.

If this is my url
http://api.themoviedb.org/3/movie/top_rated?api_key=12345678910111213
Then base url becomes
http://api.themoviedb.org/
It will be passed in as
retrofit = new Retrofit.Builder()
.baseUrl("http://api.themoviedb.org/");
and the remaining part will become get or post query

Related

Change Retrofit baseUrl with Dagger in runtime

Is there is some solutions to change Retrofit BaseUrl in runtime?
I'm using Dagger to make Retrofit instance, but in my case Url is the users input after Application created and launcher activity started.

Retrofit2: Protect baseUrl

I am using Retrofit2 for API calls via my Android app:
Api api = new Retrofit.Builder()
.baseUrl(BASE_URL) //<-- Problem here
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient.build())
.build()
.create(Api.class);
Nowadays, many apps are being re-uploaded to the store by just changing the BASE_URL needed or with some minimal re-skinning.
I know that protecting to 100% an app from Reverse Engineering is impossible, but I just want to somehow make it harder for anyone to just change the BASE_URL and use the app with his own API.
For the BASE_URL itself, I am getting it with some native code as explained here. But still, anyone can put whatever he/she wants in baseUrl(BASE_URL) and he/she is good to go.
For now, I am thinking to import the whole Retrofit2 as a module in my project and modify there to add some level of obscurity.
But I am just wandering, isn't there any better way to do it?
Thanks.

RX Java with Retrofit-2 VS Normal Retrofit-2

I am currently using Retrofit2 for API parsing. As I was asked to change it with RxJava + Retrofit for my new application. How can I achieve this. What is the benefits of using RxJava along with Retrofit.
Any help should be a greatly appreciated.
Below is the code I am using for normal Retrofit parsing
Retrofit.Builder builder =new Retrofit.Builder().baseUrl(API_BASE_URL).client(httpClient).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
As I was asked to change it with RxJava + Retrofit for my new application. How can I achieve this.
Add com.squareup.retrofit2:adapter-rxjava2 as a dependency in your project and configure it on your Retrofit.Builder:
addCallAdapterFactory(
RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
Then you can change your Retrofit interfaces from
Call<ReturnType> op(...)
to
Observable<ReturnType> op(...)
and instead of enqueue()ing the Call, subscribe the observable to get your requests flying.
What is the benefits of using RxJava along with Retrofit
Retrofit service API calls integrate nicely with other rxjava code in your application. If you're not using rxjava elsewhere in your application, there's little benefit.

Retrofit dynamic HTTP methods

I'm planning to replace Apache HTTP client with retrofit in my project.
The problem I'm facing is that retrofit didn't(I couldn't find) support setting HTTP method to request at runtime.
In my Web Service I don't know what HTTP method to call in advance, so annotations #GET, #POST, ... are useless.
Retrofit is not designed for dynamic url requests. You configure retrofit with your API base URL then make specific requests.
For a more flexible dynamic option use out OkHttp. It is the HTTP Client used by Retrofit and it easy to work with.
You can use Retrofit 2 for dynamic URL request with the new #Url annotation:
public interface CarService {
#GET
public Call<ImageResponse> getPicture(#Url String url);
}
Then just also create #POST, #PUT etc. You are going to have to make the choice somewhere.

HTTP vs HTTPS requests with Retrofit

I'm working on REST API client for Android using Retrofit.
Some of the use something like this http://my.backend.com and others use https://my.backend.com. The way I found is to create two separate interfaces and build two RestAdapters with different endpoints.
But I would like to keep my interfaces consitent and I'm wondering if it is possible for example build my Res adapter with my.backend.com and specify if the methot thould use https with #HTTPS annotation ?
Thanks.
The only thing you can change on a RestAdapter after it's been built is the log level so I'm afraid the only solution is to have two RestAdapters. Two seperate interfaces should not be necessary though, as long as the path after your endpoint (my.backend.com) is the same for both the http and the https version.
You can do the following generic method which returns retrofit and keep just one interface. "baseUrl" can be either "http" or "https" urls.
public static Retrofit getRetrofit(#NotNull String baseUrl) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}

Categories

Resources