X-Country-Code", countryCode);
// 设置App标识(String),例:"9af1aeda-531b-407a-80b4-65b40ef77bd6"
postMethod.setRequestHeader("HMS-APPLICATION-ID", applicationId);
// 设置证书指纹(String),例:"xxxxxxxxxxxxxxx"
postMethod.setRequestHeader("certFingerprint", certFingerprint);
// 设置动态获取的AccessToken(String)
postMethod.setRequestHeader("Authorization","Bearer " + accessToken);
// 设置请求body
Map<String, Object> bodyMap = new HashMap<>();
// taskId对应的值是创建异步任务时返回的任务ID(taskId)
bodyMap.put("taskId", taskId);
RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
postMethod.setRequestEntity(requestEntity);
HttpClient httpClient = new HttpClient();
int ret = httpClient.executeMethod(postMethod);
String rpsContent = postMethod.getResponseBodyAsString();
if (ret == 200) {
System.out.println(rpsContent);
} else {
System.out.println("callApi failed: ret =" + ret + " rsp=" + rpsContent);
}
}
注意:
上述代码中xxxxx对应的值请根据实际情况填写,具体取值请参见查询异步任务状态。
2.2.3取消异步任务
用户创建歌声合成异步任务后,可以通过调用此接口,取消指定异步任务并删除相应任务数据。
通过access_token信息和创建异步任务获取到的taskId,发送HTTPS POST取消异步任务。
示例代码(Java)如下所示:
其中requestUrl = "https://audioeditor-api-drcn.cloud.huawei.com/v1/audioeditor/gateway/ai/ttsing/async/task/cancel"。
/**
* 调用取消异步任务接口
* @param taskId 创建异步任务获取的taskId
* @throws Exception IO异常
*/
private static void cancelAsyncTask(String taskId) throws Exception {
// 设置请求header
PostMethod postMethod = new PostMethod(requestUrl);
// 设置文本类型(String),例:"application/json;charset=utf-8"
postMethod.setRequestHeader("Content-Type", contentType);
// 设置请求ID(String),例:"9af1aeda-531b-407a-80b4-65b40ef77bd6"
postMethod.setRequestHeader("X-Request-ID", requestId);
// 设置App包名(String),例:"com.huawei.demo"
postMethod.setRequestHeader("X-Package-Name", pacageName);
// 设置App所在国家(String),例:"cn"
postMethod.setRequestHeader("X-Country-Code", countryCode);
// 设置App标识(String),例:"9af1aeda-531b-407a-80b4-65b40ef77bd6"
postMethod.setRequestHeader("HMS-APPLICATION-ID", applicationId);
// 设置证书指纹(String),例:"xxxxxxxxxxxxxxx"
postMethod.setRequestHeader("certFingerprint", certFingerprint);
// 设置动态获取的AccessToken(String)
postMethod.setRequestHeader("Authorization","Bearer " + accessToken);
// 设置请求body
Map<String, Object> bodyMap = new HashMap<>();
// taskId对应的值是创建异步任务时返回的任务ID(taskId)
bodyMap.put("taskId", taskId);
RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
postMethod.setRequestEntity(requestEntity);
HttpClient httpClient = new HttpClient();
int ret = httpClient.executeMethod(postMethod);
String rpsContent = postMethod.getResponseBodyAsString();
if (ret == 200) {
System.out.println(rpsContent);
} else {
System.out.println("callApi failed: ret =" + ret + " rsp=" + rpsContent);
}
}
注意:
上述代码中xxxxx对应的值请根据实际情况填写,具体取值请参见取