Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将httpclient重新写入HttpSourceConnection_Android_Upload_Httpurlconnection_Apache Httpclient 4.x - Fatal编程技术网

Android 将httpclient重新写入HttpSourceConnection

Android 将httpclient重新写入HttpSourceConnection,android,upload,httpurlconnection,apache-httpclient-4.x,Android,Upload,Httpurlconnection,Apache Httpclient 4.x,我正在尝试使用httpUrlConnection而不是httpClient发送postRequest。我试图访问的API提供了一个使用httpclinet的实现,但我不能使用它,因为它现在已被弃用。我以为我使用urlConnection做了同样的事情,但是我一直得到一个错误,没有发送任何图像(并且我必须确保我发送的POST请求与文档匹配) 这是我正在使用的API提供的代码 public static String upload(String path) throws IllegalStateEx

我正在尝试使用httpUrlConnection而不是httpClient发送postRequest。我试图访问的API提供了一个使用httpclinet的实现,但我不能使用它,因为它现在已被弃用。我以为我使用urlConnection做了同样的事情,但是我一直得到一个错误,没有发送任何图像(并且我必须确保我发送的POST请求与文档匹配)

这是我正在使用的API提供的代码

public static String upload(String path) throws IllegalStateException, JSONException, IOException {


    String urlString = "http://www.bitocr.com/api";
    File file = new File(path);
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost postRequest = new HttpPost(urlString);

    // build request parameters
    StringBody apiKey = new StringBody("apikey", ContentType.MULTIPART_FORM_DATA);
    StringBody lang = new StringBody("en", ContentType.MULTIPART_FORM_DATA);
    FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addPart("apikey", apiKey);
    builder.addPart("lang", lang);
    builder.addPart("file", fileBody);

    postRequest.setEntity(builder.build());
    HttpResponse res = null;

    // execute the request
    res = httpClient.execute(postRequest);

    JSONObject jsonObject = new JSONObject();
    if (res != null) {

        jsonObject = new JSONObject(IOUtils.toString(res.getEntity().getContent(), "UTF-8"));
        String error = jsonObject.get("error").toString();
        if (error.equals("0")) {
            // success
            System.out.println(jsonObject.getString("result"));
        } else {

            System.out.println("Error #" + jsonObject.get("error_code") + " " + jsonObject.get("error_message"));
        }
    }

    return jsonObject.toString();
}
这是我使用urlConnection实现的上传

private String uploadURLCONN(String path){

    System.setProperty("http.proxyHost", "proxy.example.com");
    System.setProperty("http.proxyPort", "8080");

    String urlString = "http://www.bitocr.com/api";
    File file = new File(path);

    // build request parameters
    StringBody apiKey = new StringBody("api_key", ContentType.MULTIPART_FORM_DATA);
    StringBody lang = new StringBody("eng", ContentType.MULTIPART_FORM_DATA);
    FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addPart("apikey", apiKey);
    builder.addPart("lang", lang);
    builder.addPart("file", fileBody);
    HttpEntity reqEntity = builder.build();

    try {
        URL url = new URL(urlString);
        System.out.println("Not 1");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        System.out.println("Not 2");
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        System.out.println("Not 3");
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
        conn.setRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());

        DataOutputStream os = new DataOutputStream(conn.getOutputStream());
        System.out.println("Not 4");
        reqEntity.writeTo(conn.getOutputStream());
        System.out.println("Not 5");
        os.close();
        System.out.println("Not 6");
        conn.connect();
        System.out.println("Not 7");

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            System.out.println("Not 8");
            return readStream(conn.getInputStream());
        }
    }catch(Exception e){
        System.out.println("multipart post error " + e + "(" + urlString + ")");
    }
    System.out.println("returning null");
    return null;
}
我使用的是httpClient和HttpTime的4.4版。以及httpCore的4.4.3版。任何关于为什么我的照片没有通过的帮助都将不胜感激

我已确保用于图像的路径确实获得了正确的图像,并且Upload()位于正确调用的AsyncTask中。提前谢谢

这就是文档中描述的原始请求的样子

    POST /api HTTP/1.1
    Content-Length:17778
    Content-Type:multipart/form-data; boundary=--------------------------               -3339166599332
    Host:www.bitocr.com

    ---------------------------3339166599332
    Content-Disposition: form-data; name="file"; filename="Untitled.png"
    Content-Type: image/png

    {image data here}
    ---------------------------3339166599332
    Content-Disposition: form-data; name="apikey"

    a7412c8ac8c8d738
    ---------------------------3339166599332
    Content-Disposition: form-data; name="lang"

    eng
    ---------------------------3339166599332

恐怕你的问题太具体了。最好的方法是使用一些tcp嗅探器在fiddler窗口下比较您的请求(HTTPClient one和Url)。差异会告诉你你做错了什么


您还可以尝试转换为完整的Url实现,如下面的答案所示:。我想您使用了这个示例,以便将Url与HttpClient类混合在一起,您可能会发现一些有用的注释,比如使用setFixedLengthStreamingMode。

您并没有说明什么不起作用。响应代码是什么?有陷阱吗?如果是这样,您应该打印e.getMessage().“conn.connect();”。拆下那条线。当数据已经发送时,尝试再次连接完全错误。“但我不断收到错误消息,没有发送图像(并且我必须确保发送的POST请求与文档匹配)。”。你从哪里得到这个错误的?和。谁谈到了匹配?这是输出:{“error”:1,“error\u message”:“未检测到输入文件。请确保根据API文档发送POST请求。”,“error\u code”:5}您正在向php发布吗?如果是,请显示相关代码。那个http请求很正常,所以我想知道为什么会有问题。但是为什么您仍然使用HttpEntity呢?那不是也被贬低了吗?