OkHttpUtil.java 16.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
package com.xly.util;

import okhttp3.*;
import okio.Buffer;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * OkHttp3通用工具类
 * 支持GET/POST/PUT/DELETE请求,同步/异步调用,文件上传/下载等
 */
public class OkHttpUtil {

    private static volatile OkHttpUtil instance;
    private OkHttpClient client;

    // 默认配置
    private static final long CONNECT_TIMEOUT = 10;
    private static final long READ_TIMEOUT = 30;
    private static final long WRITE_TIMEOUT = 30;

    private OkHttpUtil() {
        initClient();
    }

    private OkHttpUtil(long connectTimeout, long readTimeout, long writeTimeout) {
        initClient(connectTimeout, readTimeout, writeTimeout);
    }

    /**
     * 获取单例实例(默认配置)
     */
    public static OkHttpUtil getInstance() {
        if (instance == null) {
            synchronized (OkHttpUtil.class) {
                if (instance == null) {
                    instance = new OkHttpUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 获取自定义配置的单例
     */
    public static OkHttpUtil getInstance(long connectTimeout, long readTimeout, long writeTimeout) {
        return new OkHttpUtil(connectTimeout, readTimeout, writeTimeout);
    }

    /**
     * 初始化默认客户端
     */
    private void initClient() {
        initClient(CONNECT_TIMEOUT, READ_TIMEOUT, WRITE_TIMEOUT);
    }

    /**
     * 初始化自定义客户端
     */
    private void initClient(long connectTimeout, long readTimeout, long writeTimeout) {
        client = new OkHttpClient.Builder()
                .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                .readTimeout(readTimeout, TimeUnit.SECONDS)
                .writeTimeout(writeTimeout, TimeUnit.SECONDS)
                .addInterceptor(new LoggingInterceptor())
                .build();
    }

    /**
     * 更新客户端配置
     */
    public void updateClient(OkHttpClient.Builder builder) {
        client = builder.build();
    }

    // ==================== 同步请求方法 ====================

    /**
     * 同步GET请求
     */
    public String get(String url) throws IOException {
        return get(url, null, null);
    }

    public String get(String url, Map<String, String> headers) throws IOException {
        return get(url, headers, null);
    }

    public String get(String url, Map<String, String> headers, Map<String, String> params) throws IOException {
        Request request = buildRequest(url, "GET", headers, params, null);
        return executeRequest(request);
    }

    /**
     * 同步POST请求 - JSON
     */
    public String postJson(String url, String json) throws IOException {
        return postJson(url, null, json);
    }

    public String postJson(String url, Map<String, String> headers, String json) throws IOException {
        RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
        Request request = buildRequest(url, "POST", headers, null, body);
        return executeRequest(request);
    }

    /**
     * 同步POST请求 - Form表单
     */
    public String postForm(String url, Map<String, String> formParams) throws IOException {
        return postForm(url, null, formParams);
    }

    public String postForm(String url, Map<String, String> headers, Map<String, String> formParams) throws IOException {
        FormBody.Builder builder = new FormBody.Builder();
        if (formParams != null) {
            for (Map.Entry<String, String> entry : formParams.entrySet()) {
                builder.add(entry.getKey(), entry.getValue());
            }
        }
        Request request = buildRequest(url, "POST", headers, null, builder.build());
        return executeRequest(request);
    }

    /**
     * 同步POST请求 - 多部分表单(文件上传)
     */
    public String uploadFile(String url, Map<String, String> headers,
                             Map<String, String> formParams,
                             Map<String, File> files) throws IOException {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);

        // 添加普通表单参数
        if (formParams != null) {
            for (Map.Entry<String, String> entry : formParams.entrySet()) {
                builder.addFormDataPart(entry.getKey(), entry.getValue());
            }
        }

        // 添加文件
        if (files != null) {
            for (Map.Entry<String, File> entry : files.entrySet()) {
                File file = entry.getValue();
                if (file.exists()) {
                    RequestBody fileBody = RequestBody.create(file,
                            MediaType.parse("application/octet-stream"));
                    builder.addFormDataPart(entry.getKey(), file.getName(), fileBody);
                }
            }
        }

        Request request = buildRequest(url, "POST", headers, null, builder.build());
        return executeRequest(request);
    }

    /**
     * 同步PUT请求
     */
    public String putJson(String url, String json) throws IOException {
        return putJson(url, null, json);
    }

    public String putJson(String url, Map<String, String> headers, String json) throws IOException {
        RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
        Request request = buildRequest(url, "PUT", headers, null, body);
        return executeRequest(request);
    }

    /**
     * 同步DELETE请求
     */
    public String delete(String url) throws IOException {
        return delete(url, null);
    }

    public String delete(String url, Map<String, String> headers) throws IOException {
        Request request = buildRequest(url, "DELETE", headers, null, null);
        return executeRequest(request);
    }

    // ==================== 异步请求方法 ====================

    /**
     * 异步GET请求
     */
    public void getAsync(String url, Callback callback) {
        getAsync(url, null, null, callback);
    }

    public void getAsync(String url, Map<String, String> headers,
                         Map<String, String> params, Callback callback) {
        Request request = buildRequest(url, "GET", headers, params, null);
        executeAsync(request, callback);
    }

    /**
     * 异步POST请求 - JSON
     */
    public void postJsonAsync(String url, String json, Callback callback) {
        postJsonAsync(url, null, json, callback);
    }

    public void postJsonAsync(String url, Map<String, String> headers,
                              String json, Callback callback) {
        RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
        Request request = buildRequest(url, "POST", headers, null, body);
        executeAsync(request, callback);
    }

    // ==================== 通用构建方法 ====================

    /**
     * 构建请求
     */
    private Request buildRequest(String url, String method,
                                 Map<String, String> headers,
                                 Map<String, String> params,
                                 RequestBody body) {
        // 处理URL参数
        HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
            }
        }

        // 构建请求
        Request.Builder requestBuilder = new Request.Builder()
                .url(urlBuilder.build());

        // 添加请求头
        if (headers != null && !headers.isEmpty()) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        // 设置请求方法和请求体
        switch (method.toUpperCase()) {
            case "GET":
                requestBuilder.get();
                break;
            case "POST":
                if (body != null) {
                    requestBuilder.post(body);
                } else {
                    requestBuilder.post(RequestBody.create("", null));
                }
                break;
            case "PUT":
                requestBuilder.put(body != null ? body : RequestBody.create("", null));
                break;
            case "DELETE":
                requestBuilder.delete(body != null ? body : RequestBody.create("", null));
                break;
            default:
                throw new IllegalArgumentException("Unsupported HTTP method: " + method);
        }

        return requestBuilder.build();
    }

    /**
     * 执行同步请求
     */
    private String executeRequest(Request request) throws IOException {
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code: " + response.code() + ", message: " + response.message());
            }
            ResponseBody body = response.body();
            return body != null ? body.string() : "";
        }
    }

    /**
     * 执行异步请求
     */
    private void executeAsync(Request request, Callback callback) {
        client.newCall(request).enqueue(callback != null ? callback : new DefaultCallback());
    }

    // ==================== 工具方法 ====================

    /**
     * 将字符串数组转换为Header Map
     * 格式: ["key1", "value1", "key2", "value2", ...]
     */
    public static Map<String, String> arrayToHeaderMap(String[] headerArray) {
        Map<String, String> headers = new HashMap<>();
        if (headerArray == null || headerArray.length % 2 != 0) {
            return headers;
        }

        for (int i = 0; i < headerArray.length; i += 2) {
            if (i + 1 < headerArray.length) {
                headers.put(headerArray[i], headerArray[i + 1]);
            }
        }
        return headers;
    }

    /**
     * 将字符串数组转换为参数Map
     */
    public static Map<String, String> arrayToParamMap(String[] paramArray) {
        return arrayToHeaderMap(paramArray); // 逻辑相同
    }

    /**
     * 构建URL参数字符串
     */
    public static String buildQueryString(Map<String, String> params) {
        if (params == null || params.isEmpty()) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (sb.length() > 0) {
                sb.append("&");
            }
            sb.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue());
        }
        return sb.toString();
    }

    /**
     * 下载文件
     */
    public boolean downloadFile(String url, String savePath) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                ResponseBody body = response.body();
                if (body != null) {
                    File file = new File(savePath);
                    // 这里可以添加文件写入逻辑
                    // 或者使用body.byteStream()处理大文件
                    return true;
                }
            }
            return false;
        }
    }

    // ==================== 内部类 ====================

    /**
     * 默认回调
     */
    private static class DefaultCallback implements Callback {
        @Override
        public void onFailure(Call call, IOException e) {
            System.err.println("Request failed: " + e.getMessage());
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                String responseData = response.body().string();
                System.out.println("Response: " + responseData);
            } else {
                System.err.println("Request failed with code: " + response.code());
            }
        }
    }

    /**
     * 日志拦截器
     */
    private static class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            long startTime = System.nanoTime();
            System.out.println(String.format("Sending request %s on %s%n%s",
                    request.url(), chain.connection(), request.headers()));

            // 打印请求体(如果是可读的)
            if (request.body() != null) {
                Buffer buffer = new Buffer();
                request.body().writeTo(buffer);
                System.out.println("Request Body: " + buffer.readString(StandardCharsets.UTF_8));
            }

            Response response = chain.proceed(request);

            long endTime = System.nanoTime();
            System.out.println(String.format("Received response for %s in %.1fms%n%s",
                    response.request().url(), (endTime - startTime) / 1e6d, response.headers()));

            return response;
        }
    }

    // ==================== Getter/Setter ====================

    public OkHttpClient getClient() {
        return client;
    }

    public void setClient(OkHttpClient client) {
        this.client = client;
    }

    public static void main(String[] args) {
        // 1. 获取实例
        OkHttpUtil httpUtil = OkHttpUtil.getInstance();

        // 2. 同步GET请求
        try {
            // 普通GET
            String result = httpUtil.get("https://api.example.com/data");
            System.out.println(result);

            // 带参数的GET
            Map<String, String> params = new HashMap<>();
            params.put("page", "1");
            params.put("size", "10");
            String result2 = httpUtil.get("https://api.example.com/data", null, params);

            // 带Header的GET
            String[] headersArray = {"Authorization", "Bearer token123", "Accept", "application/json"};
            Map<String, String> headers = OkHttpUtil.arrayToHeaderMap(headersArray);
            String result3 = httpUtil.get("https://api.example.com/data", headers, params);

        } catch (IOException e) {
            e.printStackTrace();
        }

        // 3. 同步POST请求
        try {
            // JSON POST
            String json = "{\"name\":\"张三\",\"age\":25}";
            String result = httpUtil.postJson("https://api.example.com/user", json);

            // Form POST
            Map<String, String> formParams = new HashMap<>();
            formParams.put("username", "admin");
            formParams.put("password", "123456");
            String result2 = httpUtil.postForm("https://api.example.com/login", formParams);

        } catch (IOException e) {
            e.printStackTrace();
        }

        // 4. 异步请求
        httpUtil.getAsync("https://api.example.com/data", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.err.println("Request failed: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseData = response.body().string();
                    System.out.println("Async response: " + responseData);
                }
            }
        });

        // 5. 文件上传
        try {
            Map<String, String> formParams = new HashMap<>();
            formParams.put("description", "test file");

            Map<String, File> files = new HashMap<>();
            files.put("file", new File("/path/to/file.txt"));

            String result = httpUtil.uploadFile(
                    "https://api.example.com/upload",
                    null,
                    formParams,
                    files
            );

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}