okhttp get post 使用原始碼

hcmony發表於2017-10-20
package com.hcmony.utils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by hcmony on 2017/4/12.
 */
public class OkHttpUtils {

    // 建立一個 OkHttpClient
    private static final OkHttpClient mOkHttpClient = new OkHttpClient.Builder().connectTimeout(5,
                                                                                                TimeUnit.SECONDS).readTimeout(10,
                                                                                                                              TimeUnit.SECONDS).build();

    // 引數格式
    public static final MediaType     JSON          = MediaType.parse("application/json; charset=utf-8");

    /**
     * 方法描述:httpGet
     * 
     * @author hcmony 2017510日 下午3:59:07
     * @param url serverHost可以帶引數如http://www.baidu.com?param1=xxx&param2=xxx
     * @return
     * @throws IOException
     */
    public static String get(String url) throws IOException {
        Request request = new Request.Builder().url(url).get().build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpGet
     * 
     * @author hcmony 2017510日 下午3:53:50
     * @param url serverHost
     * @param params 引數:map格式
     * @return
     * @throws IOException
     */
    public static String get(String url, Map<String, Object> params) throws IOException {
        // 構建請求引數
        StringBuffer paramSb = new StringBuffer();
        if (params != null) {
            for (Entry<String, Object> e : params.entrySet()) {

                if (e.getValue() != null) {
                    paramSb.append(e.getKey().toString());
                    paramSb.append("=");
                    // 將引數值urlEncode編碼,防止傳遞中亂碼
                    paramSb.append(URLEncoder.encode(e.getValue().toString(), "UTF-8"));
                    paramSb.append("&");
                }
            }
        }
        if (paramSb.length() > 0) {
            String paramStr = paramSb.toString();
            paramStr = paramStr.substring(0, paramStr.length() - 1);
            url += "?" + paramStr;
        }
        Request request = new Request.Builder().url(url).get().build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpPost
     * 
     * @author hcmony 2017510日 下午3:53:28
     * @param url serverHost
     * @param params 引數:json格式
     * @return
     * @throws IOException
     */
    public static String post(String url, String params) throws IOException {
        RequestBody body = RequestBody.create(JSON, params);
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpPost
     * 
     * @author hcmony 2017510日 下午3:53:50
     * @param url serverHost
     * @param params 引數:map格式
     * @return
     * @throws IOException
     */
    public static String post(String url, Map<String, Object> params) throws IOException {

        FormBody.Builder build = new FormBody.Builder();
        for (String key : params.keySet()) {
            if (params.get(key) != null) {
                build.add(key, URLEncoder.encode(params.get(key).toString(), "UTF-8"));
            }
        }
        RequestBody body = build.build();
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpPut
     * 
     * @author hcmony 2017510日 下午3:53:50
     * @param url serverHost
     * @param params 引數:json格式
     * @return
     * @throws IOException
     */
    public static String put(String url, String params) throws IOException {
        RequestBody body = RequestBody.create(JSON, params);
        Request request = new Request.Builder().url(url).put(body).build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpPut
     * 
     * @author hcmony 2017510日 下午3:53:50
     * @param url serverHost
     * @param params 引數:map格式數
     * @return
     * @throws IOException
     */
    public static String put(String url, Map<String, Object> params) throws IOException {
        FormBody.Builder build = new FormBody.Builder();
        for (String key : params.keySet()) {
            if (params.get(key) != null) {
                build.add(key, URLEncoder.encode(params.get(key).toString(), "UTF-8"));
            }
        }
        RequestBody body = build.build();

        Request request = new Request.Builder().url(url).put(body).build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpDelete
     * 
     * @author hcmony 2017510日 下午3:53:50
     * @param url serverHost
     * @param params 引數:json格式
     * @return
     * @throws IOException
     */
    public static String delete(String url) throws IOException {
        Request request = new Request.Builder().url(url).delete().build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpPatch
     * 
     * @author hcmony 2017510日 下午3:57:23
     * @param url serverHost
     * @param params 引數:map格式
     * @return
     * @throws IOException
     */
    public static String patch(String url, Map<String, Object> params) throws IOException {
        FormBody.Builder build = new FormBody.Builder();
        for (String key : params.keySet()) {
            if (params.get(key) != null) {
                build.add(key, URLEncoder.encode(params.get(key).toString(), "UTF-8"));
            }
        }
        RequestBody body = build.build();
        Request request = new Request.Builder().url(url).patch(body).build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpPatch
     * 
     * @author hcmony 2017510日 下午3:57:23
     * @param url serverHost
     * @param params 引數:json格式
     * @return
     * @throws IOException
     */
    public static String patch(String url, String params) throws IOException {
        RequestBody body = RequestBody.create(JSON, params);
        Request request = new Request.Builder().url(url).patch(body).build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 方法描述:httpHead
     * 
     * @author hcmony 2017510日 下午3:58:28
     * @param url serverHost
     * @return
     * @throws IOException
     */

    public static String head(String url) throws IOException {
        Request request = new Request.Builder().url(url).head().build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

}

相關文章