このトピックでは、ApsaraVideo Liveが提供するJava用サーバーSDKの使用方法について説明します。

始める前に

手順

  1. Java用サーバーSDKをダウンロードします。 詳細については、「SDK のダウンロード」をご参照ください。
  2. Alibaba Cloud Core SDKとApsaraVideo Live SDKの依存関係を、サーバーSDK for JavaのJARパッケージのpom.xmlファイルに追加します。 次の例では、ApsaraVideo Live SDKのバージョンは参照専用です。 最新バージョンのダウンロード方法の詳細については、ApsaraVideo Live SDKページをご覧ください。
    1. 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. したがって、API操作を呼び出す前に、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"); // カスタムエンドポイントを追加します。
         client = new DefaultAcsClient(profile);
         // System.setProperty("http.proxyHost", "127.0.0.1"); // プロキシ設定を指定します。 Fiddlerを使用してHTTPリクエストをインターセプトおよび表示し、デバッグを容易にできます。  
         // System.setProperty("http.proxyPort" 、"8888");
     }
  4. リクエストを初期化します。

    Before you call an API operation, you must initialize the corresponding request instance. この例では、DescribeLiveSnapshotConfig操作が使用されています。

    public void requestInitSample() {
         DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest=新しいDescribeLiveSnapshotConfigRequest();
         describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com");
         // describeLiveSnapshotConfigRequest.setProtocol(ProtocolType.HTTPS); // リクエストプロトコルを指定します。
         // describeLiveSnapshotConfigRequest.setAcceptFormat(FormatType.JSON); // 操作の応答形式を指定します。
         //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 (新しい文字列 (httpResponse.getContent()));
             // todo何か
         } catch (ServerException e) {
             e.printStackTrace();
         } catch (ClientException e) {
             e.printStackTrace();
         }
     }
    }
  5. 操作を呼び出し、結果を解析します。

    IAcsClientインスタンスは、呼び出しの応答を取得する2つのメソッドを提供します。

    • // doActionメソッドを呼び出して、呼び出しの元の応答であるHttpResponse型の応答を取得します。 次のサンプルコードに例を示します。
      public void invokeSample() {
           DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest=新しいDescribeLiveSnapshotConfigRequest();
           describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com");
           try {
               HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest);
               System.out.println(httpResponse.getUrl());
               System.out.println (新しい文字列 (httpResponse.getContent()));
               // todo何か他のもの
           } catch (ServerException e) {
               e.printStackTrace();
           } catch (ClientException e) {
               e.printStackTrace();
           }
       }

      返されたHTTPステータスコードを確認します。

      • HTTPステータスコードが200以上300未満の場合、呼び出しは成功です。
      • HTTPステータスコードが300以上500未満の場合、Java用サーバーSDKはクライアントエラーを示すClientExceptionをスローします。
      • HTTPステータスコードが500以上の場合、Java用サーバーSDKはサーバーエラーを示すServerExceptionをスローします。
    • getAcsResponseメソッドを呼び出して、逆シリアル化オブジェクトを取得します。 次のサンプルコードに例を示します。
      public void invokeSample() {
           DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest=新しいDescribeLiveSnapshotConfigRequest();
           describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com");
           try {
               DescribeLiveSnapshotConfigResponse describeLiveSnapshotConfigResponse = client.getAcsResponse(describeLiveSnapshotConfigRequest);
               // todo何か
           } catch (ServerException e) {
               e.printStackTrace();
           } catch (ClientException e) {
               e.printStackTrace();
           }
       }