Commit 68548d3e04d6e993f4372ca32c05e06b88c7e724
1 parent
bf02f60e
Exists in
master
优化okhttpclient
Showing
1 changed file
with
31 additions
and
26 deletions
Show diff stats
src/main/java/com/taover/util/UtilHttpByOkHttp.java
... | ... | @@ -30,6 +30,10 @@ import okhttp3.Response; |
30 | 30 | import okhttp3.ResponseBody; |
31 | 31 | |
32 | 32 | public class UtilHttpByOkHttp { |
33 | + //防止线程并发,使用threadlocal管理client | |
34 | + private static ThreadLocal<OkHttpClient> localHttpClientForHttp = new ThreadLocal<OkHttpClient>(); | |
35 | + private static ThreadLocal<OkHttpClient> localHttpClientForHttps = new ThreadLocal<OkHttpClient>(); | |
36 | + | |
33 | 37 | public static final String METHOD_GET = "GET"; |
34 | 38 | public static final String METHOD_POST = "POST"; |
35 | 39 | public static final String METHOD_DELETE = "DELETE"; |
... | ... | @@ -125,16 +129,36 @@ public class UtilHttpByOkHttp { |
125 | 129 | |
126 | 130 | |
127 | 131 | public static OkHttpClient getHttpClient(String url, int timeoutInSecond){ |
128 | - if(url.trim().toLowerCase().startsWith("https")){ | |
129 | - return buildOkHttpClientForHttps(timeoutInSecond); | |
132 | + OkHttpClient client = null; | |
133 | + boolean isHttps = url.trim().toLowerCase().startsWith("https"); | |
134 | + if(isHttps){ | |
135 | + client = localHttpClientForHttps.get(); | |
136 | + }else{ | |
137 | + client = localHttpClientForHttp.get(); | |
138 | + } | |
139 | + | |
140 | + if(client != null){ | |
141 | + return client; | |
142 | + } | |
143 | + | |
144 | + if(isHttps){ | |
145 | + client = buildOkHttpClientForHttps(timeoutInSecond); | |
130 | 146 | }else{ |
131 | 147 | OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
132 | 148 | builder.connectTimeout(timeoutInSecond, TimeUnit.SECONDS) |
133 | 149 | .readTimeout(timeoutInSecond, TimeUnit.SECONDS) |
134 | 150 | .writeTimeout(timeoutInSecond,TimeUnit.SECONDS) |
135 | 151 | .retryOnConnectionFailure(true); |
136 | - return builder.build(); | |
152 | + client = builder.build(); | |
153 | + } | |
154 | + | |
155 | + if(isHttps){ | |
156 | + localHttpClientForHttps.set(client); | |
157 | + }else{ | |
158 | + localHttpClientForHttp.set(client); | |
137 | 159 | } |
160 | + | |
161 | + return client; | |
138 | 162 | } |
139 | 163 | |
140 | 164 | /** |
... | ... | @@ -216,30 +240,11 @@ public class UtilHttpByOkHttp { |
216 | 240 | * @return 只发post请求 |
217 | 241 | * @throws Exception |
218 | 242 | */ |
219 | - public static ResponseBody sendPostFile(String url, String filePath, String fileName,String fileKey,Map<String, String> header) throws Exception { | |
220 | - OkHttpClient client = new OkHttpClient(); | |
221 | - RequestBody requestBody = new MultipartBody.Builder() | |
222 | - .setType(MultipartBody.FORM) | |
223 | - .addFormDataPart(fileKey, fileName, | |
224 | - RequestBody.create(MediaType.parse("multipart/form-data"), new File(filePath))) | |
225 | - .build(); | |
226 | - | |
227 | - Request request = new Request.Builder() | |
228 | - .headers(Headers.of(header)) | |
229 | - .url(url) | |
230 | - .post(requestBody) | |
231 | - .build(); | |
232 | - | |
233 | - Response response = client.newCall(request).execute(); | |
234 | - if (!response.isSuccessful()){ | |
235 | - throw new IOException("Unexpected code " + response); | |
236 | - } | |
237 | - return response.body(); | |
238 | - } | |
239 | - | |
240 | - | |
243 | + public static ResponseBody sendPostFile(String url, String filePath, String fileName, String fileKey, Map<String, String> header) throws Exception { | |
244 | + return sendPostFile(url, new File(filePath), fileKey, header); | |
245 | + } | |
241 | 246 | |
242 | - public static ResponseBody sendPostFile(String url, File file,String fileKey,Map<String, String> header) throws Exception { | |
247 | + public static ResponseBody sendPostFile(String url, File file, String fileKey, Map<String, String> header) throws Exception { | |
243 | 248 | OkHttpClient client = new OkHttpClient(); |
244 | 249 | RequestBody requestBody = new MultipartBody.Builder() |
245 | 250 | .setType(MultipartBody.FORM) | ... | ... |