HttpURLConnection conn = null;
try {
URL url = new URL(YITU_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
// 设置接收类型否则返回415错误
//conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型,以此来防范返回415;
conn.setRequestProperty("accept", "application/json");
// 设置文件类型:
if(mFile == null) {
mFile = new File(AUDIO_RECORD);
}
String strBoundary = java.util.UUID.randomUUID().toString();
//conn.setRequestProperty("speech_file", file.getName());
MyLog.LOGD("MainActivity", "filename = " + mFile.getName());
JSONObject jsonObject = new JSONObject();
jsonObject.put("engine_type", "sms16k");
jsonObject.put("aue", "pcm");
jsonObject.put("scene", "main");
jsonObject.put("flags", 0);
// JSONArray jsonArray = new JSONArray();
// jsonArray.put(jsonObject);
// String json = jsonArray.toString();
// MyLog.LOGD("MainActivity", "json = " + json);
// MyLog.LOGD("MainActivity", "jsonObject = " + jsonObject.toString());
// String param_base64 = "eyJmbGFncyI6IDAsICJhdWUiOiAicGNtIiwgInNjZW51IjogIm1haW4iLCAiZW5naW51X3R5cGUiOiAic21zMTZrIn0=";
String param_base64 = "eyJlbmdpbmVfdHlwZSI6ICJzbXMxNmsiLCAiYXVlIjogInBjbSIsICJzY2VuZSI6ICJtYWluIiwgImZsYWdzIjogMH0=";
// String param_base64 = Base64.encodeToString(jsonObject.toString().getBytes("UTF-8"),1).toString();
MyLog.LOGD(TAG,"param_base64:"+param_base64);
Long lCurtime = System.currentTimeMillis()/1000;///1000;//精确到s
MyLog.LOGD("MainActivity","curtime = "+lCurtime);
//String strCheckSum = getMD5(YITU_DEV_KEY + lCurtime + param_base64);
Md5 md5 = new Md5(YITU_DEV_KEY + lCurtime + param_base64);
String strCheckSum = md5.get32();
MyLog.LOGD("MainActivity","strCheckSum = "+strCheckSum);
conn.setRequestProperty("AppId", YITU_USER);
conn.setRequestProperty("CurTime", lCurtime + "");
conn.setRequestProperty("Param", param_base64);
conn.setRequestProperty("Checksum", strCheckSum);
StringBuffer sb = new StringBuffer();
sb.append("--"+strBoundary+"\r\n");
sb.append("Content-Disposition: form-data; name=\"speech_file\"; filename=\""+mFile.getName()+"\"\r\n");
sb.append("Content-Type: audio/wav\r\n\r\n");
byte[] data = sb.toString().getBytes("UTF-8");
byte[] end_data = ("\r\n--"+strBoundary+"--\r\n").getBytes("UTF-8");
conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + strBoundary);
// conn.setRequestProperty("Content-Length",String.valueOf(data.length+mFile.length()+end_data.length));
MyLog.LOGD("MainActivity","end_datalength = "+end_data.length);
conn.connect();
conn.getOutputStream();
OutputStream out = new DataOutputStream(conn.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(mFile));
out.write(data);
int bytes = 0;
byte[] bufferOut = new byte[2048];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
out.write(end_data);
out.flush();
out.close();
int statuCode = conn.getResponseCode();
MyLog.LOGD("MainActivity", "statuCode = " + statuCode);
if(statuCode == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String json = null;
while ((json = reader.readLine()) != null) {
//System.out.println("---line---" + line);
JSONObject jsonObject1 = new JSONObject(json);
outputYituMessage(jsonObject1.getString("data"));
MyLog.LOGD("MainActivity", json);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}