This topic describes how to use the server SDK for Java.

Procedure

  1. Download the server SDK for Java. For more information, see SDK download.
  2. Add the dependency on ApsaraVideo Live SDK to the pom.xml file in the JAR package of the server SDK for Java.
    1. Modify the dependency on Alibaba Cloud SDK for Java.
      <dependencies>
          <dependency>    
              <groupId>com.aliyun</groupId>    
              <artifactId>aliyun-java-sdk-core</artifactId>    
              <version>4.4.6</version>
          </dependency>
      </dependencies>
    2. Add the dependency on ApsaraVideo Live SDK.
      <dependencies>
          <dependency>
              <groupId>com.aliyun</groupId>
              <artifactId>aliyun-java-sdk-live</artifactId>
              <version>3.9.0</version>
          </dependency>
      </dependencies>

    You can also add the dependency on a historical version as required. For more information, see Alibaba Cloud SDK.

  3. Initialize the IAcsClient instance.

    The server SDK for Java calls operations by using the IAcsClient instance. Before you call an operation, you must initialize the IAcsClient instance. The following code provides an example:

    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 operation, you must initialize the corresponding request instance. The following sample code uses the DescribeLiveSnapshotConfig operation as an example:

    public void requestInitSample() {
         DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
         describeLiveSnapshotConfigRequest.setDomainName("live.aliyunlive.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 access region. 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 code provides an example:
      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();
           }
       }

      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 code provides an example:
      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();
           }
       }