public class OkHttpManager {
private static OkHttpClient mOkHttpClient;
private static volatile OkHttpManager instance = null;
private OkHttpManager() {
}
public static OkHttpManager getInstance() {
if (mOkHttpClient == null) {
if (instance == null) {
synchronized (OkHttpManager.class) {
if (mOkHttpClient == null)
mOkHttpClient = new OkHttpClient();
if (instance == null)
instance = new OkHttpManager();
initOkHttpClient();
}
}
}
return instance;
}
/**
* 设置请求超时时间以及缓存
*/
private static void initOkHttpClient() {
//设置缓存目录
File sdcache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cache");
//设置缓存大小,10MB
int cacheSize = 1024 * 1024 * 10;
OkHttpClient.Builder builder = new OkHttpClient.Builder()
//连接超时时间 10S
.connectTimeout(10, TimeUnit.SECONDS)
//写入超时时间 20S
.writeTimeout(20, TimeUnit.SECONDS)
//读取超时时间 20S
.readTimeout(20, TimeUnit.SECONDS)
//缓存
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
//将请求设置参数赋值给OkHttp对象
mOkHttpClient = builder.build();
}
/**
* get异步请求
*
* @param what 唯一识别码
* @param httpCallBack 回调
* @param httpUrl 请求地址
*/
publi