前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >okhttp3请求笔记

okhttp3请求笔记

作者头像
py3study
发布2020-01-09 15:45:53
5670
发布2020-01-09 15:45:53
举报
文章被收录于专栏:python3
代码语言:javascript
复制
<dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.2.0</version>
        </dependency>
代码语言:javascript
复制
import okhttp3.*;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class HttpUtil {

    private static OkHttpClient client = null;

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

    private static void init(){
        client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .build();
    }

    /**
     *
     * MethodName : executeGet
     * @Description : get方式链接url获取返回String结果
     * @param url 请求链接
     * @return 返回结果字符串
     * @throws Exception
     * @author
     * @date: 2017-7-13 上午9:12:55
     */
    public static String executeGet(String url) throws Exception {
        if (url == null || "".equals(url.trim())) {
            throw new Exception("null url");
        }
        Request request = new Request.Builder().url(url).build();
        init();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * MethodName : executePost
     * @Description : post方式链接url获取返回String结果
     * @param url 请求路径
     * @param params 参数map
     * @return 返回结果字符串
     * @throws Exception
     * @author
     * @date: 2017-12-14 上午10:12:55
     */
    public static String executePost(String url, Map<String,String> params) throws Exception {
        if (url == null || "".equals(url.trim())) {
            throw new Exception("null url");
        }
        FormBody.Builder formBody = new FormBody.Builder();
        if (params != null && params.size() > 0 ){
            for (String key : params.keySet()) {
                formBody.add(key,params.get(key));
            }
        }
        Request requestPost = new Request.Builder()
                .url(url)
                .post(formBody.build())
                .build();
        init();
        Response response = client.newCall(requestPost).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * POST请求,参数为json
     * @param url
     * @param json
     * @return
     * @throws IOException
     */
    public static String postJson(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
//                .header("键", "值")
//                .header("键", "值")
                .post(body)
                .build();
        init();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    public static void main(String[] args) {
        try {
            String res =  postJson("http://",
                    "{\"groupNoList\":[\"g1766149\"]}");
            System.out.println(res);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档