不要将数据发送到ocr.api服务器

时间:2019-06-02 15:08:45

标签: java android ocr

我无法将数据发送到服务器。我的错在哪里?

我搜索了有关如何将文件传递到服务器的信息,但是我什么也没找到

private String sendPost(boolean isOverlayRequired, File imageUrl, String language) throws Exception {
    //  String url = "https://api.ocr.space/parse/image";
    String apiKey="";
    StringBuffer responseString = new StringBuffer();
    try {
        MultipartUtility multipart = new MultipartUtility(url, "UTF-8");
        multipart.addHeaderField("User-Agent", "Mozilla/5.0");
        multipart.addHeaderField("Accept-Language", "tr,en-us,en;q=0.5");
        multipart.addFilePart("file", imageUrl);
        multipart.addFormField("language", language);

        multipart.addFormField("apikey", "my api");
        multipart.addFormField("isOverlayRequired", "false");

        List<String> response = multipart.finish();

        for (String line : response) {
            responseString.append(line);
        }
    } catch (IOException ex) {
        Log.v("OCR Exception", ex.getMessage());
    }

    //return result
    return String.valueOf(responseString);
}

}

和我的多元课程

public MultipartUtility(字符串requestURL,字符串字符集)             引发IOException {         this.charset = charset;

    // creates a unique boundary based on time stamp
    boundary = "===" + System.currentTimeMillis() + "===";

    URL url = new URL(requestURL);
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setDoOutput(true); // indicates POST method
    httpConn.setDoInput(true);
    httpConn.setRequestProperty("Content-Type",
            "multipart/form-data; boundary=" + boundary);
    httpConn.setRequestProperty("User-Agent", "Mozilla/5.0");
    httpConn.setRequestProperty("language","tur");
    httpConn.setRequestProperty("apikey", "my api");

    outputStream = httpConn.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
            true);
}

public void addFormField(String name, String value) {
    writer.append("--" + boundary).append(LINE_FEED);
    writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
            .append(LINE_FEED);
    writer.append("Content-Type: text/plain; charset=" + charset).append(
            LINE_FEED);
    writer.append(LINE_FEED);
    writer.append(value).append(LINE_FEED);
    writer.flush();
}


public void addFilePart(String fieldName, File uploadFile)
        throws IOException {
    String fileName = uploadFile.getName();
    writer.append("--" + boundary).append(LINE_FEED);
    writer.append(
            "Content-Disposition: form-data; name=\"" + fieldName
                    + "\"; filename=\"" + fileName + "\"")
            .append(LINE_FEED);
    writer.append(
            "Content-Type: "
                    + URLConnection.guessContentTypeFromName(fileName))
            .append(LINE_FEED);
    Log.v("MultiPart",fileName);
    Log.v("MultiPart",uploadFile.getPath());
    writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
    writer.append(LINE_FEED);
    writer.flush();

    FileInputStream inputStream = new FileInputStream(uploadFile);
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    outputStream.flush();
    inputStream.close();

    writer.append(LINE_FEED);
    writer.flush();
}


public void addHeaderField(String name, String value) {
    writer.append(name + ": " + value).append(LINE_FEED);
    writer.flush();
}


public List<String> finish() throws IOException {
    List<String> response = new ArrayList<String>();

    writer.append(LINE_FEED).flush();
    writer.append("--" + boundary + "--").append(LINE_FEED);
    writer.close();

    // checks server's status code first
    int status = httpConn.getResponseCode();
    Log.v("MultiPart",""+httpConn.getResponseMessage());
    if (status == HttpURLConnection.HTTP_OK) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpConn.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            Log.v("MultiPart",""+line);
            response.add(line);
        }
        reader.close();
        httpConn.disconnect();
    } else {
        throw new IOException("Server returned non-OK status: " + status);
    }

    return response;
}

}

当我运行程序并碰到臀部时,程序什么也没有显示,只有等待

0 个答案:

没有答案