全部产品
云市场

示例

更新时间:2017-11-20 20:06:04

智能对话分析服务推荐您使用SDK进行相关业务开发。利用SDK进行开发可有效屏蔽JSON拼接、加签、鉴权等方面的难度。SDK的调用方法请参考Github。采用SDK调用有Get/Post两种方式,下面以上传待检文本数据接口为例,展示接口调用的两种方式。

  1. import org.junit.Before;
  2. import org.junit.Test;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.exceptions.ServerException;
  7. import com.aliyuncs.http.FormatType;
  8. import com.aliyuncs.http.HttpResponse;
  9. import com.aliyuncs.http.MethodType;
  10. import com.aliyuncs.http.ProtocolType;
  11. import com.aliyuncs.profile.DefaultProfile;
  12. import com.aliyuncs.profile.IClientProfile;
  13. import com.aliyuncs.qualitycheck.model.v20160801.*;
  14. public class TestApi {
  15. IAcsClient client = null;
  16. @Before
  17. public void init() throws ClientException {
  18. // 构建一个 Aliyun Client, 用于发起请求
  19. // 构建Aliyun Client时需要设置AccessKeyId和AccessKeySevcret
  20. // SCA API入口位于华东 1 (杭州) , 这里Region填写"cn-hangzhou"
  21. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<secret Id>", "<secret key>");
  22. //用于指定Endpoint
  23. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Qualitycheck", "qualitycheck.cn-hangzhou.aliyuncs.com");
  24. client = new DefaultAcsClient(profile);
  25. }
  26. /**
  27. * 取得反序列化的实例对象 当http status>=200且<300 表示API调用成功;当http status>=300且<500
  28. * SDK抛ClientException;当http status >=500 SDK 抛 ServerException
  29. * 注意,Request/Response会成对儿出现,其前缀会根据调用的相关接口进行变更,
  30. * 例如调用获取结果接口时使用 GetResultRequest/GetResultResponse
  31. */
  32. @Test
  33. public void uploadDataSample(String aliUid) {
  34. UploadDataRequest req = new UploadDataRequest();
  35. req.setJsonStr("<Upload Data Json>");
  36. try {
  37. UploadDataResponse response = client.getAcsResponse(req);
  38. // todo something.
  39. System.out.println(response.getCode());
  40. System.out.println(response.getMessage());
  41. System.out.println(com.alibaba.fastjson.JSON.toJSONString(response));
  42. } catch (ServerException e) {
  43. e.printStackTrace();
  44. } catch (ClientException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. /**
  49. * 取得原始的api调用结果
  50. * 注意,Request/Response,其前缀会根据调用的相关接口进行变更,
  51. * 例如调用获取结果接口时使用 GetResultRequest/GetResultResponse
  52. */
  53. @Test
  54. public void uploadDataActionSample(String aliUid) {
  55. UploadDataRequest req = new UploadDataRequest();
  56. req.setJsonStr("<Upload Data Json>");
  57. req.setProtocol(ProtocolType.HTTP); // 指定访问协议
  58. req.setAcceptFormat(FormatType.JSON); // 指定api返回格式
  59. req.setMethod(MethodType.POST); // 指定请求方法
  60. // describeRegionsRequest.setRegionId("cn-hangzhou");//指定要访问的Region,仅对当前请求生效,不改变client的默认设置。
  61. try {
  62. HttpResponse httpResponse = client.doAction(req);
  63. System.out.println(req.getUrl());
  64. System.out.println(httpResponse.getUrl());
  65. System.out.println(new String(httpResponse.getContent()));
  66. // todo something.
  67. } catch (ServerException e) {
  68. e.printStackTrace();
  69. } catch (ClientException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }