Commit 89301510 authored by lumisce's avatar lumisce

Refactor retrofit service

parent afd1dddb
......@@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".CustomApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
......
......@@ -4,7 +4,21 @@ import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.IOException;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class AppUtils {
public static CanteeneoApiInterface service;
private static Context c;
public static boolean isNetworkAvailable(Context c) {
ConnectivityManager connectivity =(ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
......@@ -23,4 +37,47 @@ public class AppUtils {
}
return false;
}
public static void initREST(String serverIP, Context c) {
AppUtils.c = c;
File httpCacheDirectory = new File(c.getCacheDir(), "responses");
int cacheSize = 10 * 1024 * 1024; // 10MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
OkHttpClient client = new OkHttpClient().newBuilder()
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://" + serverIP + ":5000/")
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
service = retrofit.create(CanteeneoApiInterface.class);
}
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response origResponse = chain.proceed(chain.request());
if (AppUtils.isNetworkAvailable(c)) {
int maxAge = 60; // read from cache for 1 minute
return origResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
return origResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
}
};
}
package com.testapp;
import android.app.Application;
public class CustomApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
AppUtils.initREST(getResources().getString(R.string.server_ip), getApplicationContext());
}
}
......@@ -56,28 +56,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File httpCacheDirectory = new File(this.getCacheDir(), "responses");
int cacheSize = 10 * 1024 * 1024; // 10MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
OkHttpClient client = new OkHttpClient().newBuilder()
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://" + getResources().getString(R.string.server_ip) + ":5000/")
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
service = retrofit.create(CanteeneoApiInterface.class);
c = getApplicationContext();
setContentView(R.layout.activity_nav_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
......@@ -253,23 +231,4 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
}
});
}
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response origResponse = chain.proceed(chain.request());
if (AppUtils.isNetworkAvailable(c)) {
int maxAge = 60; // read from cache for 1 minute
return origResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
return origResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
}
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment