Android中使用httpurlConnection传递音频数据

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();
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值