中国站
帮助中心 > 网络与加速 > > > Java SDK使用说明

Java SDK使用说明

本文介绍Java SDK使用说明。请您按照以下步骤进行操作。

操作步骤

1、引入合作伙伴核心SDK

请在pom.xml文件中增加视频直播依赖,准确的SDK版本号,参见开发工具包(SDK)

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. <version>4.4.6</version>
  6. </dependency>
  7. </dependencies>

2、引入合作伙伴视频直播SDK

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-live</artifactId>
  5. <version>3.8.0</version>
  6. </dependency>
  7. </dependencies>

3、初始化Client

SDK通过IAcsClient的instance来完成openAPI的调用。因此,在您发起调用前,请先初始化IAcsClient实例。示例代码如下:

  1. public void init() throws ClientException {
  2. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "<your accessKey>", "<your accessSecret>");
  3. //DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "live", "live.aliyuncs.com"); //添加自定义endpoint。
  4. client = new DefaultAcsClient(profile);
  5. //System.setProperty("http.proxyHost", "127.0.0.1"); //此设置用于设置代理,可用fiddler拦截查看http请求,便于调试。
  6. //System.setProperty("http.proxyPort", "8888");
  7. }

4、初始化请求
发起API调用前,先初始化对应的请求的request实例,DescribeLiveSnapshotConfig(查询截图配置)接口为例,示例代码如下:

  1. public void requestInitSample() {
  2. DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
  3. describeLiveSnapshotConfigRequest.setDomainName("live.aliyunlive.com");
  4. //describeLiveSnapshotConfigRequest.setProtocol(ProtocolType.HTTPS); //指定访问协议
  5. //describeLiveSnapshotConfigRequest.setAcceptFormat(FormatType.JSON); //指定api返回格式
  6. //describeLiveSnapshotConfigRequest.setMethod(MethodType.POST); //指定请求方法
  7. //describeLiveSnapshotConfigRequest.setRegionId("cn-shanghai");//指定要访问的Region,仅对当前请求生效,不改变client的默认设置。
  8. try {
  9. HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest);
  10. System.out.println(httpResponse.getUrl());
  11. System.out.println(new String(httpResponse.getContent()));
  12. //todo something.
  13. } catch (ServerException e) {
  14. e.printStackTrace();
  15. } catch (ClientException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }

5、发起openAPI调用并解析结果
利用上一步初始化好的IAcsClient,您可以发起openAPI调用。IAcsClient提供了两种类型的调用结果返回,一种方式是通过调用doAction方法获取取得原始的API调用结果,即返回HttpResponse类型的结果。示例代码如下:

  1. public void invokeSample() {
  2. DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
  3. describeLiveSnapshotConfigRequest.setDomainName("live.aliyunlive.com");
  4. try {
  5. HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest);
  6. System.out.println(httpResponse.getUrl());
  7. System.out.println(new String(httpResponse.getContent()));
  8. //todo something else.
  9. } catch (ServerException e) {
  10. e.printStackTrace();
  11. } catch (ClientException e) {
  12. e.printStackTrace();
  13. }
  14. }

另一种方式,通过调用getAcsResponse方法,获取反序列化后的对象,示例代码如下:

  1. public void invokeSample() {
  2. DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
  3. describeLiveSnapshotConfigRequest.setDomainName("live.aliyunlive.com");
  4. try {
  5. DescribeLiveSnapshotConfigResponse describeLiveSnapshotConfigResponse = client.getAcsResponse(describeLiveSnapshotConfigRequest);
  6. //todo something.
  7. } catch (ServerException e) {
  8. e.printStackTrace();
  9. } catch (ClientException e) {
  10. e.printStackTrace();
  11. }
  12. }

异常类型

•当http status≥200且<300,表示API调用成功。

•当http status≥300且<500,SDK抛ClientException,表示客户端错误。

•当http status≥500,SDK抛ServerException,表示服务器端错误。