version:
fix:优化请求次数 update:
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
84
app/src/main/java/com/aoleyun/sn/network/NetStateUtils.java
Normal file
84
app/src/main/java/com/aoleyun/sn/network/NetStateUtils.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.aoleyun.sn.network;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
public class NetStateUtils {
|
||||
|
||||
/**
|
||||
* 判断网络连接状态
|
||||
*
|
||||
* @param context
|
||||
* @return true:网络已链接, false:网络已断开连接
|
||||
*/
|
||||
public static boolean isNetworkConnected(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mNetworkInfo = mConnectivityManager
|
||||
.getActiveNetworkInfo();
|
||||
if (mNetworkInfo != null) {
|
||||
return mNetworkInfo.isAvailable();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断wifi状态
|
||||
*
|
||||
* @param context
|
||||
* @return true:是wifi情况 ,false:非wifi情况
|
||||
*/
|
||||
public static boolean isWifiConnected(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
if (mWiFiNetworkInfo != null) {
|
||||
return mWiFiNetworkInfo.isAvailable();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断移动网络
|
||||
*
|
||||
* @param context
|
||||
* @return true:是移动网络情况, false:非移动网络情况
|
||||
*/
|
||||
public static boolean isMobileConnected(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mMobileNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
||||
if (mMobileNetworkInfo != null) {
|
||||
return mMobileNetworkInfo.isAvailable();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接类型
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static int getConnectedType(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mNetworkInfo = mConnectivityManager
|
||||
.getActiveNetworkInfo();
|
||||
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
|
||||
return mNetworkInfo.getType();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,9 @@
|
||||
package com.aoleyun.sn.network;
|
||||
|
||||
import com.aoleyun.sn.BuildConfig;
|
||||
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
public class UrlAddress {
|
||||
/*根网址*/
|
||||
public static final String HTTP_TAG_HEAD_NEW = BuildConfig.ROOT_URL;
|
||||
|
||||
/*app下载完成发送下载次数*/
|
||||
public static final String SEND_DOWNLOAD_FILE_INFO = "app/downloadApp";
|
||||
/*获取系统默认程序*/
|
||||
@@ -36,8 +30,6 @@ public class UrlAddress {
|
||||
public static final String CHECK_UPDATE = "Silent/silent";
|
||||
/*获取设备标签*/
|
||||
public static final String GET_DEVICES_TAGS = "Sn/getSnTag";
|
||||
/*删除Aliases*/
|
||||
public static final String DELETE_GEDEVICE_ALIAS = "Sn/deleteAliases";
|
||||
/*浏览器书签主页设置*/
|
||||
public static final String SET_HOMEPAG_TAG = "Label";
|
||||
/*app内部网页管控*/
|
||||
@@ -78,7 +70,6 @@ public class UrlAddress {
|
||||
public static final String GET_ALL_APP = "recommend/index";
|
||||
/*获取电子书包激活码*/
|
||||
public static final String GET_EBAG_CODE = "And/getEbagCode";
|
||||
|
||||
/*恢复出厂设置次数*/
|
||||
public static final String SEND_RESTORE_TIMES = "And/restore";
|
||||
/*获取远程关机时间*/
|
||||
@@ -99,4 +90,9 @@ public class UrlAddress {
|
||||
public static final String DELETE_JPUSH_ALIAS = "https://device.jpush.cn/v3/aliases/";
|
||||
/*删除tags*/
|
||||
public static final String DELETE_JPUSH_TAG = "https://device.jpush.cn/v3/tags/";
|
||||
|
||||
|
||||
@Deprecated
|
||||
/*删除Aliases*/
|
||||
public static final String DELETE_GEDEVICE_ALIAS = "Sn/deleteAliases";
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.ForceDownloadData;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.DefaultApp;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.SnRunLog;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.LogoImg;
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.ScreenLockState;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
|
||||
import io.reactivex.rxjava3.core.Observable;
|
||||
@@ -9,7 +10,7 @@ import retrofit2.http.Query;
|
||||
|
||||
public interface ScreenLockStateApi {
|
||||
@GET(UrlAddress.GET_LOCK_SCREEN_STATE)
|
||||
Observable<BaseResponse> getScreenLockState(
|
||||
Observable<BaseResponse<ScreenLockState>> getScreenLockState(
|
||||
@Query("sn") String sn
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.get;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
|
||||
import com.aoleyun.sn.bean.Appground;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.BrowserBookmarks;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.BrowserData;
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.AppUpdateInfo;
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
|
||||
@@ -12,7 +13,7 @@ public interface CheckUpdateApi {
|
||||
@FormUrlEncoded
|
||||
@POST(UrlAddress.CHECK_UPDATE)
|
||||
//1MTK平台 2展锐平台
|
||||
Observable<BaseResponse> getUpdate(
|
||||
Observable<BaseResponse<AppUpdateInfo>> getUpdate(
|
||||
@Field("package") String packages,
|
||||
@Field("type") int type
|
||||
);
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.ForceDownloadBean;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.AppListInfo;
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.PoweroffBean;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.SnRunLog;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.SnRunLog;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.NetAndLaunchBean;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.BuildConfig;
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.bean.StudentsInfo;
|
||||
@@ -9,7 +9,7 @@ import retrofit2.http.Field;
|
||||
import retrofit2.http.FormUrlEncoded;
|
||||
import retrofit2.http.POST;
|
||||
|
||||
public interface StudentsInfosApi {
|
||||
public interface StudentsInfoApi {
|
||||
@FormUrlEncoded
|
||||
@POST(UrlAddress.GET_STUDENTS_INFO)
|
||||
Observable<BaseResponse<StudentsInfo>> getStudentsInfo(
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.aoleyun.sn.network.api;
|
||||
package com.aoleyun.sn.network.api.post;
|
||||
|
||||
import com.aoleyun.sn.bean.BaseResponse;
|
||||
import com.aoleyun.sn.network.UrlAddress;
|
||||
@@ -0,0 +1,278 @@
|
||||
package com.aoleyun.sn.network.interceptor;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.aoleyun.sn.BuildConfig;
|
||||
import com.aoleyun.sn.disklrucache.DiskLruCacheHelper;
|
||||
import com.aoleyun.sn.utils.JGYUtils;
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Protocol;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okhttp3.internal.http.HttpHeaders;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSource;
|
||||
|
||||
import static okhttp3.internal.Util.closeQuietly;
|
||||
|
||||
public class PostCacheInterceptor implements Interceptor {
|
||||
final DiskLruCacheHelper cache;
|
||||
|
||||
public PostCacheInterceptor(DiskLruCacheHelper cache) {
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
private final int REQUEST_URL = 0;
|
||||
private final int REQUEST_METHOD = 1;
|
||||
private final int REQUESTCONTENTTYPE = 2;
|
||||
private final int PROTOCAL = 3;
|
||||
private final int CODE = 4;
|
||||
private final int MESSAGE = 5;
|
||||
private final int REPONSE_BODY = 6;
|
||||
private final int MEDIA_TYPE = 7;
|
||||
private final int SETN_REQUEST_AT_MILLIS = 8;
|
||||
private final int RECEIVE_REPONSE_AT_MILLIS = 9;
|
||||
private final int CACHE_LENGTH = 10;
|
||||
|
||||
|
||||
@Override
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
if (!isNeedCache(chain.request().url().toString())) {
|
||||
return chain.proceed(chain.request());
|
||||
}
|
||||
|
||||
//获取缓存
|
||||
String key = createKey(chain.request());
|
||||
LogUtils.d("cache key: " + key);
|
||||
Response cacheResponse = null;
|
||||
String cacheRes = cache != null&&key!=null
|
||||
? cache.getAsString(key)
|
||||
: null;
|
||||
|
||||
if (!TextUtils.isEmpty(cacheRes)) {
|
||||
LogUtils.d("cacheRes: " + cacheRes);
|
||||
cacheResponse = combineCacheToResponse(cacheRes);
|
||||
}
|
||||
|
||||
//没有网络连接的时候读取缓存
|
||||
if (!JGYUtils.getInstance().isNetworkConnected()) {
|
||||
LogUtils.d("no network connected jujge cache available");
|
||||
if (cacheResponse != null) {
|
||||
LogUtils.d("no network connected, return cache: " + cacheResponse);
|
||||
return cacheResponse;
|
||||
}
|
||||
}
|
||||
|
||||
LogUtils.d("waiting for network response...");
|
||||
//获取网络响应
|
||||
Request netWorkRequest = chain.request();
|
||||
Response networkResponse = null;
|
||||
try {
|
||||
networkResponse = chain.proceed(netWorkRequest);
|
||||
} finally {
|
||||
if (networkResponse == null) {
|
||||
LogUtils.d("close cache response...");
|
||||
if (cacheResponse!=null&&HttpHeaders.hasBody(cacheResponse)){
|
||||
closeQuietly(cacheResponse.body());
|
||||
}
|
||||
return chain.proceed(netWorkRequest);
|
||||
}
|
||||
}
|
||||
LogUtils.d("prepare update cache response...");
|
||||
//更新缓存
|
||||
if (cacheResponse != null) {
|
||||
Response response = null;
|
||||
response = networkResponse.newBuilder()
|
||||
.request(new Request.Builder()
|
||||
.method("GET", null)
|
||||
.url(netWorkRequest.url())
|
||||
.headers(netWorkRequest.headers())
|
||||
.tag(netWorkRequest.tag())
|
||||
.build())
|
||||
.build();
|
||||
LogUtils.d("update cache response");
|
||||
if (key!=null){
|
||||
cache.put(key, createCache(response));
|
||||
}
|
||||
if (cacheResponse!=null&&HttpHeaders.hasBody(cacheResponse)){
|
||||
closeQuietly(cacheResponse.body());
|
||||
}
|
||||
return networkResponse;
|
||||
|
||||
}
|
||||
|
||||
Request newRequest = new Request.Builder()
|
||||
.method("GET", null)
|
||||
.url(netWorkRequest.url())
|
||||
.headers(netWorkRequest.headers())
|
||||
.tag(netWorkRequest.tag())
|
||||
.build();
|
||||
|
||||
Response newResponse = networkResponse.newBuilder()
|
||||
.request(newRequest)
|
||||
.build();
|
||||
|
||||
LogUtils.d("init cache response");
|
||||
//放入缓存
|
||||
if (cache != null) {
|
||||
LogUtils.d("url: " + netWorkRequest.url().toString());
|
||||
if (HttpHeaders.hasBody(newResponse)) {
|
||||
try {
|
||||
LogUtils.d("chain request url: " + newResponse.request().url());
|
||||
if (key!=null){
|
||||
cache.put(key, createCache(newResponse));
|
||||
LogUtils.d("put cache response key: " + key);
|
||||
}
|
||||
// String resp1 = cache.getAsString(key);
|
||||
// LogUtils.d("resp1: " + resp1);
|
||||
return networkResponse;
|
||||
} catch (Exception e) {
|
||||
LogUtils.d("put cache exception: " + e);
|
||||
}finally {
|
||||
if (cacheResponse != null && HttpHeaders.hasBody(cacheResponse)) {
|
||||
closeQuietly(cacheResponse.body());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return networkResponse;
|
||||
}
|
||||
|
||||
|
||||
private String createKey(Request request) {
|
||||
RequestBody requestBody = request.body();
|
||||
Charset charset = Charset.forName("UTF-8");
|
||||
String url = request.url().toString();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(url + "&");
|
||||
MediaType type = requestBody.contentType();
|
||||
if (type != null) {
|
||||
charset = type.charset() == null ? charset : type.charset();
|
||||
}
|
||||
Buffer buffer = new Buffer();
|
||||
try {
|
||||
requestBody.writeTo(buffer);
|
||||
sb.append(buffer.readString(charset));
|
||||
} catch (Exception e) {
|
||||
LogUtils.d("read request error: " + e);
|
||||
} finally {
|
||||
buffer.close();
|
||||
}
|
||||
// if (url.startsWith(BuildConfig.ROOT_URL + "your own url")) {
|
||||
// return //这里可以根据url来定制化key
|
||||
// }
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
//根据键返回索引
|
||||
private int[] getIndexofKeyValue(String str, String originStr) {
|
||||
int[] indexs = new int[2];
|
||||
indexs[0] = originStr.indexOf(str);
|
||||
indexs[1] = originStr.indexOf("&", indexs[0]) >= 0 ? originStr.indexOf("&", indexs[0]) : originStr.length();
|
||||
LogUtils.d("index0: " + indexs[0] + " index1: " + indexs[1]);
|
||||
return indexs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private boolean isNeedCache(String url) {
|
||||
//这里可以根据Url来判断是否需要缓存
|
||||
return true;
|
||||
}
|
||||
|
||||
private Response combineCacheToResponse(String cache) {
|
||||
String[] caches = cache.split("&#&#");
|
||||
if (caches == null || caches.length <= 0) {
|
||||
return null;
|
||||
}
|
||||
Request request = new Request.Builder()
|
||||
.url(caches[REQUEST_URL])
|
||||
.method(caches[REQUEST_METHOD], null)
|
||||
.build();
|
||||
Response.Builder builder = new Response.Builder();
|
||||
try {
|
||||
builder.protocol(Protocol.get(caches[PROTOCAL]));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return builder.message(caches[MESSAGE])
|
||||
.code(Integer.valueOf(caches[CODE]))
|
||||
.request(request)
|
||||
.receivedResponseAtMillis(Long.valueOf(caches[RECEIVE_REPONSE_AT_MILLIS]))
|
||||
.sentRequestAtMillis(Long.valueOf(caches[SETN_REQUEST_AT_MILLIS]))
|
||||
.body(ResponseBody.create(MediaType.parse(caches[MEDIA_TYPE]), caches[REPONSE_BODY]))
|
||||
.build();
|
||||
}
|
||||
|
||||
private String createCache(Response response) {
|
||||
String[] caches = new String[CACHE_LENGTH];
|
||||
caches[REQUEST_URL] = response.request().url().toString();
|
||||
caches[REQUEST_METHOD] = response.request().method();
|
||||
if (response.request().body() != null && response.request().body().contentType() != null) {
|
||||
caches[REQUESTCONTENTTYPE] = response.request().body().contentType().toString();
|
||||
} else {
|
||||
caches[REQUESTCONTENTTYPE] = "application/x-www-form-urlencoded";
|
||||
}
|
||||
caches[PROTOCAL] = response.protocol().toString();
|
||||
caches[CODE] = response.code() + "";
|
||||
caches[MESSAGE] = response.message();
|
||||
if (response.body() != null && response.body().contentType() != null) {
|
||||
caches[MEDIA_TYPE] = response.body().contentType().toString();
|
||||
} else {
|
||||
caches[MEDIA_TYPE] = "application/x-www-form-urlencoded";
|
||||
}
|
||||
caches[SETN_REQUEST_AT_MILLIS] = response.sentRequestAtMillis() + "";
|
||||
caches[RECEIVE_REPONSE_AT_MILLIS] = response.receivedResponseAtMillis() + "";
|
||||
if (HttpHeaders.hasBody(response)) {
|
||||
BufferedSource source = response.body().source();
|
||||
Buffer buffer = null;
|
||||
try {
|
||||
source.request(Long.MAX_VALUE);
|
||||
buffer = source.buffer();
|
||||
Charset charset = response.body().contentType().charset();
|
||||
if (charset == null) {
|
||||
charset = Charset.forName("UTF-8");
|
||||
}
|
||||
caches[REPONSE_BODY] = buffer.clone().readString(charset);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
// closeQuietly(response.body());
|
||||
}
|
||||
}
|
||||
String cache = "";
|
||||
for (String str : caches) {
|
||||
cache += str + "&#&#";
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
static boolean isEndToEnd(String fieldName) {
|
||||
return !"Connection".equalsIgnoreCase(fieldName)
|
||||
&& !"Keep-Alive".equalsIgnoreCase(fieldName)
|
||||
&& !"Proxy-Authenticate".equalsIgnoreCase(fieldName)
|
||||
&& !"Proxy-Authorization".equalsIgnoreCase(fieldName)
|
||||
&& !"TE".equalsIgnoreCase(fieldName)
|
||||
&& !"Trailers".equalsIgnoreCase(fieldName)
|
||||
&& !"Transfer-Encoding".equalsIgnoreCase(fieldName)
|
||||
&& !"Upgrade".equalsIgnoreCase(fieldName);
|
||||
}
|
||||
|
||||
private String subString(String str, int[] index) {
|
||||
if (index == null || index.length < 2) {
|
||||
return null;
|
||||
}
|
||||
if (index[0] < 0 || index[1] < 0) {
|
||||
return null;
|
||||
}
|
||||
return str.substring(index[0], index[1]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.aoleyun.sn.network.interceptor;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.aoleyun.sn.utils.MD5Util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Protocol;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class RepeatRequestInterceptor implements Interceptor {
|
||||
private static final String TAG = RepeatRequestInterceptor.class.getSimpleName();
|
||||
|
||||
private final ConcurrentHashMap<String, Long> requestIdsMap = new ConcurrentHashMap<>();
|
||||
public static final String REPEAT_REQUEST_PROTOCOL = "OKHTTP_REPEAT_REQUEST_PROTOCOL";
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Response intercept(@NotNull Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
//相同的请求
|
||||
String requestKey = MD5Util.getUpperMD5Str(request.method() + request.url().toString() + request.body());
|
||||
long time = System.currentTimeMillis();//请求时间
|
||||
try {
|
||||
if (requestIdsMap.size() > 0 && requestIdsMap.containsKey(requestKey)) {
|
||||
Log.e("REPEAT-REQUEST", "重复请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url() + "\t" + request.body());
|
||||
//下面这行写了不会抛出onerror
|
||||
// chain.call().cancel();
|
||||
return new Response.Builder()
|
||||
.protocol(Protocol.get(REPEAT_REQUEST_PROTOCOL))
|
||||
.request(request) //multi thread
|
||||
.build();
|
||||
}
|
||||
requestIdsMap.put(requestKey, time);
|
||||
Log.e("REPEAT-REQUEST", "注册请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url() + "\t" + request.body());
|
||||
// RepeatRequestInterceptor.Builder builder = request.newBuilder();
|
||||
// builder.addHeader("header", jsonObject.toString());
|
||||
return chain.proceed(request);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "intercept: " + e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (requestIdsMap.containsKey(requestKey) && requestIdsMap.containsValue(time)) {//请求任务完成删除map中的数据
|
||||
requestIdsMap.remove(requestKey);
|
||||
Log.e("REPEAT-REQUEST", "移除请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url() + "\t" + request.body());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user