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

时间:2017-02-17 17:00:01

标签: android httpurlconnection cmusphinx

我需要从此网站 http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run

获取数据

但是我得到了这个" [ ERRO ]你的语料库的问题;无法继续。请检查诊断[0 0]和#34;当我尝试将文本文件发送到网站时。

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

最好将OkHttp用于网络请求。您可以尝试this example进行多部分请求。

答案 1 :(得分:1)

感谢您的帮助@MaxV我通过使用OkHttp和POSTMAN解决了我的问题