Java tutorial
/* * Copyright (C) 2015 Naman Dwivedi * * Licensed under the GNU General Public License v3 * * This is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. */ package com.guoxiaoxing.music.lastfmapi; import android.content.Context; import com.squareup.okhttp.Cache; import com.squareup.okhttp.OkHttpClient; import java.util.concurrent.TimeUnit; import retrofit.RequestInterceptor; import retrofit.RestAdapter; import retrofit.client.OkClient; public class RestServiceFactory { private static final String TAG_OK_HTTP = "OkHttp"; private static final long CACHE_SIZE = 1024 * 1024; public static <T> T createStatic(final Context context, String baseUrl, Class<T> clazz) { final OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE)); okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS); RequestInterceptor interceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade request) { //7-days cache request.addHeader("Cache-Control", String.format("max-age=%d,max-stale=%d", Integer.valueOf(60 * 60 * 24 * 7), Integer.valueOf(31536000))); request.addHeader("Connection", "keep-alive"); } }; RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl) .setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient)); return builder.build().create(clazz); } public static <T> T create(final Context context, String baseUrl, Class<T> clazz) { RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl); return builder.build().create(clazz); } }