Retrofit response Cache - android

There are a lot of complex answers here about Retrofit cache but none seem to help me.
How do I cache my code response coming from the Retrofit response?
My App works fine downloading the JSON below but I need to make a cache for it and I never did cache on Android.
I need to open the App offline and get the result from my Retrofit call as if I was online.
public class APIClient {
public static final String BASE_URL = "https://raw.githubusercontent.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;
}}
The json response is VERY simple:
{
"fruits":[
{
"name":"Apple",
"image":"https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg",
"price":35
},
{
"name":"Banana",
"image":"https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Bananas_white_background_DS.jpg/320px-Bananas_white_background_DS.jpg",
"price":12
},
{
"name":"Grapes",
"image":"https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Table_grapes_on_white.jpg/320px-Table_grapes_on_white.jpg",
"price":45
},
{
"name":"Pineapple",
"image":"https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Pineapple_and_cross_section.jpg/286px-Pineapple_and_cross_section.jpg",
"price":200
},
{
"name":"cherry",
"image":"http://www.desicomments.com/wp-content/uploads/2017/05/Cherry-Image-600x570.jpg",
"price":13
},
{
"name":"clementine",
"image":"http://www.icecreamnation.org/wp-content/uploads/2013/04/Clementine_orange.jpg",
"price":12.4
},
{
"name":"olive",
"image":"https://www.homenaturalcures.com/wp-content/uploads/olive.jpg",
"price":9.5
},
{
"name":"tomato",
"image":"http://cdn2.stylecraze.com/wp-content/uploads/2013/05/tomato-hair-benefits1.jpg",
"price":8.75
},
{
"name":"huckleberry",
"image":"http://farm3.static.flickr.com/2131/2082287810_47339fc93e.jpg",
"price":11.75
},
{
"name":"papaya",
"image":"http://media.mercola.com/assets/images/foodfacts/papaya-nutrition-facts.jpg",
"price":2.75
},
{
"name":"lime",
"image":"https://www.florihana.com/images/stories/virtuemart/product/FLE019%20-%20LIME.jpg",
"price":5.75
},
{
"name":"pear",
"image":"https://www.organicfacts.net/wp-content/uploads/pear.jpg",
"price":4.75
}
]
}

Assuming server respects Cache-Control/If-Modified-Since headers, than all you have to do is to create a Cache object and set it to OkHttpClient. Retrofit will take care of the rest.
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(getCacheDir(), cacheSize);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cache(cache)
.build();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:3000/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Source: futurestud.io

Inorder cache our server response using retrofit 2.0 and okHttp, we all need is OkHttpClient, AddNetworkInterceptor add cache interceptor, you can not use. AddInterceptor, also need not both at the same time.
This method requires no server-side operation, no other server-side caching strategy, if the server has its own cache strategy code should be modified accordingly to adapt to the server.
You can refer this link , it helped me to solve my problem

Related

Retrofit with OkHTTP not set Content-Type with #FormUrlEncoded

I'm trying to implement auth via x-www-form-urlencoded with Retrofit 2 on Android but faced a problem, that Header Content-Type not set with #FormUrlEncoded annotation, as well as I'm trying to set it manually, but when I'm setting it with a typo like Cotent-Type it works correctly and I can see it in headers.
Retrofit version: 2.4.0
So my question: why #FormUrlEncoded not set a content type as well as #Header annotation or what can remove it from headers.
My request:
#FormUrlEncoded
#POST("account/login")
Single<LoginResponse> login(#Field("memberId") String memberId,
#Field("pin") String pin);
OkHTTP/Retrofit provider with interceptors:
#Singleton
#Provides
Retrofit provideRetrofit(final OkHttpClient client, final Moshi moshi) {
return new Retrofit.Builder()
.baseUrl(Configuration.BASE_URL)
.client(client)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
#Provides
OkHttpClient provideOkHttpClient(#AppContext final Context context) {
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.followRedirects(true)
.followSslRedirects(true)
.addInterceptor(createLanguageInterceptor(context));
if (BuildConfig.DEBUG) {
builder.addInterceptor(new LoggingInterceptor());
}
return builder.build();
}
Interceptor createLanguageInterceptor(#AppContext final Context context) {
Locale current = context.getResources().getConfiguration().locale;
return chain -> {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("Accept-Language", current.getLanguage());
Request request = builder.build();
Response response = chain.proceed(request);
return response;
};
}
As a workaround, I've implemented the following interceptor:
Interceptor createHeaderTransformationInterceptor() {
return chain -> {
final Request request = chain.request();
String dataType = request.header("Data-Type");
final Request resultRequest = dataType == null
? request
: chain.request().newBuilder()
.removeHeader("Data-Type")
.addHeader("Content-Type", dataType)
.build();
return chain.proceed(resultRequest);
};
}
and it works fine with the following annotation:
#Headers({"Data-Type: application/x-www-form-urlencoded"})
UPD: the reason that my interceptor didn't see that is in a place where the content type is stored. The right way to see that header in an interceptor:
if (requestBody.contentType() != null) {
logger.log("Content-Type: " + requestBody.contentType());
}
if (requestBody.contentLength() != -1) {
logger.log("Content-Length: " + requestBody.contentLength());
}
By this Request
#FormUrlEncoded
#POST("account/login")
Single<LoginResponse> login(#Field("memberId") String memberId,
#Field("pin") String pin);
method #POST and #FormUrlEncoded automatic add
Content-Type: application/x-www-form-urlencoded in header you can check in log by
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.build();
it print all log in verbose mode

Retrofit returns HTTP 401 for call running in background

I have a strange problem with one of my retrofit call,it works fine when the app is in the background(recent list)
I have a call through which i update my widget data,the problem is when the app is cleared of from the recent list,the call gives HTTP 401 unauthorized response.
however i pass the same bearer token with it.
please have a look at the code and suggest some help
public static OkHttpClient getOkhttpClient() {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + TokenGenerator.getToken())
.build();
return chain.proceed(newRequest);
}
}).build();
return client;
}
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkhttpClient())
.addConverterFactory(JacksonConverterFactory.create())
.build();
}
return retrofit;
}

Android Retrofit update HTTP client configuration

We have a case where we may have to update the public keys when using Certificate Pinning with OKHttp client and Retrofit. My question is how I would update the certificate pinner of the http client after retrofit has been initialized (like, when a new public key has been received)?
Do I update the CertificatePinner in the http client and then create a new instance of retrofit? Or is there an easier way?
Any suggestions appreciated.
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
clientBuilder.certificatePinner(NetworkUtils.getCertificatePinner()) ;
OKHTTPClient client = clientBuilder.build();
Retrofit myRetrofit = new Retrofit.Builder()
.baseUrl(url)
.client(client)
.build();
// Now I need to update the certificate pinner, like this?
client.certificatePinner(NetworkUtils.getCertificatePinner());
myRetrofit = new Retrofit.Builder()
.baseUrl(url)
.client(client)
.build();
Did you try using an interceptor. Something on the lines of inner class ExpiredSessionInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request)
if (response.code() == 202) {
val newRequest = request.newBuilder().build()
return chain.proceed(newRequest)
} else {
return response;
}
}
}

Is possible to use multiple retrofit API

I am using retrofit 2. I need to use 2 different Rest API, because they have different base URL, headers and cookies.
public class RestClient {
private static IRestApi REST_CLIENT;
static {
setupRestClient();
}
public static IRestApi get() {
return REST_CLIENT;
}
private static void setupRestClient() {
Interceptor interceptor = new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
//add some cookies & headers
}
};
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(GenericConstants.READ_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(GenericConstants.CONNECTION_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(GenericConstants.BASE_ENDPOINT_INSTAGRAM)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
REST_CLIENT = retrofit.create(IRestApi.class);
}
}
I am using it like so:
Call<Obj> call = RestClient.get().myMethod(params);
I want to create 2 clases like this, because I have 2 types of requests, 1 with headers and cookies and one plain, is this possible?
you can create method of retrofit url like
public Retrofit getAdapter(String url){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit;
}
Use this adapter wherever you want.

Retrofit 2.0 beta 1 with whole URL

In retrofit 2.0 i want to use only one url .The url is same as base url as that of #GET in interface.I am facing the problem for getting the response.If Any one have better solution for using the whole url in #GET then please suggest the solution.
here is the code
public class RestClient {
private static ApiInterface apiInterface ;
private static String baseUrl = "here is my whole base url";
public static ApiInterface getClient() {
if (apiInterface == null) {
OkHttpClient okClient = new OkHttpClient();
okClient.interceptors().add(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
return response;
}
});
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverter(String.class, new ToStringConverter())
.client(okClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiInterface = client.create(ApiInterface.class);
Log.e("RETROFIT RESPONCE IS...", client.toString());
}
return ApiInterface ;
}
public interface ApiInterface {
#Headers("User-Agent: Retrofit2.0Tutorial-App")
#GET("here is my whole base url”)
Call<EventResult> getEvent();
}
}
With retrofit 2 is possible to use the #Url annotation. Let's assume your Retrofit configuration is
Retrofit builder = new Retrofit.Builder()
.baseUrl("http://wwww.example.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
Test r = builder.create(Test.class);
you declare your interface:
public interface Test {
#GET
Call<Example> getTest(#Url String url);
}
and for getTest you don't want to use the baseUrl you declared in the configuration. The #Url will ignore the baseUrl you declared and will use the one you provide as argument
I don't think it's possible since BaseURL is mandatory in Retrofit Builder and even you supply the builder with the full URL the builder will parse it and save only the BaseURL. I guess the reason why they do this is to keep it simple and consistent.
for reference you can see the source code here

Categories

Resources