uploading csv with gdata - Content-Length error
I'm attempting to use the gdata project from an android app. I'm attempting to upload a new csv file to google docs, but I keep encountering a 411 error (Content-Length). My code looks something like:
GoogleService ss = new SpreadsheetService("testApp");
ss.setUserCredentials("<username>", "<password>开发者_JAVA技巧;");
DocumentListEntry newEntry = null;
newEntry = new DocumentListEntry();
newEntry.setTitle(new PlainTextConstruct("my.csv"));
TextConstruct tc = TextConstruct.plainText("1,2,3,4,5,6,7,8,9,10");
newEntry.setContent(new TextContent(tc));
DocumentListEntry res = ss.insert(new URL("https://docs.google.com/feeds/default/private/full/"), newEntry);
Since the GData lib is abstracting the network calls from me I assume that I don't need to set the Content-Length myself, which leads to me believe I'm simply not using the lib correctly.
What am I missing? Thanks.
The content of the file should not be set in the metadata but sent along the metadata using an HTTP multipart request. The client library takes care of doing that and you can set the content using:
newEntry.setFile(/* java.io.File instance */ file, /* MIME type */ "text/csv");
This requires to load the data from a file, but you can use streams to load from memory. A more detailed example can be found in the Java client library project page.
精彩评论