1.示例代码在SDK中没找到,下面提供了具体实现
2.返回失败的原因,logisticID 以Sign开头
3.通过MAVEN打包JAR到私服,引用不到HTTP-CLIENT,maven引入commons-codec、fastjson和commons-httpclient包。
具体错误:
reason":“参数校验失败”,\n\t"resultCode":"2006
SIGN参数,
logisticID 渠道单号 String 32 是 由第三方接入商产生的订单号(生成规则为sign+数字,sign值由双方约定)
官方例子没找到SDK的方法
//时间戳 SDK提供SecurityUtil获取时间戳
String timestamp=SecurityUtil.getTimestamp();
//摘要 SDK提供SecurityUtil生成摘要
String digest=SecurityUtil.getDigest(params+appkey+timestamp);方法在SDK中没有找到
自己实现如下
long timestamp = System.currentTimeMillis();
String currentDateTime= DateUtils.getTimestamp(timestamp);
//摘要 SDK提供SecurityUtil生成摘要
String _params = params.replaceAll("@time", currentDateTime);
String digest = getDigest(_params + appkey + timestamp);
public static String getTimestamp(long timestamp) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
return DateUtils.dateToString(cal, "yyyy-MM-dd HH:mm:ss");
}
private static String getDigest(String plainText) {
return Base64.encodeBase64String(DigestUtils.md5Hex(plainText).getBytes());
}
long timestamp = System.currentTimeMillis();
String currentDateTime= DateUtils.getTimestamp(timestamp);
//摘要 SDK提供SecurityUtil生成摘要
String _params = params.replaceAll("@time", currentDateTime);
String digest = getDigest(_params + appkey + timestamp);
//post请求参数
NameValuePair[] data = new NameValuePair[4];
data[0] = new NameValuePair("companyCode", companyCode);
data[1] = new NameValuePair("digest", digest);
data[2] = new NameValuePair("timestamp", String.valueOf(timestamp));
data[3] = new NameValuePair("params", _params);
//请求url
String url=NEW_ORDER_URL;
try {
//返回结果
ResultDO<String> stringResultDO = HttpUtils.sendRequest(url, data, "UTF-8", TIMEOUT_5S);
return stringResultDO;
} catch (Exception e) {
e.printStackTrace();
}
static String params = "{\n" +
" \"companyCode\":\"EWBCJZHJSHWLWKJYXGS\",\n" +
" \"custOrderNo\":\"123456\",\n" +
" \"customerCode\":\"12345678\",\n" +
" \"logisticID\":\"ANLW33343111111113445\",\n" +
" \"needTraceInfo\":1,\n" +
" \"orderType\":\"1\",\n" +
" \"packageInfo\":{\n" +
" \"cargoName\":\"货物名\",\n" +
" \"deliveryType\":\"9\",\n" +
" \"totalNumber\":2,\n" +
" \"totalVolume\":0.01,\n" +
" \"totalWeight\":1,\n" +
" \"packageService\":\"纸\"\n" +
" },\n" +
" \"receiver\":{\n" +
" \"address\":\"详细地址\",\n" +
" \"city\":\"上海市\",\n" +
" \"companyName\":\"德邦\",\n" +
" \"county\":\"青浦区\",\n" +
" \"mobile\":\"180****2531\",\n" +
" \"name\":\"AAA\",\n" +
" \"province\":\"上海\",\n" +
" \"town\":\"徐泾镇\"\n" +
" },\n" +
"\n" +
" \"sender\":{\n" +
" \"address\":\"详细地址\",\n" +
" \"city\":\"上海市\",\n" +
" \"companyName\":\"德邦\",\n" +
" \"county\":\"青浦区\",\n" +
" \"mobile\":\"180****3451\",\n" +
" \"name\":\"AAA\",\n" +
" \"province\":\"上海\",\n" +
" \"town\":\"徐泾镇\"\n" +
" },\n" +
" \"transportType\":\"PACKAGE\",\n" +
" \"gmtCommit\":\"@time\",\n" +
" \"payType\":\"1\",\n" +
" \"isOut\":\"N\"\n" +
"}";