【置顶】语音合成REST API问题及答案汇总贴
Ta的回复 :IsonGT: 希望有一份java的例子提供参考,我写好的java后台方法测试总是提示"err_msg": "parameter error.","err_no": 501。如果正常在浏览器地址栏请求是可以正常显示的。只是在后台运行就报错 HttpURLConnection conn = (HttpURLConnection) new URL(serverURL).openConnection(); // 设置请求参数 JSONObject params = new JSONObject(); params.put("tex", URLEncoder.encode(URLEncoder.encode("广州中科诺泰技术邮箱公司", "utf-8"), "utf-8")); params.put("lan", URLEncoder.encode(URLEncoder.encode("zh", "utf-8"), "utf-8")); params.put("ctp", URLEncoder.encode(URLEncoder.encode("1", "utf-8"), "utf-8")); params.put("tok", URLEncoder.encode(URLEncoder.encode(token, "utf-8"), "utf-8")); params.put("cuid", URLEncoder.encode(URLEncoder.encode(cuid, "utf-8"), "utf-8")); // add request header conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "json/html; charset=utf-8"); conn.setDoInput(true); conn.setDoOutput(true); System.out.println("转码前:" + params.toString()); //URLENCODE转码 String s = params.toString(); System.out.println("转码后:" + s); // 发送接受 DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(s); wr.flush(); wr.close(); 以上是例子代码,我希望是获取二进制流的。 回复IsonGT:我使用的matlab里面自带用的是嵌入的java。根据我使用成功的那个样例。里面涉及到的文件输出代码为 function [file,fileOutputStream] = getFileOutputStream(location) % Specify the full path to the file so that getAbsolutePath will work when the % current directory is not the startup directory and urlwrite is given a % relative path. file = java.io.File(location); if ~file.isAbsolute location = fullfile(pwd,location); file = java.io.File(location); end try % Make sure the path isn't nonsense. file = file.getCanonicalFile; % Open the output file. fileOutputStream = java.io.FileOutputStream(file); catch error(mm('urlwrite','InvalidOutputLocation',char(file.getAbsolutePath))); end