HTTP POST 400。错误的请求。 Google云端存储XML API调用上传文件

时间:2016-01-14 04:34:02

标签: xmlhttprequest google-cloud-storage httpurlconnection

我收到此错误代码。

  

E / camera_classes.callHttpPostToSendFiles:异常消息e         java.io.IOException:服务器返回非正常状态:400消息:错误请求错误流:com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream@42353798at camera_classes.postMultipartEntity.finish(postMultipartEntity.java:161)         at camera_classes.callHttpPostToSendFiles.postData(callHttpPostToSendFiles.java:193)         at camera_classes.callHttpPostToSendFiles $ 1.doInBackground(callHttpPostToSendFiles.java:109)         at camera_classes.callHttpPostToSendFiles $ 1.doInBackground(callHttpPostToSendFiles.java:105)         在android.os.AsyncTask $ 2.call(AsyncTask.java:288)         在java.util.concurrent.FutureTask.run(FutureTask.java:237)         在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)         在java.lang.Thread.run(Thread.java:841)

错误发生在

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();
        String server_message = httpConn.getResponseMessage();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                response.add(line);
            }
            reader.close();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status + " message: " + server_message + " error stream : " + httpConn.getErrorStream());
        }

1)我有兴趣获取有关列出here的错误代码(400错误请求)的更多信息,因此我可以理解导致&#34; 400错误请求的原因&#34;错误。谁能帮忙。我尝试了所有可用的公开方法 - httpConn.getResponseMessage()httpConn.getResponseCode()httpConn.getErrorStream()

2)com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream@42353798是什么意思?这是httpConn.getErrorStream()的输出。

1 个答案:

答案 0 :(得分:0)

我正在使用Google Cloud XML Api的上传方法,如下所示。关于你的第二个问题,也许它与使用来自HttpUrlConnection的方法setFixedLengthStreamingMode有关,就像我在我的函数中使用的那样,你必须把文件的长度上传到:

URL url = new URL("www.urltoupload.com");

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.setRequestProperty("Content-Type", Utils.getMimeType(path));
httpCon.setRequestProperty("Connection", "Keep-Alive");
httpCon.setFixedLengthStreamingMode((int) dest.length());

final DataOutputStream out = new DataOutputStream(httpCon.getOutputStream());

float bytesTotal = fileBeingUploaded.length();

int bytesRead;
byte buf[] = new byte[8192];
BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(dest));

while ((bytesRead = bufInput.read(buf)) != -1) {
    out.write(buf, 0, bytesRead);
    out.flush();
}

out.close();