Android App Receives '411 Length Required' Error from Google Docs API on Older Versions

I’m making an Android app that lets users sync files with Google Docs. It works fine on Android 2.3.3 (API 10) emulators, but on real devices and emulators running Android 2.2.x (API 8) or lower, I get a ‘411 Length Required’ error when trying to start a resumable upload.

I’m using Eclipse and Google API Java Client 1.4.1-beta. The problem happens when sending an empty POST request to start the upload session. Here’s how I set up the headers:

GoogleHeaders headers = new GoogleHeaders();
headers.contentLength = "0";
headers.gdataVersion = "3";
headers.setGoogleLogin(authToken);
headers.setSlugFromFileName(file.getName());
headers.setApplicationName("MyApp");
headers.set("X-Upload-Content-Length", file.length());
headers.set("X-Upload-Content-Type", getFileType(file));
request.headers = headers;

Packet sniffing shows that older Android versions don’t set the Content-Length header. I’ve tried different ways to fix this, like:

transport = new NetHttpTransport();
transport.defaultHeaders.contentLength = "0";

But nothing works. Any ideas on how to solve this across all Android versions? Could it be a bug in the Google API or Android itself?

hav u tried using HttpURLConnection directly? it mite give u more control over the headers. also, double-check ur API client version - maybe theres a newer one that fixes this. could be worth testing with different content-length values too, like ‘0’ vs 0 (no quotes). good luck!

This issue sounds frustrating. Have you considered implementing a version check in your app? You could detect the Android version at runtime and adjust your header-setting logic accordingly. For older versions, you might need to explicitly set the Content-Length header using a different method.

Another approach worth exploring is using OkHttp library instead of the default HttpURLConnection. OkHttp provides more consistent behavior across different Android versions and might handle this header issue more gracefully.

If all else fails, you may need to implement a fallback mechanism for older Android versions, perhaps using a different upload method that doesn’t rely on the resumable upload feature. It’s not ideal, but it could be a temporary workaround while you investigate further.

I’ve encountered similar issues when working with older Android versions. One thing that helped me was using Apache HttpClient instead of HttpURLConnection. It offers more granular control over headers and tends to be more consistent across Android versions.

Here’s a snippet that worked for me:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uploadUrl);
httpPost.setHeader("Content-Length", "0");
httpPost.setHeader("GData-Version", "3");
// Add other headers as needed

HttpResponse response = httpClient.execute(httpPost);

This approach bypasses some of the quirks in the default Android networking stack. Just remember to handle the response properly and close connections when done.

Also, consider setting a minimum API level for your app if supporting very old Android versions becomes too cumbersome. Most users are on newer versions anyway.