文档首页 > > 接入指南> API类商品接入指南> 计量数据推送请求和响应处理(代码示例)

计量数据推送请求和响应处理(代码示例)

分享
更新时间:2020/03/28 GMT+08:00

Java代码示例:

String serviceName = "";
        String region = "";
        String ak = "";
        String sk = "";
        String partnerId = "042ffe758bba44ab9dd73f7c40c0d66b";
        String mktUrl = "https://mkt.myhuaweicloud.com/v1.0/"+partnerId+"/billing/bill-mgr/push-usage-data";
        
        Map<String,Object> usageRecords = new HashMap<String, Object>();

        // 封装第一条业务计量数据(根据实际情况,将此段写在循环中处理)
        Map<String, Object> usageEntity = new HashMap<String, Object>();
        usageEntity.put("Duration", "3524");
        Map<String, Object> usageRecordData1 = new HashMap<String, Object>();
        usageRecordData1.put("instanceId", "455be31e-6771-4d6f-b8c1-7e8bdb10c21f");
        usageRecordData1.put("recordTime", "20180117T151020Z");
        usageRecordData1.put("beginTime", "20180117T150000Z");
        usageRecordData1.put("endTime", "20180117T160000Z");
        usageRecordData1.put("usageEntity", usageEntity);
        List<Map<String, Object>> usageRecordDataList = new ArrayList<Map<String, Object>>();
        usageRecordDataList.add(usageRecordData1);
        usageRecords.put("usageRecords", usageRecordDataList);

        // 封装第二条业务计量数据,参照封装第一条业务计量数据的代码示例
        // …
        
        // 将请求数据转成json字符串格式,并且调用接口发送到华为云云市场
        String requestMsgBody = JSON.toJSONString(usageRecords);
        System.out.println(requestMsgBody);

            // 构造并发送请求
        AccessService accessService = new AccessServiceImpl(serviceName, region, ak, sk);
        URL url = null;
        HttpResponse response = null;
        String responseMsgBody = "";
        try
        {
            url = new URL(mktUrl);
            HttpMethodName httpMethod = HttpMethodName.POST;

            InputStream content = new ByteArrayInputStream(requestMsgBody.getBytes());
            response = accessService.access(url, content,
              (long) requestMsgBody.getBytes().length, httpMethod);
            if(null == response)
            {
               //TODO:响应异常处理
                return;
            }
            HttpEntity entity = response.getEntity();
            
            if (null == entity)
            {
                //TODO:响应报文读取异常处理
                return;
            }
            responseMsgBody = EntityUtils.toString(entity);
        }
        catch (MalformedURLException e)
        {
            // TODO URL异常处理
            return;
        }
        catch (Exception e)
        {
            // TODO 其他异常处理
            return;
        }
        
        int responseStatus = response.getStatusLine().getStatusCode();
        JSONObject rspJsonObject = JSON.parseObject(responseMsgBody);
        String errorCode = null;
        String errorMsg = null;
        if(null != rspJsonObject){
            if(rspJsonObject.containsKey("error_code")){
                errorCode = rspJsonObject.getString("error_code");
            }
            if(rspJsonObject.containsKey("error_msg")){
                errorMsg = rspJsonObject.getString("error_msg");
            }
        }else{
            //TODO:报文解析异常处理
            return;
        }
        if(null == errorCode || null == errorMsg){
            //TODO:参数异常处理
            return;
        }
        if(200 == responseStatus)
        {
            //TODO:业务正常处理
            
        }else{
            //TODO:根据error_code处理异常
            return;
        }
  • partnerId即账号ID。
  • region取空""或者"all",serviceName取CBCOpenAPI。
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!非常感谢您的反馈,我们会继续努力做到更好!
反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问