OkHttpUtil工具类

OkhttpUtil

public class Okhttp3Utils {
private static MediaType JSON = MediaType.parse(“application/json; charset=utf-8”);

/**
 * post 请求 表单提交
 */
public static String sendPostForm(String url, Map<String, String> param, Map<String, String> headerMap) {
    OkHttpClient client = new OkHttpClient();
    FormBody.Builder builder = new FormBody.Builder();

    if (param != null){
        if (!param.isEmpty()){ //如果param里面没有数据返回true
            Iterator<Map.Entry<String, String>> it = param.entrySet().iterator();
            while (it.hasNext()){
                Map.Entry<String, String> next = it.next();
                builder.add(next.getKey(),next.getValue());
            }
        }
    }
    Request.Builder post = new Request.Builder()
            .url(url)
            .post(builder.build());
    if (headerMap != null){
        if (!headerMap.isEmpty()){
            Iterator<Map.Entry<String, String>> dd = headerMap.entrySet().iterator();
            while (dd.hasNext()){
                Map.Entry<String, String> next = dd.next();
                post.addHeader(next.getKey(),next.getValue());
            }
        }
    }

    //同步请求
    Call call = client.newCall(post.build());
    try {
        Response response = call.execute();
        return response.body().string();
    }catch (IOException e){
        return null;
    }

}
/**
 * post 发送json
 */
public static String sendPostJson(String url,String json, Map<String, String> headerMap) {
    OkHttpClient client = new OkHttpClient();

    RequestBody body=RequestBody.create(JSON,json);

    Request.Builder post = new Request.Builder()
            .url(url)
            .post(body);
    if (headerMap != null){
        if (!headerMap.isEmpty()){
            Iterator<Map.Entry<String, String>> dd = headerMap.entrySet().iterator();
            while (dd.hasNext()){
                Map.Entry<String, String> next = dd.next();
                post.addHeader(next.getKey(),next.getValue());
            }
        }
    }
    //同步请求
    Call call = client.newCall(post.build());
    try {
        Response response = call.execute();
        return response.body().string();
    }catch (IOException e){
        return null;
    }
}
/**
 * get 请求
 */
public static String sendGet(String url,Map<String,Object> param,Map<String,String> headerMap){
    OkHttpClient client = new OkHttpClient();

    StringBuffer stringBuffer = new StringBuffer();

    if (param != null){
        if (!param.isEmpty()){
            Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
            while (it.hasNext()){
                Map.Entry<String, Object> next = it.next();
                if (stringBuffer.toString().equals("")){
                    stringBuffer.append(next.getKey()+"="+next.getValue());
                }else {
                    stringBuffer.append("&"+next.getKey()+"="+next.getValue());
                }
            }
        }
    }
    if (!stringBuffer.equals("")){
        url = url+"?";
        url = url + stringBuffer.toString();
    }
    Request.Builder get = new Request.Builder()
            .url(url);
    if (headerMap != null){
        if (!headerMap.isEmpty()){
            Iterator<Map.Entry<String, String>> dd = headerMap.entrySet().iterator();
            while (dd.hasNext()){
                Map.Entry<String, String> next = dd.next();
                get.addHeader(next.getKey(),next.getValue());
            }
        }
    }
    //同步请求
    Call call = client.newCall(get.build());
    try {
        Response response = call.execute();
        return response.body().string();
    }catch (IOException e){
        return null;
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值