okhttp工具类 maven导入即可 用的好可以点个star 谢谢

1.调用顺序为

1.Ok.builder
2.url||param||header||urls
3.get||post
4.sync||async

2.具体功能

1.支持自定义预期响应格式,预期为json且与预期json格式状态不符合(code,status)默认重试2次
2.info存在一个文件warn,error存到一个文件,自动压缩。符合预期记录到info,不符合记录在error中
3.验证json是否有数据(http是否响应了,以及响应的data字段时候有数据),如果存在,快捷获取。
4.在jdk1.8,springboot2环境中测试过。 (并不依赖springboot)
https://github.com/kukuze/Ok

当然,我可以帮你封装一个简单的OkHttp工具类。以下是一个示例代码,你可以根据自己的需求进行修改和扩展: ```java import okhttp3.*; import java.io.IOException; import java.util.Map; public class OkHttpUtils { private static OkHttpClient client; private static OkHttpClient getClient() { if (client == null) { client = new OkHttpClient(); } return client; } public static String get(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); try (Response response = getClient().newCall(request).execute()) { if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); } } } public static String post(String url, Map<String, String> params) throws IOException { FormBody.Builder formBodyBuilder = new FormBody.Builder(); for (Map.Entry<String, String> entry : params.entrySet()) { formBodyBuilder.add(entry.getKey(), entry.getValue()); } RequestBody requestBody = formBodyBuilder.build(); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); try (Response response = getClient().newCall(request).execute()) { if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); } } } } ``` 这个工具类封装了get和post方法,可以发送HTTP请求并返回响应结果。你可以使用`OkHttpUtils.get(url)`发送GET请求,或者使用`OkHttpUtils.post(url, params)`发送POST请求。其中,url是请求的URL,params是POST请求的参数。 请注意,这只是一个简单的封装示例,你可以根据自己的项目需求进行扩展和优化。例如,可以添加超时设置、请求头配置等功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值