Is retrofit using OkHttpClient only? - android

Is retrofit using OkHttpClient only?
If not set Client.Provider into RestAdapter.Builder.

By default, retrofit(from retrofit 2.0 onwards) is using OkHttpClient. If you want other clients, you can mention while creating the builder. Please note that from retrofit 2.0 onwards 'Retrofit' is used instead of 'RestAdapter'.

Related

Can one set the timeout in Retrofit2 on android without creating an okHttpClient?

I'm currently building a Retrofit object as follows:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
I don't need to pass it a client, i.e. no need to deal with the internals.
Now I would like to set a timeout, but all the examples I found involve creating an okHttpClient and assigning it to the Retrofit object.
Does it mean that okHttp is the de-facto client used by the system in all the situations?
Does it mean that okHttp is the de-facto client used by the system in all the situations?
Yes. Retrofit uses OkHttp for its network I/O. For example, in the Retrofit documentation, they mention in the section on R8/Proguard:
You might also need rules for OkHttp and Okio which are dependencies of this library

Implementing cache mechanism with retrofit 2 as like robospice + retrofit 1

I have been using retrofit1 + robospice to make API requests. You can check sample here. In this framework, I used to make request like this
getSpiceManager().execute(request, CACHE_KEY, DurationInMillis.ONE_MINUTE(CACHE_TIME), new ResponseListener());
Above statement will make sure that it will return the cached response for one minute from the time I requested(with the same cache key).But currently, robospice does not support retrofit v2. My Question is that is there any cache mechanism for retrofit v2 to implement like this. I googled for some time but I could not find what exactly I want.
the proper way to cache responses in android is using OkHttp (and it combines with Retrofit as well)
use the cache method :
OkHttpClient client = new OkHttpClient.Builder()
.cache(new Cache(cacheDir, MAX_SIZE))
.build()
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.build()

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