本教程详细介绍如何使用Alibaba Cloud SDK for Java查看账户情况。

前提条件

在使用本教程前,请确保已完成以下操作:

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见阿里云开发工具包(SDK)
    <dependencies>      <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core --&gt;      <dependency&gt;          <groupId>com.aliyun</groupId>          <artifactId>aliyun-java-sdk-core</artifactId>          <version>4.4.3</version>      </dependency>      <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-bssopenapi -->      <dependency>          <groupId>com.aliyun</groupId>          <artifactId>aliyun-java-sdk-bssopenapi</artifactId>          <version>1.4.2</version>      </dependency> </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.bssopenapi.model.v20171214.*; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.google.gson.Gson;  /**  * 查询账户余额信息  */ public class QueryAccountBalanceDemo {      /**      * 账户余额查询      */     private QueryAccountBalanceResponse queryAccountBalance(QueryAccountBalanceRequest request, IAcsClient client){         try {             // 调用SDK发送请求             return client.getAcsResponse(request);         } catch (ClientException e) {             e.printStackTrace();             // 发生调用错误,抛出运行时异常             throw new RuntimeException();         }     }      /**      * 代金券查询      */     private QueryCashCouponsResponse queryCashCoupons(QueryCashCouponsRequest request, IAcsClient client){         try {             // 调用SDK发送请求             return client.getAcsResponse(request);         } catch (ClientException e) {             e.printStackTrace();             // 发生调用错误,抛出运行时异常             throw new RuntimeException();         }     }      /**      * 储值卡查询      */     private QueryPrepaidCardsResponse queryPrepaidCards(QueryPrepaidCardsRequest request, IAcsClient client){         try {             // 调用SDK发送请求             return client.getAcsResponse(request);         } catch (ClientException e) {             e.printStackTrace();             // 发生调用错误,抛出运行时异常             throw new RuntimeException();         }     }      /**      * 提货券查询      */     private QueryRedeemResponse queryRedeem(QueryRedeemRequest request,IAcsClient client){         try {             // 调用SDK发送请求             return client.getAcsResponse(request);         } catch (ClientException e) {             e.printStackTrace();             // 发生调用错误,抛出运行时异常             throw new RuntimeException();         }     }      private static void log_print(String functionName, Object result) {         Gson gson = new Gson();         System.out.println("-------------------------------" + functionName + "-------------------------------");         System.out.println(gson.toJson(result));     }      public static void main(String[] args) {         // 设置鉴权参数,初始化客户端         DefaultProfile profile = DefaultProfile.getProfile(                 "cn-qingdao",// 地域ID                 "your-access-key-id",// 您的AccessKey ID                 "your-access-key-secret");// 您的AccessKey Secret         IAcsClient client = new DefaultAcsClient(profile);          QueryAccountBalanceDemo queryAccountBalanceDemo = new QueryAccountBalanceDemo();          // 查询账户余额         QueryAccountBalanceRequest queryAccountBalanceRequest = new QueryAccountBalanceRequest();         QueryAccountBalanceResponse queryAccountBalanceResponse = queryAccountBalanceDemo.queryAccountBalance(queryAccountBalanceRequest, client);         log_print("queryAccountBalance",queryAccountBalanceResponse);          // 查询代金券         QueryCashCouponsRequest queryCashCouponsRequest = new QueryCashCouponsRequest();         // 代金券是否生效         queryCashCouponsRequest.setEffectiveOrNot(true);         QueryCashCouponsResponse queryCashCouponsResponse = queryAccountBalanceDemo.queryCashCoupons(queryCashCouponsRequest, client);         log_print("queryCashCoupons",queryCashCouponsResponse);          // 查询储值卡         QueryPrepaidCardsRequest queryPrepaidCardsRequest = new QueryPrepaidCardsRequest();         // 代金券是否生效         queryPrepaidCardsRequest.setEffectiveOrNot(true);         QueryPrepaidCardsResponse queryPrepaidCardsResponse = queryAccountBalanceDemo.queryPrepaidCards(queryPrepaidCardsRequest, client);         log_print("queryPrepaidCards",queryPrepaidCardsResponse);          // 查询提货券         QueryRedeemRequest queryRedeemRequest = new QueryRedeemRequest();         // 代金券是否生效         queryRedeemRequest.setEffectiveOrNot(true);         QueryRedeemResponse queryRedeemResponse = queryAccountBalanceDemo.queryRedeem(queryRedeemRequest, client);         log_print("queryRedeem",queryRedeemResponse);     } }

运行结果

正确运行结果类似如下:

-------------------------------queryAccountBalance------------------------------- {     "requestId": "769B9459-26DE-48C1-8408-4546C27BE3CB",     "success": true,     "code": "200",     "message": "success",     "data": {         "availableAmount": "0.10",         "availableCashAmount": "0.10",         "creditAmount": "0.00",         "mybankCreditAmount": "0.00",         "currency": "CNY"     } } -------------------------------queryCashCoupons------------------------------- {     "requestId": "F4D34762-84DD-4556-94B2-248420B7A977",     "success": true,     "code": "Success",     "message": "Successful!",     "data": [         {             "cashCouponId": 55620452xxxx,             "cashCouponNo": "Q-3185d1b0xxxx",             "grantedTime": "2019-07-29T06:51:44Z",             "effectiveTime": "2019-07-29T06:51:44Z",             "expiryTime": "2020-01-25T06:51:43Z",             "applicableProducts": "通用",             "applicableScenarios": "通用",             "nominalValue": "100.00",             "balance": "100.00",             "status": "Available"         }     ] } -------------------------------queryPrepaidCards------------------------------- {     "requestId": "D9AF1639-D446-4661-AAA4-C88AFA67333E",     "success": true,     "code": "Success",     "message": "Successful!",     "data": {     "PrepaidCard": [       {         "Status": "Available",         "PrepaidCardNo": "Q-7edaab979fc9",         "ApplicableProducts": "通用",         "NominalValue": "100.00",         "ApplicableScenarios": "通用",         "PrepaidCardId": 161120020661,         "Balance": "100.00",         "ExpiryTime": "2019-08-04T01:39:11Z",         "EffectiveTime": "2018-08-03T01:39:11Z",         "GrantedTime": "2018-08-03T01:39:11Z"       }     ]   } } -------------------------------queryRedeem------------------------------- {     "requestId": "A36726F3-B74D-4D14-AB28-F31F87C3B751",     "success": true,     "code": "Success",     "message": "Successful!",     "data": {         "pageNum": 1,         "pageSize": 20,         "totalCount": 1,         "redeem": [             {                 "Status": "expired",                 "ApplicableProducts": "云服务器(包年包月国际站1)",                 "NominalValue": "0",                 "Specification": "",                 "Balance": "0",                 "RedeemId": "15",                 "ExpiryTime": "2019-08-04T01:39:11Z",                 "EffectiveTime": "2018-08-03T01:39:11Z",                 "RedeemNo": "4889*****1610",                 "GrantedTime": "2018-08-03T01:39:11Z"             }         ]     } }