OkHttp简单封装

OkHttp简单封装

public class OkHttpUtil {

    public static final String CHARSET = "UTF_8";
    private static OkHttpClient okHttpClient = null;

    private OkHttpUtil() {
    }

    private static OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            synchronized (OkHttpUtil.class) {
                if (okHttpClient == null) {
                    okHttpClient = new OkHttpClient.Builder()//.addInterceptor()
                            .connectTimeout(10 * 1000, TimeUnit.MILLISECONDS).readTimeout(10 * 1000, TimeUnit.MILLISECONDS)
                            .writeTimeout(10 * 1000, TimeUnit.MILLISECONDS)//.cookieJar()
                            .build();
                }
            }
        }
        return okHttpClient;
    }

    private static String encodeParams(Map<String, String> params, String charser, boolean urlEncode) {
        StringBuilder stringBuilder = new StringBuilder();
        if (params != null && !params.isEmpty()) {
            int i = 0;
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (!TextUtils.isEmpty(value) && !TextUtils.isEmpty(key)) {
                    if (urlEncode) {
                        try {
                            key = URLEncoder.encode(key, charser);
                            value = URLEncoder.encode(value, charser);
                            stringBuilder.append(key)
                                    .append("=")
                                    .append(value);
                            if (i != params.size() - 1) {
                                stringBuilder.append("&");
                            }
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
                i++;
            }
        }
        return stringBuilder.toString();
    }

    private static String encodeParams(Map<String, String> params) {
        return encodeParams(params, CHARSET, true);
    }

    private static Response execute(Request request) throws IOException {
        Request request1 = request.newBuilder().addHeader("User-Agent", "Android").build();
        return getOkHttpClient().newCall(request1).execute();
    }

    public static Response doGet(String url, Map<String, String> params) throws IOException {
        if (url == null) {
            throw new IllegalArgumentException("url can't be null");
        }
        int index = url.lastIndexOf("?");
        if (index == -1) {
            url = url + "?";
        } else {
            url = url + "&";
        }
        String encodeParams = encodeParams(params);
        Request request = new Request.Builder().url(url + encodeParams).get().build();
        return execute(request);
    }

    public static Response doPost(String url, RequestBody body) throws IOException {
        if (url == null) {
            throw new IllegalArgumentException("url can't be null");
        }
        Request request = new Request.Builder().url(url).post(body).build();
        return execute(request);
    }

    public static Response doPost(String url, Map<String, String> params) throws IOException {
        FormBody.Builder formBody = new FormBody.Builder();
        if (params != null) {
            for (String key : params.keySet()) {
                String value = params.get(key);
                if (!TextUtils.isEmpty(value)) {
                    formBody.add(key, value);
                }
            }
        }
        return doPost(url, formBody.build());
    }

    public static Response doPost(String url, String contentType, File file) throws IOException {
        if (file == null) {
            throw new IllegalArgumentException("file can't be null");
        }
        String fileName = file.getName();
        fileName = fileName.substring(0, fileName.lastIndexOf("."));
        MediaType mediaType = MediaType.parse(contentType);
        RequestBody body = RequestBody.create(mediaType, file);
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", fileName, body);
        return doPost(url, builder.build());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值