Instructions

Updated at:

Alibaba Cloud provides the Smart Conversation Analysis (SCA) Java SDK and the Alibaba Cloud core library as Maven project dependencies and JAR packages. This document describes how to call the SDK to access the SCA service.

Notes

  • If you have not activated the Alibaba Cloud Smart Conversation Analysis service or are unfamiliar with it, visit the product homepage for more information.

  • This document applies to SDK versions 3.0.0 and later.

  • This document describes how to install and use the Smart Conversation Analysis Java SDK and provides related notes.

    For more information about the Java SDK, see the Alibaba Cloud SDK Documentation.

  • If you have not created an AccessKey ID and an AccessKey secret, go to the Alibaba Cloud AccessKey Management console to create an AccessKey.

    An AccessKey grants full access to all APIs for your Alibaba Cloud account. Exposing your AccessKey poses security risks. We recommend that you create and use a Resource Access Management (RAM) user for API calls and routine O&M. For more information, see Create a RAM user.

  • For detailed descriptions of the request and response parameters for each API, see Developer Guide > API Reference in the navigation pane on the left. For example, see Upload audio for quality inspection.

Download the SDK

You can use Maven to manage dependencies. The latest version number of the SDK is available at https://github.com/aliyun/aliyun-openapi-java-sdk/blob/master/aliyun-java-sdk-qualitycheck/ChangeLog.txt.

Add the following dependencies to the pom.xml file:

       <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-qualitycheck</artifactId>
            <version>3.0.7</version>
        </dependency>
	<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.0.6</version>
        </dependency>

Configure access credentials

Configure the SCA_AK_ENV and SCA_SK_ENV environment variables.

  • Configuration for Linux and macOS

    Run the following commands:

    export SCA_AK_ENV=<access_key_id>
    export SCA_SK_ENV=<access_key_secret>

    Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret.

  • Windows configuration

    1. Create the SCA_AK_ENV and SCA_SK_ENV environment variables and set their values to your AccessKey ID and AccessKey secret.

    2. Restart Windows.

Sample calls

import com.aliyuncs.http.FormatType;
import com.aliyuncs.qualitycheck.model.v20190115.UploadDataRequest;
import com.aliyuncs.qualitycheck.model.v20190115.UploadDataResponse;
import org.junit.Before;
import org.junit.Test;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class TestApi {
    IAcsClient client = null;
    @Before
    public void init() throws ClientException {
        // The AccessKey of an Alibaba Cloud account has permissions on all APIs. Use a RAM user for API calls or routine O&M.
        // Do not save the AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey leak and compromise the security of all your resources.
      	// This example shows how to save the AccessKey ID and AccessKey secret in environment variables. You can also save them in a configuration file as needed.
        String accessKeyId = System.getenv("SCA_AK_ENV");
        String accessKeySecret = System.getenv("SCA_SK_ENV");
        // Build an Aliyun Client to send requests.
        // The endpoint of the Smart Conversation Analysis API is in the China (Hangzhou) region. The Region value is fixed to "cn-hangzhou".
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        // Specify the service endpoint. The following value is a static field.
        DefaultProfile.addEndpoint("HZ", "cn-hangzhou", "Qualitycheck", "qualitycheck.cn-hangzhou.aliyuncs.com");
        client = new DefaultAcsClient(profile);
    }
    /**
     * Obtains the deserialized instance object. If the HTTP status code is greater than or equal to 200 and less than 300, the API call is successful.
     * If the HTTP status code is greater than or equal to 300 and less than 500, a ClientException is reported.
     * If the HTTP status code is greater than or equal to 500, a ServerException is reported.
     * Request and Response appear in pairs. Their prefixes change based on the API operation that you call.
     * For example, when you call the operation to get results, use GetResultRequest/GetResultResponse.
     */
    @Test
    public void uploadDataSample(String aliUid) {
        UploadDataRequest req = new UploadDataRequest();
        req.setJsonStr("<Upload Data Json>");
        try {
            UploadDataResponse response = client.getAcsResponse(req);
            // do something.
            System.out.println(response.getCode());
            System.out.println(response.getMessage());
            System.out.println(com.alibaba.fastjson.JSON.toJSONString(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    /**
     * Obtains the raw result of the API call.
     * For Request/Response, the prefix changes based on the API operation that you call.
     * For example, when you call the operation to get results, use GetResultRequest/GetResultResponse.
     */
    @Test
    public void uploadDataActionSample(String aliUid) {
        UploadDataRequest req = new UploadDataRequest();
        // Define the format of the response message.
        //req.setAcceptFormat(FormatType.JSON);
        req.setJsonStr("<Upload Data Json>");
        // describeRegionsRequest.setRegionId("cn-hangzhou"); // Specify the region to access. This setting is valid only for the current request and does not change the default settings of the client.
        try {
            HttpResponse httpResponse = client.doAction(req);
            System.out.println(req.getUrl());
            System.out.println(httpResponse.getUrl());
            byte[] content = httpResponse.getHttpContent();
	    System.out.println(new String(content));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}