快递100接口java对接
1.先引入快递100maven依赖
<dependency>
<groupId>com.github.kuaidi100-api</groupId>
<artifactId>sdk</artifactId>
<version>1.0.2</version>
</dependency>
2.工具类
package com.es.biz.common.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.es.common.exception.RRException;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* 快递100工具类
*/
@Component()
public class KuaiDi100Utils {
private static Logger logger = LoggerFactory.getLogger(KuaiDi100Utils.class);
private static final String KEY = "HRP*******660";
private static final String CUSTOMER = "FE61**************F26F1";
private static final String SECRET_KEY = "YD*******3Tc";
private static final String SECRET_SECRET = "329**************5f9";
private static final String SECRET_CODE = "535**************3e";
private static final String RESULTV2 = "1";
/**
* 快递100智能识别快递编号
*/
public static String findExpressCode(String expressNo) {
Map<String, String> params = new HashMap<>(4);
params.put("secret_key", SECRET_KEY);
params.put("secret_code", SECRET_CODE);
params.put("secret_sign", SignUtils.cloudSign(SECRET_KEY, SECRET_SECRET));
params.put("num", expressNo);
StringBuilder response = new StringBuilder("");
BufferedReader reader = null;
try {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> param : params.entrySet()) {
if (builder.length() > 0) {
builder.append('&');
}
builder.append(URLEncoder.encode(param.getKey(), "UTF-8"));
builder.append('=');
builder.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] bytes = builder.toString().getBytes(StandardCharsets.UTF_8);
URL url = new URL("http://cloud.kuaidi100.com/api");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
conn.setRequestMethod("POST");
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(bytes);
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response.toString();
}
/**
* 快递100 物流查询
*
* @param com 快递公司编码
* @param num 快递单号
* @return SFReturnData
*/
public static String expressInfo(String num, String com) {
try {
QueryTrackReq queryTrackReq = new QueryTrackReq();
QueryTrackParam queryTrackParam = new QueryTrackParam();
queryTrackParam.setCom(com);
queryTrackParam.setNum(num);
queryTrackParam.setResultv2(RESULTV2);
String param = new Gson().toJson(queryTrackParam);
queryTrackReq.setParam(param);
queryTrackReq.setCustomer(CUSTOMER);
queryTrackReq.setSign(SignUtils.querySign(param, KEY, CUSTOMER));
IBaseClient baseClient = new QueryTrack();
logger.error(baseClient.execute(queryTrackReq).getBody());
return baseClient.execute(queryTrackReq).getBody();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String findOrder(String expressNo) {
try {
//获取快递编码
String result = findExpressCode(expressNo);
JSONArray jsonArray = JSONArray.parseArray(result);
JSONObject jsonObject = jsonArray.getJSONObject(0);
boolean returnCode = jsonObject.containsKey("returnCode");
if (returnCode) {
throw new RRException("无法识别该快递单号");
}
String comCode = jsonObject.getString("comCode");
return KuaiDi100Utils.expressInfo(expressNo, comCode);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
String order = findOrder("");
System.out.println(order);
}
}
3.返回示例
{
"message":"ok",
"nu":"773097*****999",
"ischeck":"1",
"com":"shentong",
"status":"200",
"data":[
{
"time":"2021-05-19 14:37:53",
"context":"已签收,签收人是【本人签收】,如有疑问请联系:1580*****97,风里来,雨里去,汗也撒泪也流,申通小哥一刻不停留。不求服务惊天下,但求好评动我心,给个好评呗!!",
"ftime":"2021-05-19 14:37:53",
"areaCode":null,
"areaName":null,
"status":"签收"
},
{
"time":"2021-05-19 09:34:27",
"context":"【浙江杭州九堡十二部】的派件员【徐飞】正在为您派件,如有疑问请联系派件员,联系电话【158*****097】",
"ftime":"2021-05-19 09:34:27",
"areaCode":"CN330104105000",
"areaName":"浙江,杭州市,江干区,九堡镇",
"status":"派件"
},
{
"time":"2021-05-19 09:26:40",
"context":"快件已到达【浙江杭州九堡十二部】扫描员是【徐飞】",
"ftime":"2021-05-19 09:26:40",
"areaCode":"CN330104105000",
"areaName":"浙江,杭州市,江干区,九堡镇",
"status":"在途"
},
{
"time":"2021-05-19 03:58:35",
"context":"快件由【浙江杭州彭埠公司】发往【浙江杭州九堡十二部】",
"ftime":"2021-05-19 03:58:35",
"areaCode":"CN330104102000",
"areaName":"浙江,杭州市,江干区,彭埠镇",
"status":"在途"
},
{
"time":"2021-05-19 03:55:16",
"context":"快件已到达【浙江杭州彭埠公司】扫描员是【到件快扫机】",
"ftime":"2021-05-19 03:55:16",
"areaCode":"CN330104102000",
"areaName":"浙江,杭州市,江干区,彭埠镇",
"status":"在途"
},
{
"time":"2021-05-18 19:57:43",
"context":"快件由【浙江杭州转运中心】发往【浙江杭州彭埠公司】",
"ftime":"2021-05-18 19:57:43",
"areaCode":"CN330100000000",
"areaName":"浙江,杭州市",
"status":"在途"
},
{
"time":"2021-05-18 19:51:13",
"context":"快件已到达【浙江杭州转运中心】扫描员是【交叉带1】",
"ftime":"2021-05-18 19:51:13",
"areaCode":"CN330100000000",
"areaName":"浙江,杭州市",
"status":"在途"
},
{
"time":"2021-05-18 01:17:48",
"context":"快件由【福建厦门转运中心】发往【浙江杭州转运中心】",
"ftime":"2021-05-18 01:17:48",
"areaCode":"CN350200000000",
"areaName":"福建,厦门市",
"status":"在途"
},
{
"time":"2021-05-18 01:05:47",
"context":"快件已到达【福建厦门转运中心】扫描员是【龙庆红】",
"ftime":"2021-05-18 01:05:47",
"areaCode":"CN350200000000",
"areaName":"福建,厦门市",
"status":"在途"
},
{
"time":"2021-05-17 21:19:28",
"context":"【福建厦门江头二部】的收件员【龙怀涛】已收件",
"ftime":"2021-05-17 21:19:28",
"areaCode":"CN350206004000",
"areaName":"福建,厦门市,湖里区,江头",
"status":"揽收"
}
],
"state":"3",
"condition":"F00"
}