Java OkHttp使用

记录一下okhttp的使用

1. 自定义工具类 (GET,POST请求)

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.4.1</version>
</dependency>
/**
 * httpUtil工具类
 */
@Slf4j
public class HttpUtil {
	// 公共的OkHttpClient 对象
    public static final OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(10, TimeUnit.SECONDS)
        .readTimeout(10, TimeUnit.SECONDS)
        .build();

    /**
     * get 
     * @param url
     */
    public static ResponseBody sendOkHttpGet(String url){
        ResponseBody responseBody = null;
        try {
            Request request = new Request.Builder().url(url).get().build();
            responseBody = client.newCall(request).execute().body();
        } catch (IOException e) {
            log.error("[send request error]", e);
        }
        return responseBody;
    }

    /**
     * post
     * @param url
     * @param body	请求参数
     * @return
     */
    public static ResponseBody sendOkHttpPost(String url, RequestBody body){
        ResponseBody responseBody = null;
        try {
            Request request = new Request.Builder().url(url).post(body).build();
            responseBody = client.newCall(request).execute().body();
        } catch (IOException e) {
            log.error("[send request error]", e);
        }
        return responseBody;
    }
}

2. RequestBody

okhttp的post请求可以传递参数,使用FormBody构建

 //把请求参数封装到RequestBody里面
 // 1
FormBody.BuilderformBuilder = new FormBody.Builder();
formBuilder.add("username","admin");
formBuilder.add("password","admin");
RequestBody requestBody = formBuilder.build();
HttpUtil.sendOkHttpPost("请求url", requestBody);

// 2
FormBody.Builder builder = new FormBody.Builder();
       RequestBody body = builder
               .add("appKey", "G2NLz26de1g3456")
               .add("day", "2023-10-14")
               .build();

JSONObject 构建

JSONObject json = new JSONObject();
json.put("appKey", "G2NLz26de1g3456");
json.put("day", "2023-10-14");
RequestBody body = RequestBody.create(String.valueOf(json), MediaType.parse("application/json"));

3. ResponseBody

    public static void responseBody(String url, String path){
        try {
            Request request = new Request.Builder().url(url).get().build();
            // 请求返回ResponseBody 对象
            ResponseBody body = client.newCall(request).execute().body();
            // 返回字节
            byte[] bytes = body.bytes();
            // 返回字符串
            String string = body.string();
           	// 返回输入流
            InputStream inputStream = body.byteStream();
        }catch (Exception e){
            log.error("[error: {}]", e.getMessage());
        }
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洋哥登陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值