You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
4.8 KiB
115 lines
4.8 KiB
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
import com.aliyuncs.IAcsClient;
|
|
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
|
|
import com.aliyuncs.http.FormatType;
|
|
import com.aliyuncs.http.HttpResponse;
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
import com.aliyuncs.profile.IClientProfile;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import java.util.*;
|
|
|
|
/**
|
|
* 查询异步语音检测结果。
|
|
*
|
|
* @author yinzuomei@elink-cn.com
|
|
* @date 2020/12/8 22:08
|
|
*/
|
|
@Slf4j
|
|
public class VoiceAsyncScanResultsRequestSample extends BaseSample {
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
// 请替换成您自己的AccessKey ID、AccessKey Secret。
|
|
IClientProfile profile = DefaultProfile
|
|
.getProfile("cn-shanghai",
|
|
"LTAI4G6Fv6uTzQbpsayATHq4",
|
|
"QevMw1RYCwQUG3RSMPq1J6EAfmSblo");
|
|
final IAcsClient client = new DefaultAcsClient(profile);
|
|
//提交语音异步检测任务后返回的taskId
|
|
pollingScanResult(client, Arrays.asList("vc_f_7WZZ01BCH0q6ZnM3g1OKGG-1tAaZ7", "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi"));
|
|
// pollingScanResult(client, "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi");
|
|
}
|
|
|
|
public static void pollingScanResult(IAcsClient client, List<String> taskIdList) throws InterruptedException {
|
|
int failCount = 0;
|
|
boolean stop = false;
|
|
do {
|
|
// 设置每10秒查询一次。
|
|
Thread.sleep(10 * 1000);
|
|
JSONObject scanResult = getScanResult(client, taskIdList);
|
|
System.out.println("接口完整返参:" + JSON.toJSONString(scanResult, true));
|
|
if (scanResult == null || 200 != scanResult.getInteger("code")) {
|
|
failCount++;
|
|
System.out.println("请求失败,get result fail, failCount=" + failCount);
|
|
if (scanResult != null) {
|
|
System.out.println("请求失败,错误信息errorMsg:" + scanResult.getString("msg"));
|
|
}
|
|
if (failCount > 20) {
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
JSONArray taskResults = scanResult.getJSONArray("data");
|
|
if (taskResults.isEmpty()) {
|
|
System.out.println("请求成功,but data is empty");
|
|
break;
|
|
}
|
|
System.out.println("data.size=" + taskResults.size());
|
|
for (Object taskResult : taskResults) {
|
|
JSONObject result = (JSONObject) taskResult;
|
|
Integer code = result.getInteger("code");
|
|
String taskId = result.getString("taskId");
|
|
if (280 == code) {
|
|
System.out.println("taskId=" + taskId + ": processing status: " + result.getString("msg"));
|
|
} else if (200 == code) {
|
|
System.out.println("taskId=" + taskId + "请求成功,返参:" + JSON.toJSONString(taskResult, true));
|
|
stop = true;
|
|
} else {
|
|
System.out.println("taskId=" + taskId + "请求失败,返参:" + JSON.toJSONString(taskResult, true));
|
|
stop = true;
|
|
}
|
|
}
|
|
} while (!stop);
|
|
}
|
|
|
|
private static JSONObject getScanResult(IAcsClient client, List<String> taskIdList) {
|
|
VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest();
|
|
getResultsRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
|
|
getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
|
|
getResultsRequest.setEncoding("utf-8");
|
|
getResultsRequest.setRegionId("cn-shanghai");
|
|
|
|
|
|
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
|
|
for (String taskId : taskIdList) {
|
|
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
|
|
task1.put("taskId", taskId);
|
|
tasks.add(task1);
|
|
}
|
|
|
|
/**
|
|
* 请务必设置超时时间。
|
|
*/
|
|
getResultsRequest.setConnectTimeout(3000);
|
|
getResultsRequest.setReadTimeout(6000);
|
|
|
|
try {
|
|
getResultsRequest.setHttpContent(JSON.toJSONString(tasks).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
|
|
|
|
HttpResponse httpResponse = client.doAction(getResultsRequest);
|
|
if (httpResponse.isSuccess()) {
|
|
return JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
|
|
} else {
|
|
System.out.println("response not success. status: " + httpResponse.getStatus());
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("method exception", e);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|