Commit 831fc935376d876bba775db0d18c1fe69037eee0
1 parent
f4ff510176
Exists in
master
提升服务器超时允许时间
Showing
1 changed file
with
3 additions
and
3 deletions
Show diff stats
libs/common/src/main/java/com/prws/common/net/OkHttp3Utils.java
1 | package com.prws.common.net; | 1 | package com.prws.common.net; |
2 | 2 | ||
3 | import android.content.Context; | 3 | import android.content.Context; |
4 | import android.net.ConnectivityManager; | 4 | import android.net.ConnectivityManager; |
5 | import android.net.NetworkInfo; | 5 | import android.net.NetworkInfo; |
6 | import android.util.Log; | 6 | import android.util.Log; |
7 | 7 | ||
8 | 8 | ||
9 | import com.chuckerteam.chucker.api.ChuckerInterceptor; | 9 | import com.chuckerteam.chucker.api.ChuckerInterceptor; |
10 | import com.prws.common.CommonApplication; | 10 | import com.prws.common.CommonApplication; |
11 | 11 | ||
12 | import java.io.File; | 12 | import java.io.File; |
13 | import java.io.IOException; | 13 | import java.io.IOException; |
14 | import java.util.List; | 14 | import java.util.List; |
15 | import java.util.concurrent.TimeUnit; | 15 | import java.util.concurrent.TimeUnit; |
16 | 16 | ||
17 | import okhttp3.Cache; | 17 | import okhttp3.Cache; |
18 | import okhttp3.CacheControl; | 18 | import okhttp3.CacheControl; |
19 | import okhttp3.Cookie; | 19 | import okhttp3.Cookie; |
20 | import okhttp3.CookieJar; | 20 | import okhttp3.CookieJar; |
21 | import okhttp3.HttpUrl; | 21 | import okhttp3.HttpUrl; |
22 | import okhttp3.Interceptor; | 22 | import okhttp3.Interceptor; |
23 | import okhttp3.OkHttpClient; | 23 | import okhttp3.OkHttpClient; |
24 | import okhttp3.Request; | 24 | import okhttp3.Request; |
25 | import okhttp3.Response; | 25 | import okhttp3.Response; |
26 | 26 | ||
27 | /** | 27 | /** |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | public class OkHttp3Utils { | 31 | public class OkHttp3Utils { |
32 | 32 | ||
33 | private static OkHttpClient mOkHttpClient; | 33 | private static OkHttpClient mOkHttpClient; |
34 | 34 | ||
35 | //设置缓存目录 | 35 | //设置缓存目录 |
36 | private static File cacheDirectory = new File(CommonApplication.getAppContext().getApplicationContext().getCacheDir().getAbsolutePath(), "MyCache"); | 36 | private static File cacheDirectory = new File(CommonApplication.getAppContext().getApplicationContext().getCacheDir().getAbsolutePath(), "MyCache"); |
37 | private static Cache cache = new Cache(cacheDirectory, 10 * 1024 * 1024); | 37 | private static Cache cache = new Cache(cacheDirectory, 10 * 1024 * 1024); |
38 | 38 | ||
39 | 39 | ||
40 | /** | 40 | /** |
41 | * 获取OkHttpClient对象 | 41 | * 获取OkHttpClient对象 |
42 | * | 42 | * |
43 | * @return | 43 | * @return |
44 | */ | 44 | */ |
45 | public static OkHttpClient getOkHttpClient() { | 45 | public static OkHttpClient getOkHttpClient() { |
46 | if (null == mOkHttpClient) { | 46 | if (null == mOkHttpClient) { |
47 | 47 | ||
48 | //同样okhttp3后也使用build设计模式 | 48 | //同样okhttp3后也使用build设计模式 |
49 | mOkHttpClient = new OkHttpClient.Builder() | 49 | mOkHttpClient = new OkHttpClient.Builder() |
50 | //设置一个自动管理cookies的管理器 | 50 | //设置一个自动管理cookies的管理器 |
51 | .cookieJar(new CookiesManager()) | 51 | .cookieJar(new CookiesManager()) |
52 | .cache(cache) | 52 | .cache(cache) |
53 | .addInterceptor(new ChuckerInterceptor.Builder(CommonApplication.getAppContext()).build()) | 53 | .addInterceptor(new ChuckerInterceptor.Builder(CommonApplication.getAppContext()).build()) |
54 | .addInterceptor(new SignInterceptor()) | 54 | .addInterceptor(new SignInterceptor()) |
55 | //添加拦截器 | 55 | //添加拦截器 |
56 | .addInterceptor(new MyIntercepter()) | 56 | .addInterceptor(new MyIntercepter()) |
57 | .addInterceptor(new LoggingInterceptor()) | 57 | .addInterceptor(new LoggingInterceptor()) |
58 | .addInterceptor(new OkHttpRetryInterceptor.Builder() | 58 | .addInterceptor(new OkHttpRetryInterceptor.Builder() |
59 | // .buildRetryCount(3)//最大重试次数 | 59 | // .buildRetryCount(3)//最大重试次数 |
60 | .buildRetryInterval(0)//重试时间间隔 | 60 | .buildRetryInterval(0)//重试时间间隔 |
61 | .build()) | 61 | .build()) |
62 | //添加网络连接器 | 62 | //添加网络连接器 |
63 | // .addNetworkInterceptor(new CookiesInterceptor(MyApplication.getInstance().getApplicationContext())) | 63 | // .addNetworkInterceptor(new CookiesInterceptor(MyApplication.getInstance().getApplicationContext())) |
64 | //设置请求读写的超时时间 | 64 | //设置请求读写的超时时间 |
65 | .connectTimeout(10, TimeUnit.SECONDS) | 65 | .connectTimeout(20, TimeUnit.SECONDS) |
66 | .writeTimeout(10, TimeUnit.SECONDS) | 66 | .writeTimeout(60, TimeUnit.SECONDS) |
67 | .readTimeout(10, TimeUnit.SECONDS) | 67 | .readTimeout(60, TimeUnit.SECONDS) |
68 | .build(); | 68 | .build(); |
69 | 69 | ||
70 | } | 70 | } |
71 | 71 | ||
72 | 72 | ||
73 | return mOkHttpClient; | 73 | return mOkHttpClient; |
74 | } | 74 | } |
75 | 75 | ||
76 | 76 | ||
77 | /** | 77 | /** |
78 | * 拦截器 | 78 | * 拦截器 |
79 | */ | 79 | */ |
80 | private static class MyIntercepter implements Interceptor { | 80 | private static class MyIntercepter implements Interceptor { |
81 | @Override | 81 | @Override |
82 | public Response intercept(Chain chain) throws IOException { | 82 | public Response intercept(Chain chain) throws IOException { |
83 | Request request = chain.request(); | 83 | Request request = chain.request(); |
84 | if (!isNetworkReachable(CommonApplication.getAppContext().getApplicationContext())) { | 84 | if (!isNetworkReachable(CommonApplication.getAppContext().getApplicationContext())) { |
85 | request = request.newBuilder() | 85 | request = request.newBuilder() |
86 | .cacheControl(CacheControl.FORCE_CACHE)//无网络时只从缓存中读取 | 86 | .cacheControl(CacheControl.FORCE_CACHE)//无网络时只从缓存中读取 |
87 | .build(); | 87 | .build(); |
88 | Log.e("缓存目录====>",cacheDirectory.getAbsolutePath()); | 88 | Log.e("缓存目录====>",cacheDirectory.getAbsolutePath()); |
89 | } | 89 | } |
90 | 90 | ||
91 | Response response = chain.proceed(request); | 91 | Response response = chain.proceed(request); |
92 | if (isNetworkReachable(CommonApplication.getAppContext().getApplicationContext())) { | 92 | if (isNetworkReachable(CommonApplication.getAppContext().getApplicationContext())) { |
93 | int maxAge = 60 * 60; // 有网络时 设置缓存超时时间1个小时 | 93 | int maxAge = 60 * 60; // 有网络时 设置缓存超时时间1个小时 |
94 | response.newBuilder() | 94 | response.newBuilder() |
95 | .removeHeader("Pragma") | 95 | .removeHeader("Pragma") |
96 | .removeHeader("Cache-Control") | 96 | .removeHeader("Cache-Control") |
97 | .header("Cache-Control", "public, max-age=" + maxAge) | 97 | .header("Cache-Control", "public, max-age=" + maxAge) |
98 | .build(); | 98 | .build(); |
99 | } else { | 99 | } else { |
100 | int maxStale = 60 * 60 * 24 * 28; // 无网络时,设置超时为4周 | 100 | int maxStale = 60 * 60 * 24 * 28; // 无网络时,设置超时为4周 |
101 | response.newBuilder() | 101 | response.newBuilder() |
102 | .removeHeader("Pragma") | 102 | .removeHeader("Pragma") |
103 | .removeHeader("Cache-Control") | 103 | .removeHeader("Cache-Control") |
104 | .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) | 104 | .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) |
105 | .build(); | 105 | .build(); |
106 | } | 106 | } |
107 | return response; | 107 | return response; |
108 | } | 108 | } |
109 | } | 109 | } |
110 | 110 | ||
111 | 111 | ||
112 | /** | 112 | /** |
113 | * 自动管理Cookies | 113 | * 自动管理Cookies |
114 | */ | 114 | */ |
115 | private static class CookiesManager implements CookieJar { | 115 | private static class CookiesManager implements CookieJar { |
116 | private final PersistentCookieStore cookieStore = new PersistentCookieStore(CommonApplication.getAppContext().getApplicationContext()); | 116 | private final PersistentCookieStore cookieStore = new PersistentCookieStore(CommonApplication.getAppContext().getApplicationContext()); |
117 | 117 | ||
118 | @Override | 118 | @Override |
119 | public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { | 119 | public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { |
120 | if (cookies != null && cookies.size() > 0) { | 120 | if (cookies != null && cookies.size() > 0) { |
121 | for (Cookie item : cookies) { | 121 | for (Cookie item : cookies) { |
122 | cookieStore.add(url, item); | 122 | cookieStore.add(url, item); |
123 | } | 123 | } |
124 | } | 124 | } |
125 | } | 125 | } |
126 | 126 | ||
127 | @Override | 127 | @Override |
128 | public List<Cookie> loadForRequest(HttpUrl url) { | 128 | public List<Cookie> loadForRequest(HttpUrl url) { |
129 | List<Cookie> cookies = cookieStore.get(url); | 129 | List<Cookie> cookies = cookieStore.get(url); |
130 | return cookies; | 130 | return cookies; |
131 | } | 131 | } |
132 | } | 132 | } |
133 | 133 | ||
134 | /** | 134 | /** |
135 | * 判断网络是否可用 | 135 | * 判断网络是否可用 |
136 | * | 136 | * |
137 | * @param context Context对象 | 137 | * @param context Context对象 |
138 | */ | 138 | */ |
139 | public static Boolean isNetworkReachable(Context context) { | 139 | public static Boolean isNetworkReachable(Context context) { |
140 | ConnectivityManager cm = (ConnectivityManager) context | 140 | ConnectivityManager cm = (ConnectivityManager) context |
141 | .getSystemService(Context.CONNECTIVITY_SERVICE); | 141 | .getSystemService(Context.CONNECTIVITY_SERVICE); |
142 | NetworkInfo current = cm.getActiveNetworkInfo(); | 142 | NetworkInfo current = cm.getActiveNetworkInfo(); |
143 | if (current == null) { | 143 | if (current == null) { |
144 | return false; | 144 | return false; |
145 | } | 145 | } |
146 | return (current.isAvailable()); | 146 | return (current.isAvailable()); |
147 | } | 147 | } |
148 | } | 148 | } |
149 | 149 |