2012-06-14 74 views
1

我正尝试使用Android上的文档列表API将文件上传到Google文档/云端硬盘。上传到Google文档的文件不显示

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

我尝试了多种方法,但W/O运气好的话,例如:

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,但相同结果(在这个例子中,我设置的元数据,但我认为我得到一个异常的内容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

你确定该文件没有被上传。有时它可能不会出现在用户界面的主要Google云端硬盘Feed中。你可以尝试使用文件的名称,并使用搜索框进行搜索,以确保? – Nivco

+0

嗨,你能告诉我用于这个代码的库吗?谢谢... – user4232

回答

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); 
+0

嗨,你能告诉我用于此代码的库吗?谢谢... – user4232

+0

我一直在使用google-api-client(http://code.google.com/p/google-api-java-client/)版本1.7.0测试版。有些方法在google-api-client的更多上升版本中可能会有所不同。 – user1660016

+0

感谢您的回复... – user4232