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

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

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

Firebase Performance: Retrofit network requests are not being logged

Using
com.squareup.retrofit2:retrofit:2.9.0
com.squareup.okhttp3:okhttp:4.4.0
com.google.firebase:firebase-perf-ktx (bom version is 29.0.4)
I'm unable to see any network requests inside the Firebase Performance network requests section. Custom traces work fine but the network requests are empty. I also cannot see any I/FirebasePerformance logcat logs suggesting that http requests are being logged.
The app uses a simple retrofit service to make requests which looks like:
Retrofit.Builder()
.baseUrl("https://myurl.com")
.client(get())
.addConverterFactory(MoshiConverterFactory.create(get()))
.build()
.create(MyService::class.java)
The OkHttpClient :
OkHttpClient.Builder()
.addInterceptor(UserAgentHeaderInterceptor())
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
I have already tried the following:
Been through Firebase's trouble shooting guide for this issue.
Downgraded my okHttp version to 3.12.

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.

Best Scope in Dagger2 for Okhttp3 with dynamic Interceptors

How would the scope work with Auth Tokens? I cannot create my Retrofit instance until I can add an interceptor that signs it with my auth token. Therefore, I would like to create Retrofit when the auth tokens are available (after sign-in). How do I get scope working correctly in this situation?
Thanks a lot!
There is no best way of doing this, and it might also depend on how often you change / recreate your Retrofit instances.
What's better, or which better fits your use case depends very strongly on what you are trying to accomplish and how. There's many ways how what you are trying to achieve is possible, but in general you have 2 options
Create a new client for every retrofit instance (e.g. if you just log the user in once), so you would just add the client within the same scope
Create a #Singleton instance of okhttp3 and modify the client when required by using the newBuilder()
I think the first point is self explanatory, just create your client when you create retrofit, use the same scope and be done.
The second approach uses Okhttp3 feature of the newBuilder() method, by adding your interceptor to the okhttp client when creating your retrofit instance.
It would look something like this:
// Some singleton client to maybe also use in other parts of your app
#Singleton
OkHttpClient provideClient() { return new OkHttpClient(); }
// creating your retrofit client
#UserScope
Retrofit provideRetrofit(OkHtpClient client, Interceptor userInterceptor) {
return new Retrofit.Builder()
.client(client.newBuilder() // new builder to modify okhttp3
.addNetworkInterceptor(interceptor)
.build())
/* other settings */
.build();
}
If you get creative you can also just expose a setCredentials() method on your interceptor, then you can just create them once and reuse all the objects by adding them to the #Singleton scope. You'd then change your user by accessing and modifying your interceptor, albeit this is not a clean approach in my humble opinion.

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