1

我正在尝试使用 Android 上的 Documents List API 将文件上传到 Google Docs/Drive。

问题是它似乎没问题(我得到状态代码 200,好),但是在查看我的文档时文件没有显示。上传到列出提要时得到的这个地址:https ://docs.google.com/feeds/upload/create-session/default/private/full?convert=false

我尝试了几种方法,但没有任何运气,例如:

File file = new File(sFile); // Path to the file on the SD card

InputStreamContent content = new InputStreamContent("application/vnd.mymimetype",
                    new BufferedInputStream(new FileInputStream(file)));

content.setLength(file.length());
HttpRequest request = transport.createRequestFactory().buildPostRequest(new GoogleUrl(sURL), content); // Urls is https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false

GoogleHeaders headers = getDefaultHeaders(); // Set version 3 and authentication

request.setHeaders(headers);
res = request.execute(); // Will have status code 200, not 201 as created and no entity returned?

我也尝试过使用 MediaUploader,但结果相同(在此示例中,我设置了元数据,但我认为我收到了未设置 Content Lenght 的异常,因此在我的工作示例中,我没有设置元数据):

StringBuilder sAtom = new StringBuilder()
                    .append("<?xml version='1.0' encoding='UTF-8'?>")
                    .append("<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:docs=\"http://schemas.google.com/docs/2007\">")
                    .append("<title>THeFile</title>").append("</entry>");
 String sCmd = sAtom.toString();
 InputStream inStream = null;

 inStream = new ByteArrayInputStream(sCmd.getBytes("UTF-8"));

 final InputStreamContent oContent = new InputStreamContent("application/atom+xml", inStream);
 oContent.setLength(sCmd.length());

 InputStreamContent mediaContent = new InputStreamContent("application/vnd.mymimetype",
                    new BufferedInputStream(new FileInputStream(file)));
 mediaContent.setLength(file.length());

 MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, transport, new HttpRequestInitializer() {

                @Override
                public void initialize(HttpRequest request) throws IOException {
                    GoogleHeaders headers = getDefaultHeaders();
                    headers.setSlug("thefile.mab");
                    request.setHeaders(headers);
                }
            });
 uploader.setMetadata(oContent);

 MediaHttpUploaderProgressListener listner = new CustomProgressListener();
 uploader.setProgressListener(listner);
 uploader.setDirectUploadEnabled(true);

 HttpResponse response = uploader.upload(new GoogleUrl(sURL));
 if (!response.isSuccessStatusCode()) { // Gets that the upload was successful
            throw new Exception("Error");
 }

我错过了什么??:/

4

1 回答 1

0

我有同样的问题,你有。看来,MediaHttpUploader 没有设置有效的标头。

这里适用于我的代码

GoogleUrl url = new GoogleUrl("resumable upload link ");

GoogleHeaders headers = new GoogleHeaders();
headers.setSlugFromFileName("my image.jpg");
headers.set("X-Upload-Content-Type", "image/jpeg");
headers.set("X-Upload-Content-Length", content.getLength());
//without this I got the same error
headers.gdataVersion = "3";

MediaHttpUploader uploader = new MediaHttpUploader(content, getTransport(), getRequestFactory().getInitializer());
uploader.setInitiationMethod(HttpMethod.POST);
uploader.setInitiationHeaders(headers);
uploader.upload(url);
于 2012-09-20T09:54:58.447 回答