前言
昨天开始接手一个小任务,说让我处理一个接口的查询返回数据,最开始的时候,我发现自己很傻逼,还想着自己写一个关于提交的方式,就是自己把拿到的数据封装到head,body当中,做了一早上,我发现好像有点难,不知道怎么处理该问题,但是在最后的时候。我师父过来一看,来一句,okhttp啊 ,你在搞啥子哦。一瞬间,我发现自己好像没有在开发的这条路上,这么好的工具怎么不用呢。对不。下面我就开始介绍一下我昨天整理好的OKHTTP
一、什么是OKHTTP?
Http是现在应用常用的一种交换数据和媒体的网络方式,高效地使用http能让资源加载更快,节省带宽。Okhttp是一个高效的Http客户端,它的特性如下:
- 支持Http/2,允许所有同一个主机地址的请求共享同一个socket连接
- 连接池能够减少请求延时
- 透明的GZIP压缩能够减少响应数据的大小
- 缓存响应内容,避免一些完全重复的请求
当网络请求出现问题的时候okhttp依然坚守自己的职责,它会自动恢复一般的连接问题,如果你的服务有多个ip地址,当第一个ip请求失败时,Okhttp会交替尝试你配置的其他Ip,okhttp使用现代TLS技术初始化新的连接,当握手失败时会回退到TLS1.0
二、使用步骤
1.pom文件示例:
<!--OK HTTP Client-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
</dependency>
三.接着我们开始写OKHTTP程序的 post 请求提交JSON格式的数据。
new Thread() {
@Override
public void run() {
// @Headers({"Content-Type:application/json","Accept: application/json"})//需要添加头
MediaType JSON = MediaType.parse("application/json;charset=utf-8");
JSONObject json = new JSONObject();
String URl = "http://www.baidu.com/"
try {
json.put("EngineerId", getString(ShareFile.UID, ""));
json.put("EngorderNumber", orderNumber);
json.put("PID", pid);
json.put("RefundType", RefundType);
json.put("RefundReason", reason_text.getText().toString());
json.put("RefundImg", str);
json.put("RefundVideo", "");
json.put("discount", discount);
json.put("RefundState", "0");
json.put("shopkeeperId", shopkeeperId);
json.put("payMoney", ActualPayment);//tuikuanPriceString
json.put("refundNum", number);
json.put("RefundCategory", RefundCategory);
json.put("sidd", oldsidd + "");
if (RefundCategory.equals("2")) {
json.put("ExchangePID", pid);
json.put("ExchangeSidd", sidd + "");
} else {
json.put("ExchangePID", "");
json.put("ExchangeSidd", "");
}
} catch (JSONException e) {
e.printStackTrace();
}
//申明给服务端传递一个json串
//创建一个OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//创建一个RequestBody(参数1:数据类型 参数2传递的json串)
//json为String类型的json数据
RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
//创建一个请求对象
// String format = String.format(KeyPath.Path.head + KeyPath.Path.waybillinfosensor, username, key, current_timestamp);
Request request = new Request.Builder()
.url(URL)
.post(requestBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//DialogUtils.showPopMsgInHandleThread(Release_Fragment.this.getContext(), mHandler, "数据获取失败,请重新尝试!");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.i("info",string+"");
try {
JSONObject json = new JSONObject(string);
strddd = json.optString("msg");
handler.sendEmptyMessage(0x11);
if (json.optString("flag").equals("00")) {
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}.start();
总结
今天就简单写到这里,后面我会继续把这个封装成一个 工具类来使用,后面在持续更新该文章内容。