请求很简单,代码如下:
public class VoiceSynthesis {
private static final String serverURL = "http://tsn.baidu.com/text2audio";
private static String token = "";
private static final String apiKey = "g1wSfeYnnLxt7NbErHtrSD75";
private static final String secretKey = "8b5ad2c63a8d75d5012348b836c4b55e";
private static final String cuid = "B8-88-E3-E7-FB-7B";
private static final String lan = "zh";
private static final String testContext = "人道主义";
public static void main(String[] args) throws Exception {
getToken();
method1();
}
private static void getToken() throws Exception {
String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials"
+ "&client_id=" + apiKey + "&client_secret=" + secretKey;
HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL)
.openConnection();
token = new JSONObject(printResponse(conn)).getString("access_token");
}
private static void method1() throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(serverURL)
.openConnection();
// construct params
JSONObject params = new JSONObject();
params.put("tex", testContext);
params.put("lan", lan);
params.put("tok", token);
params.put("ctp", 1);
params.put("cuid", cuid);
// add request header
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/json; charset=utf-8");
conn.setDoInput(true);
conn.setDoOutput(true);
// send request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(params.toString());
wr.flush();
wr.close();
printResponse(conn);
}
private static String printResponse(HttpURLConnection conn)
throws Exception {
if (conn.getResponseCode() != 200) {
// request error
return "";
}
InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(new JSONObject(response.toString()).toString(4));
return response.toString();
}
运行后控制台打印如下:
{
"scope": "public audio_voice_assistant_get audio_tts_post wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian",
"session_secret": "4db44e79ad8c8f99f3246ec50ae7aebd",
"expires_in": 2592000,
"refresh_token": "25.b1d448fc31da25207da8105cafcdbb2e.315360000.1780049836.282335-8202548",
"session_key": "9mzdWEjl9j9XcU+5OXFMp7W9IazpUIEna2kTY76zU7nHDr07u28d5kUoxuJg9XZA11JiEaG8IEsJIo2EtL7fNuuRRnb+",
"access_token": "24.c8e56c992c8d6c5e1ecc231e4e5a4bd6.2592000.1467281836.282335-8202548"
}
{
"err_msg": "parameter error.",
"err_no": 501
}
提示参数错误,到底错在哪里
您好, 后续我们会补全不同语言版本的识别与合成接口的调用示例, 谢谢
请问你有没有解决这个问题,如果已经解决了,能把代码给我吗?谢谢了
直接通过表单传参,不需要json