顺丰丰桥接口之下订单接口,适用于第三方系统对接
语言 JAVA
作者:不停留的风
1、加Servlet访问
@WebServlet("/XXX")
public class TestCallExpressNewAPIService extends HttpServlet{
//丰桥新沙箱测试顾客编码 Yg4Zf06w_sxZs3A5D
//校验码 3Xdk1jqeG1Xod9nUXus8Op7DNOkchTnw
private static final String CLIENT_CODE = "XX"; //此处替换为您在丰桥平台获取的顾客编码
private static final String CHECK_WORD = "XXXXXXXXXXXX";//此处替换为您在丰桥平台获取的校验码
//沙箱环境的地址
private static final String CALL_URL_BOX = "https://sfapi-sbox.sf-express.com/std/service";
//生产环境的地址
private static final String CALL_URL_PROD = "https://sfapi.sf-express.com/std/service";
2、循环获取数据库的数据
Fy_OrderDao fod=new Fy_OrderDao();
List<FYOrder> a=fod.findAll();
for(int i=0;a.size()>i;i++){
//商品信息 cargoDetails
String adgroup_name=a.get(i).getadgroup_name(); //商品名称
String total_price=a.get(i).gettotal_price(); //总价
double quantity=1.0;//数量
String unit1="个"; //单位
........
3、根据丰桥指定的格式进行数据传输
//第三层
//cargoDetails
JSONObject obj1 = new JSONObject();
obj1.put("amount", total_price);
obj1.put("count", quantity);
obj1.put("name", adgroup_name);
obj1.put("unit", unit1);
obj1.put("volume", volume1);
obj1.put("weight", weight1);
//COD代收货款
String value1=a.get(i).gettotal_price();
JSONObject obj4 = new JSONObject();
obj4.put("name", "COD");
obj4.put("value", value1);
//contactInfoList
//寄件方地址
JSONObject obj2 = new JSONObject();
obj2.put("address", address1);
obj2.put("city", city1);
obj2.put("company", company1);
obj2.put("contact", contact1);
obj2.put("contactType", contactType1);
obj2.put("county", county1);
obj2.put("mobile", mobile1);
obj2.put("province", province1);
//收件方地址
JSONObject obj3 = new JSONObject();
obj3.put("address", address2);
obj3.put("city", city2);
obj3.put("contact", contact2);
obj3.put("county", county2);
obj3.put("contactType", contactType2);
obj3.put("mobile", mobile2);
obj3.put("province", province2);
//第二层
//cargoDetails
JSONArray js1=new JSONArray();
js1.add(0, obj1);
//contactInfoList
JSONArray js2=new JSONArray();
js2.add(0, obj2);
js2.add(1, obj3);
//COD
JSONArray js3=new JSONArray();
js3.add(0,obj4);
//第一层
JSONObject obj = new JSONObject();
obj.put("cargoDetails", js1);
obj.put("contactInfoList", js2);
obj.put("expressTypeId", expressTypeId1);
obj.put("isOneselfPickup", isOneselfPickup1);
obj.put("language", language1);
obj.put("monthlyCard", monthlyCard1);
obj.put("orderId", orderId1);
obj.put("parcelQty", parcelQty1);
obj.put("payMethod", payMethod1);
obj.put("totalWeight", totalWeight1);
String spare7=a.get(i).getspare7();
String sss="货到付款";
System.out.print(spare7+"--"+sss);
if(spare7.equals(sss)){
obj.put("COD", js3);
}else{
System.out.print("");
}
//整合
String rs;
rs = new Gson().toJson(obj);
4、调用接口,获取返回的订单数据
EspServiceCode testService = EspServiceCode.EXP_RECE_CREATE_ORDER; //下订单
// EspServiceCode testService = EspServiceCode.EXP_RECE_SEARCH_ORDER_RESP; //查订单
// EspServiceCode testService = EspServiceCode.EXP_RECE_UPDATE_ORDER;//订单取消
// EspServiceCode testService = EspServiceCode.EXP_RECE_FILTER_ORDER_BSP;//订单筛选
// EspServiceCode testService = EspServiceCode.EXP_RECE_SEARCH_ROUTES;//查路由
// EspServiceCode testService = EspServiceCode.EXP_RECE_GET_SUB_MAILNO;//子单号
// EspServiceCode testService = EspServiceCode.EXP_RECE_QUERY_SFWAYBILL;//查运费
CallExpressServiceTools client=CallExpressServiceTools.getInstance();
// set common header
Map<String, String> params = new HashMap<String, String>();
String timeStamp = String.valueOf(System.currentTimeMillis());
String msgData =new Gson().toJson(obj);
params.put("partnerID", CLIENT_CODE); // 顾客编码 ,对应丰桥上获取的clientCode
params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
params.put("serviceCode",testService.getCode());// 接口服务码
params.put("timestamp", timeStamp);
params.put("msgData",msgData);
params.put("msgDigest", client.getMsgDigest(msgData,timeStamp,CHECK_WORD));
// System.out.println(params.get("requestID"));
long startTime = System.currentTimeMillis();
System.out.println("====调用请求:" + params.get("msgData"));
String result = HttpClientUtil.post(CALL_URL_PROD, params);
System.out.println("====调用丰桥的接口服务代码:" + String.valueOf(testService.getCode()) + " 接口耗时:"+ String.valueOf(System.currentTimeMillis()-startTime)+"====");
System.out.println("===调用地址 ==="+CALL_URL_PROD);
System.out.println("===顾客编码 ==="+CLIENT_CODE);
System.out.println("===返回结果:" +result);
5、对返回的接口进行解析存储
JSONObject job=(JSONObject) JSONObject.fromObject(result);
String aa=(String) job.get("apiResultData");
String aa2=aa.substring(1,aa.length()-1);
// System.out.print("测试2"+aa2);
JSONObject job2=(JSONObject) JSONObject.fromObject(aa2);
JSONObject job4=(JSONObject) JSONObject.fromObject(job2.get("msgData"));
JSONArray jay=new JSONArray().fromObject(job4.get("routeLabelInfo"));
JSONObject sonObject = jay.getJSONObject(0);
JSONObject dataObject = sonObject.getJSONObject("routeLabelData");
String waybillNo = dataObject.getString("waybillNo");
String sourceTransferCode=dataObject.getString("sourceTransferCode");
String sourceCityCode=dataObject.getString("sourceCityCode");
String sourceDeptCode=dataObject.getString("sourceDeptCode");
String sourceTeamCode=dataObject.getString("sourceTeamCode");
String destCityCode=dataObject.getString("destCityCode");
String destDeptCode=dataObject.getString("destDeptCode");
String destTeamCode=dataObject.getString("destTeamCode");
String destTransferCode=dataObject.getString("destTransferCode");
String destRouteLabel=dataObject.getString("destRouteLabel");
String proName=dataObject.getString("proName");
String cargoTypeCode=dataObject.getString("cargoTypeCode");
.......
需要自动打印面单的话,加个面单打印接口,然后下完单之后自动调转
需要自动跳转的话,记得加个时间间隔代码哟,不然跳转太快,面单打印会出问题。
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //设置暂停的时间 3 秒
面单打印接口,后续我也会写上
完整代码下载:
https://download.csdn.net/download/weixin_48529185/12845771