package app.jiyun.com.android_yuekao_test.unit;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import app.jiyun.com.android_yuekao_test.MainActivity;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by lvxinxin on 2017/11/13.
*/
public class OkHttpDemo {
private static OkHttpDemo okHttpDemo;
private OkHttpClient okHttpClient;
private long maxSize = 8 * 1024 * 1024;
private OkHttpDemo() {
File file = new File(Environment.getExternalStorageDirectory() + "/ACache");
Cache cache = new Cache(file, maxSize);
//followRedirects 这是重定向更改状态 之前是自动重定向
okHttpClient = new OkHttpClient.Builder().followRedirects(false).cache(cache).addInterceptor(new Interceptor() {
//Chain是一个链,是Handler的链。也就是说一次请求可能要执行多个Handler。
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest = request.newBuilder().url(MainActivity.mUrl).build();
Response proceed = chain.proceed(newRequest);
//可以清空之前的网址信息
Response newResponse = proceed.newBuilder().removeHeader("Pragma")
.removeHeader("Cache-Control")
.addHeader("Cache-Control", "max-age=" + 1000 * 30).build();
return proceed;
}
}).connectTimeout(8, TimeUnit.SECONDS).readTimeout(8, TimeUnit.SECONDS).writeTimeout(8, TimeUnit.SECONDS).build();
// okHttpClient = new OkHttpClient.Builder().cache(cache).build();
}
public synchronized static OkHttpDemo getInstance() {
if (okHttpDemo == null) {
okHttpDemo = new OkHttpDemo();
return okHttpDemo;
}
return okHttpDemo;
}
//这是同步get
/** 如果没有缓存,进行get请求获取服务器数据并缓存起来
* 如果缓存已经存在:不超过maxStale---->不进行请求,直接返回缓存数据
* 超出了maxStale--->发起请求获取数据更新,请求失败返回失败
*/
public String sendGet(String url) throws IOException {
Request request = new Request.Builder().cacheControl(new CacheControl.Builder().maxStale(10, TimeUnit.SECONDS)
.build()).url(url).build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
public String sendPost(String url, RequestBody body) throws IOException {
Request request = new Request.Builder().url(url).post(body).build();
Response response = okHttpClient.newCall(request).execute();
String string = response.body().string();
return string;
}
//这是异步
public void sendGet(String url, Callback callback) {
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
public void sendPost(String url, RequestBody body, Callback call) {
Request request = new Request.Builder().url(url).post(body).build();
Call call1 = okHttpClient.newCall(request);
call1.enqueue(call);
}
}
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import app.jiyun.com.android_yuekao_test.MainActivity;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by lvxinxin on 2017/11/13.
*/
public class OkHttpDemo {
private static OkHttpDemo okHttpDemo;
private OkHttpClient okHttpClient;
private long maxSize = 8 * 1024 * 1024;
private OkHttpDemo() {
File file = new File(Environment.getExternalStorageDirectory() + "/ACache");
Cache cache = new Cache(file, maxSize);
//followRedirects 这是重定向更改状态 之前是自动重定向
okHttpClient = new OkHttpClient.Builder().followRedirects(false).cache(cache).addInterceptor(new Interceptor() {
//Chain是一个链,是Handler的链。也就是说一次请求可能要执行多个Handler。
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest = request.newBuilder().url(MainActivity.mUrl).build();
Response proceed = chain.proceed(newRequest);
//可以清空之前的网址信息
Response newResponse = proceed.newBuilder().removeHeader("Pragma")
.removeHeader("Cache-Control")
.addHeader("Cache-Control", "max-age=" + 1000 * 30).build();
return proceed;
}
}).connectTimeout(8, TimeUnit.SECONDS).readTimeout(8, TimeUnit.SECONDS).writeTimeout(8, TimeUnit.SECONDS).build();
// okHttpClient = new OkHttpClient.Builder().cache(cache).build();
}
public synchronized static OkHttpDemo getInstance() {
if (okHttpDemo == null) {
okHttpDemo = new OkHttpDemo();
return okHttpDemo;
}
return okHttpDemo;
}
//这是同步get
/** 如果没有缓存,进行get请求获取服务器数据并缓存起来
* 如果缓存已经存在:不超过maxStale---->不进行请求,直接返回缓存数据
* 超出了maxStale--->发起请求获取数据更新,请求失败返回失败
*/
public String sendGet(String url) throws IOException {
Request request = new Request.Builder().cacheControl(new CacheControl.Builder().maxStale(10, TimeUnit.SECONDS)
.build()).url(url).build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
public String sendPost(String url, RequestBody body) throws IOException {
Request request = new Request.Builder().url(url).post(body).build();
Response response = okHttpClient.newCall(request).execute();
String string = response.body().string();
return string;
}
//这是异步
public void sendGet(String url, Callback callback) {
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
public void sendPost(String url, RequestBody body, Callback call) {
Request request = new Request.Builder().url(url).post(body).build();
Call call1 = okHttpClient.newCall(request);
call1.enqueue(call);
}
}