我正在尝试使用Android上的Documents List API将文件上传到Google文档/云端硬盘。
问题是它似乎没问题(我得到状态代码200,OK),但是在查看我的文档时文件没有出现。上传到Feed时列出的地址: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");
}
我错过了什么? :/
答案 0 :(得分: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);