android - 如何在 Android 中使用文本文件创建和发送多部分请求

标签 android httpurlconnection cmusphinx

我需要从这个站点获取数据 http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run

但是当我尝试将文本文件发送到站点时,出现“[ERRO] 语料库问题;无法继续。请检查诊断 [0 0]”。

这是我的代码:

String fileUrl = "/sdcard/fish.txt";
        File logFileToUpload = new File(fileUrl);
        final String BOUNDERY = "------WebKitFormBoundary4Pn8WfAaV8Bv3qqy";
        final String CRLF = "\r\n";
       // MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        StringBuilder sbBody_1 = new StringBuilder();
        sbBody_1.append(BOUNDERY + CRLF);
        sbBody_1.append("Content-Disposition: form-data; name=\"formtype\"" + CRLF);
        sbBody_1.append(CRLF);
       sbBody_1.append("simple");
        sbBody_1.append(BOUNDERY + CRLF);
        sbBody_1.append("Content-Disposition: form-data; name =\"corpus\""+"filename=\"fish\"" + CRLF);
        //sbBody_1.append("Content-Disposition: form-data; filename=\"fish\"" + CRLF);
        String str1="aaa";
        sbBody_1.append(CRLF);
        //sbBody_1.append(str1);
        //sbBody_1.append(CRLF);
        //sbBody_1.append(BOUNDERY + "--" );
        StringBuilder sbBody_2 = new StringBuilder();
        //sbBody_2.append("pratik");
        sbBody_2.append(BOUNDERY + "--" );
        URL url = new URL("http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
       // connection.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary4Pn8WfAaV8Bv3qqy");
       // connection.setChunkedStreamingMode(0);
        OutputStream out = new BufferedOutputStream(connection.getOutputStream());
        out.write(sbBody_1.toString().getBytes());
        //byte[] bFile = new String(getBytesFromFile(Files1)).getBytes();
       // System.out.println(""+bFile);
        FileInputStream inputStreamToLogFile = new FileInputStream(logFileToUpload);
        int bytesRead;
        byte[] dataBuffer = new byte[1024];
        while((bytesRead = inputStreamToLogFile.read(dataBuffer)) != -1) {
            out.write(dataBuffer, 0, bytesRead);
            System.out.println("output"+dataBuffer +bytesRead);
        }
       out.write(sbBody_2.toString().getBytes());
        //out.write(CRLF.getBytes());
        out.flush();
        out.close();
        BufferedReader bips = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String temp = null;
        while ((temp = bips.readLine()) != null) {
            System.out.println("output"+temp);
            ((TextView) findViewById(R.id.textview1))
                    .setText(temp);
        }
        bips.close();
        connection.disconnect();

最佳答案

最好用OkHttp对于网络请求。你可以试试this example对于多部分请求。

关于android - 如何在 Android 中使用文本文件创建和发送多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42303493/

相关文章:

java - 如何使用加速度计值显示数字

android - fragment 中的上下文操作模式 - 如果不集中则关闭?

android - 如何在 android studio gradle 中降级 proguard 版本?

Java:IllegalStateArgument 的 HttpURLConnection 问题:已经连接

android - HttpURLConnection getResponseCode 未返回

java - CMU Sphinx4 音素听写

java - SaxLoader 中的 Sphinx4 空指针异常

java - Android - 显示文件类型的图像

android - Pocketsphinx - 完善热词检测

java - 在 httpurlconnection 中编码 json 对象参数