净谦

导航

11111

4.1.1 获取Ticket

1
2
3
4
5
String url = "***";
 
String postString = "{ \"appKey\": \"***\", \"appSecurity\": \"***\", \"accessType\": \"2\"}";
 
String result = PostData (url, postString);

 

POST JSON示例:

1
{"appKey": "***","appSecurity": "***","accessType": "2"}

 

返回JSON示例:

1
2
3
4
5
6
7
8
9
10
11
{
    "status": {
        "success"true,
        "errorCode"null,
        "message"null
    },
    "data": {
        "expireTime": 7200,
        "accessToken""6b30a440a9c426e27de13f63f4170fc0"
    }
}
 

4.1.2 查询汽车票结算明细

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        String url = "***";  // 请求链接
        String sign = "9488373060c8483a3ef6333353fdc7fe"// sign
        OkHttpClient okHttpClient = new OkHttpClient(); // OkHttpClient对象
        JSONObject jsonObject = new JSONObject();
        JSONObject jsObject = new JSONObject();
        jsObject.put("appKey""******");
        jsObject.put("accessToken""*****"); // token
        jsonObject.put("authInfo", jsObject);
        jsonObject.put("apiName""QueryBusOrderSettlementService");
        JSONObject contentJson = new JSONObject();
        jsonObject.put("requestContent", contentJson);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , jsonObject.toJSONString());
        Request request = new Request.Builder().url(url).addHeader("sign", sign).post(requestBody).build(); // 请求
        okHttpClient.newCall(request).enqueue(new Callback() {// 回调
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());//成功后的回调
            }
            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage());//失败后的回调
            }
        });
 
 

POST JSON示例:

1
2
3
4
5
6
7
8
9
10
11
{
    "authInfo":{
        "appKey":"******",
        "accessToken":"******"
    },
    "apiName":"QueryBusOrderSettlementService",
    "requestContent":{
       "orderID":null,"recordIDList":[],"accountID":null,"payType":"","startTime":"2019-09-04","endTime":"2019-09-30","isCompensation":true,"batchNo":"","pageIndex":1,"pageSize":null
    }
     
}
 

 返回JSON示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
    "data": {
        "count"22,
        "busOrderAccountSettlementList": [
            {
                "accountID"21831,
                "busSettlementOrderAndDetailEntity": [
                    {
                        "busTicketSettlementEntity": {
                            "recordID"86,
                            "orderID"3072441711,
                            "passengerName""胡明明",
                            "trip""上海浦东机场->苏州",
                            "ticketFee"90.0,
                            "serviceFee"10.1,
                            "apiServiceFee"0.0,
                            "refundTicketFee"0.0,
                            "realAmount"100.1,
                            "accountID"21831,
                            "accCheckID": -1,
                            "subAccCheckID": -1
                        }
                          
                ]
            }
        ]
    },
    "status": {
        "errorCode"0,
        "message""返回成功",
        "success"true
    }
}

4.1.3 生成sign示例代码

1
2
3
4
5
6
7
  {"authInfo":{"appKey":"obk_shanglv_001","accessToken":"bb95b4e23655dac8cd8e8eee57a0595e"},"apiName":"QueryBusOrderSettlementService","requestContent":{"orderID":null,"recordIDList":[],"accountID":null,"payType":"","startTime":"2019-09-04","endTime":"2019-09-30","isCompensation":true,"batchNo":"","pageIndex":1,"pageSize":null}}
   String accessToken = "5d2d9c8307195796400005a";
   HmacSHA256 hmac = new HmacSHA256();
   byte[] signBytes = hmac.digest(body, accessToken, Charset.forName(CommonConsts.UTF_8));
   String selfSign = Encoding.hexEncoding(signBytes);
   System.out.println(selfSign);

 

签名工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public byte[] digest(String msg, String encryptKey, Charset charset)
           throws NoSuchAlgorithmException, InvalidKeyException {
       byte[] key = encryptKey.getBytes(charset);
       // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
       SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
       // 生成一个指定 Mac 算法 的 Mac 对象
       Mac mac = Mac.getInstance("HmacSHA256");
       // 用给定密钥初始化 Mac 对象
       mac.init(secretKey);
       byte[] text = msg.getBytes(charset);
       // 完成 Mac 操作
       return mac.doFinal(text);
   }
 
   /**
    * <p>
    * 16进制编码摘要串
    * </p>
    *
    * @param bytes
    *            未编码的摘要串
    * @return 摘要 @
    */
   public static String hexEncoding(byte[] bytes) {
       // 使用16进制编码对摘要串进行编码
       if (bytes == null) {
           throw new IllegalArgumentException("Argument b ( byte array ) is null! ");
       }     
       StringBuffer hs = new StringBuffer();
       for (int i = 0; i < bytes.length; i++){
           String tmp = Integer.toHexString(0xff & bytes[i]);
           if (tmp.length() < 2) {
               hs.append("0");          
           }
           hs.append(tmp);
       }
       return hs.toString().toUpperCase();
   }

posted on 2020-04-14 15:53  康安V  阅读(72)  评论(0)    收藏  举报