通过阅读本文,您可以快速了解视频直播服务端Java SDK的使用方法。
前提条件
调用服务端接口需要使用AccessKey完成身份验证,请提前获取AccessKey。获取方法请参见获取AccessKey。
操作步骤
- 下载服务端Java SDK,详情请参见SDK下载。
- 修改服务端SDK(jar包)中的pom.xml文件,引入阿里云核心SDK和视频直播SDK。此处版本仅供参考,获取最新的版本请参见视频直播SDK。
- 引入阿里云核心SDK。
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.6</version> </dependency> </dependencies>
- 引入视频直播SDK。
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-live</artifactId> <version>3.9.0</version> </dependency> </dependencies>
- 引入阿里云核心SDK。
- 初始化IAcsClient实例。
视频直播SDK通过IAcsClient实例完成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前,需先初始化对应的请求实例,此处以DescribeLiveSnapshotConfig接口为例,示例代码如下所示:
public void requestInitSample() { DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest(); describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.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提供了两种类型的调用结果,如下所示:
- 调用
doAction
方法获取原始的API调用结果,即返回HttpResponse
类型的结果。示例代码如下所示:public void invokeSample() { DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest(); describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.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(); } }
返回结果如下所示:
- 当http status大于等于200且小于300时,表示API调用成功。
- 当http status大于等于300且小于500时,服务端SDK会提示ClientException,表示客户端错误。
- 当http status大于等于500时,服务端SDK会提示ServerException,表示服务器端错误。
- 调用
getAcsResponse
方法,获取反序列化后的对象,示例代码如下所示:public void invokeSample() { DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest(); describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com"); try { DescribeLiveSnapshotConfigResponse describeLiveSnapshotConfigResponse = client.getAcsResponse(describeLiveSnapshotConfigRequest); //todo something } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } }
- 调用