视频直播的API是基于阿里云的OpenAPI实现的,因此SDK的调用也是对OpenAPI封装后调用。Java SDK建议使用maven管理项目依赖,在pom.xml中添加如下的依赖项,详情参考 SDK版本号

<dependencies>
    <dependency>
       <groupId>com.aliyun</groupId>
       <artifactId>aliyun-java-sdk-live</artifactId>
       <version>2.6.0</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
    <optional>true</optional>
      <version>3.2.9</version>
    </dependency>
</dependencies>
		

初始化Client

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

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

初始化请求

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

public void requestInitSample() {
        DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
        describeLiveSnapshotConfigRequest.setDomainName("live.aliyunlive.com");
        //describeLiveSnapshotConfigRequest.setProtocol(ProtocolType.HTTPS); //指定访问协议
        //describeLiveSnapshotConfigRequest.setAcceptFormat(FormatType.JSON); //指定api返回格式
        //describeLiveSnapshotConfigRequest.setMethod(MethodType.POST); //指定请求方法
        //describeLiveSnapshotConfigRequest.setRegionId("cn-shanghai");//指定要访问的Region,仅对当前请求生效,不改变client的默认设置。
        try {
            HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest);

            System.out.println(httpResponse.getUrl());
            System.out.println(new String(httpResponse.getContent()));
            //todo something.
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
			

发起OpenAPI调用并解析结果

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

public void invokeSample() {
        DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
        describeLiveSnapshotConfigRequest.setDomainName("live.aliyunlive.com");
        try {
            HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest);
            System.out.println(httpResponse.getUrl());
            System.out.println(new String(httpResponse.getContent()));
            //todo something else.

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
			

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

public void invokeSample() {
        DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
        describeLiveSnapshotConfigRequest.setDomainName("live.aliyunlive.com");
        try {
            DescribeLiveSnapshotConfigResponse describeLiveSnapshotConfigResponse = client.getAcsResponse(describeLiveSnapshotConfigRequest);
            //todo something.

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
			

异常类型

  • 当http status >= 200且 < 300 表示API调用成功。
  • 当http status >= 300且 < 500 SDK抛ClientException,表示客户端错误。
  • 当http status >= 500 SDK抛ServerException,表示服务器端错误。