全部产品
云市场

Java使用示例

更新时间:2019-06-06 11:21:53

Java使用示例

  1. import java.util.UUID;
  2. import com.aliyuncs.CommonRequest;
  3. import com.aliyuncs.CommonResponse;
  4. import com.aliyuncs.DefaultAcsClient;
  5. import com.aliyuncs.IAcsClient;
  6. import com.aliyuncs.exceptions.ClientException;
  7. import com.aliyuncs.http.FormatType;
  8. import com.aliyuncs.http.MethodType;
  9. import com.aliyuncs.profile.DefaultProfile;
  10. import com.aliyuncs.profile.IClientProfile;
  11. public class NlpSdkTestSmaple {
  12. public static void main(String[] args) {
  13. String accessKeyId = "yourAccessKeyId";
  14. String accessKeySecret = "yourAccessKeySecret";
  15. try {
  16. DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Nlp", "nlp.cn-shanghai.aliyuncs.com");
  17. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
  18. IAcsClient client = new DefaultAcsClient(profile);
  19. String postBody = "{\n"
  20. + " \"text\": \"Iphone专用数据线\",\n"
  21. + " \"lang\": \"ZH\"\n" + "}";
  22. CommonRequest request = new CommonRequest();
  23. request.setDomain("nlp.cn-shanghai.aliyuncs.com"); // 必须设置domain
  24. request.setUriPattern("/nlp/api/wordsegment/general"); // 设置所要请求的API路径
  25. request.setMethod(MethodType.POST); // 设置请求方式,目前只支持POST
  26. request.setHttpContent(postBody.getBytes(), "utf-8", FormatType.JSON); // 设置请求内容以及格式
  27. request.putHeadParameter("x-acs-signature-method", "HMAC-SHA1");
  28. request.putHeadParameter("x-acs-signature-nonce", UUID.randomUUID().toString()); // 设置请求唯一码,防止网络重放攻击
  29. request.setVersion("2018-04-08");
  30. CommonResponse response = client.getCommonResponse(request); // 请求并获取结果
  31. System.out.println(response.getData());
  32. } catch (ClientException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }