This topic describes how to use the server SDK for Java provided by ApsaraVideo Live.

Prerequisites

Obtain an AccessKey pair to complete identity verification so that you can call server API operations. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.

Procedure

  1. Download the server SDK for Java. For more information, see SDK download.
  2. Add the dependencies on Alibaba Cloud Core SDK and ApsaraVideo Live SDK to the pom.xml file in the JAR package of the server SDK for Java. In the following example, the version of ApsaraVideo Live SDK is for reference only. For more information about how to download the latest version, visit the ApsaraVideo Live SDK page.
    1. Import Alibaba Cloud Core SDK.
      <dependencies>
          <dependency>    
              <groupId>com.aliyun</groupId>    
              <artifactId>aliyun-java-sdk-core</artifactId>    
              <version>4.4.6</version>
          </dependency>
      </dependencies>
    2. Import ApsaraVideo Live SDK.
      <dependencies>
          <dependency>
              <groupId>com.aliyun</groupId>
              <artifactId>aliyun-java-sdk-live</artifactId>
              <version>3.9.0</version>
          </dependency>
      </dependencies>
  3. Initialize the IAcsClient instance.

    The API operations encapsulated in ApsaraVideo Live SDK are called by using the IAcsClient instance. Therefore, you must initialize an IAcsClient instance before you call API operations.

    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"); // Add a custom endpoint.
         client = new DefaultAcsClient(profile);
         //System.setProperty("http.proxyHost", "127.0.0.1"); // Specify the proxy settings. You can use Fiddler to intercept and view HTTP requests to facilitate debugging.  
         //System.setProperty("http.proxyPort", "8888");
     }
  4. Initialize a request.

    Before you call an API operation, you must initialize the corresponding request instance. In this example, the DescribeLiveSnapshotConfig operation is used.

    public void requestInitSample() {
         DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
         describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com");
         //describeLiveSnapshotConfigRequest.setProtocol(ProtocolType.HTTPS); // Specify the request protocol.
         //describeLiveSnapshotConfigRequest.setAcceptFormat(FormatType.JSON); // Specify the response format of the operation.
         //describeLiveSnapshotConfigRequest.setMethod(MethodType.POST); // Specify the request method.
         //describeLiveSnapshotConfigRequest.setRegionId("cn-shanghai");// Specify the region in which you want to perform the specified operation. This setting is valid only for the current request, and does not affect the default client settings.
         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();
         }
     }
    }
  5. Call the operation and parse the result.

    The IAcsClient instance provides two methods to obtain the response of the call:

    • // Call the doAction method to obtain a response of the HttpResponse type, which is the original response of the call. The following sample code provides an example:
      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();
           }
       }

      Check the returned HTTP status code:

      • If the HTTP status code is greater than or equal to 200 but less than 300, the call is successful.
      • If the HTTP status code is greater than or equal to 300 but less than 500, the server SDK for Java throws a ClientException, which indicates a client error.
      • If the HTTP status code is greater than or equal to 500, the server SDK for Java throws a ServerException, which indicates a server error.
    • Call the getAcsResponse method to obtain a deserialized object. The following sample code provides an example:
      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();
           }
       }