CommonRequest中的setDomain方法 在IDEA中 根本点不出来还有 CommonResponse中的getHttpStatus都是一样的问题!
下面是具体的代码:红色部分即为刚才所述的问题
package com.yg.util; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; public class Transcription { /** * 地域 ID 常量内容,请勿改变 */ public static final String REGIONID = "cn-shanghai"; public static final String ENDPOINTNAME = "cn-shanghai"; public static final String PRODUCT = "nls-filetrans"; public static final String DOMAIN = "filetrans.cn-shanghai.aliyuncs.com"; public static final String API_VERSION = "2018-08-17"; public static final String POST_REQUEST_ACTION = "SubmitTask"; public static final String GET_REQUEST_ACTION = "GetTaskResult"; /** * 参数设置 Key 常量内容,请勿改变 */ public static final String KEY_APP_KEY = "xyJ0B4BoFmH3bRgR"; public static final String KEY_FILE_LINK = "file_link"; public static final String KEY_TASK = "Task"; public static final String KEY_TASK_ID = "TaskId"; public static final String KEY_STATUS_TEXT = "StatusText"; public static void main(String args[]) throws Exception { if (args.length < 3) { System.err.println("FileTransJavaDemo need params: <AccessKey Id> <AccessKey Secret> <app-key>"); } //获取访问标识accessKeyId final String accessKeyId = args[0]; //获取accessKeySecret final String accessKeySecret = args[1]; //获取项目标识appKey final String appKey = args[2]; /** * 阿里云鉴权 client */ IAcsClient client; // 设置 endpoint DefaultProfile.addEndpoint(ENDPOINTNAME, REGIONID, PRODUCT, DOMAIN); // 创建 DefaultAcsClient 实例并初始化 DefaultProfile profile = DefaultProfile.getProfile(REGIONID, accessKeyId, accessKeySecret); client = new DefaultAcsClient(profile); /** * 创建 CommonRequest 设置请求参数 */ CommonRequest postRequest=null; // 设置域名 postRequest.setDomain(DOMAIN); // 设置 API 的版本号,格式为 YYYY-MM-DD postRequest.setVersion(API_VERSION); // 设置 action postRequest.setActionName(POST_REQUEST_ACTION); // 设置产品名称 postRequest.setLocationProduct(PRODUCT); /** * 设置录音文件识别请求参数,以 JSON 字符串的格式设置到请求的 Body 中 */ JSONObject taskObject = new JSONObject(); // 设置 app_key taskObject.put(KEY_APP_KEY, appKey); // 设置音频文件访问链接 taskObject.put(KEY_FILE_LINK, "http://aliyun-nls.oss.aliyuncs.com/asr/fileASR/examples/nls-sample.wav"); String task = taskObject.toJSONString(); // 设置以上 JOSN 字符串为 Body 参数 postRequest.putQueryParameter(KEY_TASK, task); // 设置为 POST 方式的请求 postRequest.setMethod(MethodType.POST); /** * 提交录音文件识别请求 */ // 获取录音文件识别请求任务的 ID,以供识别结果查询使用 String taskId = ""; CommonResponse postResponse = client.getCommonResponse(postRequest); if (postResponse.getHttpStatus() == 200) { JSONObject result = JSONObject.parseObject(postResponse.getData()); String statusText = result.getString(KEY_STATUS_TEXT); if (statusText.equals("SUCCESS")) { System.out.println("录音文件识别请求成功响应:" + result.toJSONString()); taskId = result.getString(KEY_TASK_ID); } else { System.err.println("录音文件识别请求失败:" + postResponse.getData()); return; } } else { System.err.println("录音文件识别请求失败,Http 错误码:" + postResponse.getHttpStatus()); System.err.println("录音文件识别请求失败响应:" + postResponse.getData()); return; } /** * 创建 CommonRequest 设置任务 ID */ CommonRequest getRequest = new CommonRequest(); // 设置域名 // getRequest.setDomain(DOMAIN); // 设置 API 版本 getRequest.setVersion(API_VERSION); // 设置 action getRequest.setActionName(GET_REQUEST_ACTION); // 设置产品名称 getRequest.setLocationProduct(PRODUCT); // 设置任务 ID 为查询参数 getRequest.putQueryParameter(KEY_TASK_ID, taskId); // 设置为 GET 方式的请求 getRequest.setMethod(MethodType.GET); /** * 提交录音文件识别结果查询请求 * 以轮询的方式进行识别结果的查询,直到服务端返回的状态描述为“SUCCESS”、“SUCCESS_WITH_NO_VALID_FRAGMENT”,或者为错误描述,则结束轮询。 */ String statusText = ""; while (true) { CommonResponse getResponse = client.getCommonResponse(getRequest); if (getResponse.getHttpStatus() != 200) { System.err.println("识别结果查询请求失败,Http 错误码:" + getResponse.getHttpStatus()); System.err.println("识别结果查询请求失败:" + getResponse.getData()); break; } JSONObject result = JSONObject.parseObject(getResponse.getData()); System.out.println("识别查询结果:" + result.toJSONString()); statusText = result.getString(KEY_STATUS_TEXT); if (statusText.equals("RUNNING") || statusText.equals("QUEUEING")) { // 继续轮询 Thread.sleep(3000); } else { break; } } if (statusText.equals("SUCCESS") || statusText.equals("SUCCESS_WITH_NO_VALID_FRAGMENT")) { System.out.println("录音文件识别成功!"); } else { System.err.println("录音文件识别失败!"); } } }