Unable to Upload PDF Document in Android Platform

495 views
Skip to first unread message

kunalkfp

unread,
Mar 29, 2012, 2:30:20 AM3/29/12
to google-docum...@googlegroups.com
Hello, I m trying to integrate the Google Docs API in android with Google-API-java-client-1.6.0 for the first time in android, and i have integrate it successfully. I got the List of files from account, I can Create the file with metadata only, but i couldn't upload the file.

Actually i m trying to upload the PDF file that is of around 1.2 MB, I first send the initial POST request that give me response as 200 OK, but after that when i send first chunk of data with the same request i used for initial request just by changing the method as put and URL as the next location, i got the following error on request.execute().


W/System.err(11656): org.apache.http.client.ClientProtocolException
W/System.err(11656):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
W/System.err(11656):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err(11656):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err(11656):     at com.google.api.client.http.apache.ApacheHttpRequest.execute(ApacheHttpRequest.java:58)
W/System.err(11656):     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:639)
W/System.err(11656):     at com.android.example.googledoc.SampleLoginActivity.uploadDocs(SampleLoginActivity.java:268)
W/System.err(11656):     at com.android.example.googledoc.SampleLoginActivity.onClick(SampleLoginActivity.java:115)
W/System.err(11656):     at android.view.View.performClick(View.java:2408)
W/System.err(11656):     at android.view.View$PerformClick.run(View.java:8816)
W/System.err(11656):     at android.os.Handler.handleCallback(Handler.java:587)
W/System.err(11656):     at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(11656):     at android.os.Looper.loop(Looper.java:123)
W/System.err(11656):     at android.app.ActivityThread.main(ActivityThread.java:4633)
W/System.err(11656):     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(11656):     at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(11656):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
W/System.err(11656):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err(11656):     at dalvik.system.NativeStart.main(Native Method)
W/System.err(11656): Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
W/System.err(11656):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:402)
W/System.err(11656):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err(11656):     ... 17 more


I have attached the source code.t's almost one week i m trying to resolve my problem but couldn't actually find it.

Any kind of help will be appreciated.
Thanks in Advance
SampleLoginActivity.java

Ali Afshar

unread,
Mar 29, 2012, 10:52:58 AM3/29/12
to google-docum...@googlegroups.com
Hi Kunal,

That is not exactly correct to change the initial request by changing the URL and the method. Please check our docs [1] as you need to set some headers too, e.g. X-Upload-Content-Type: application/pdf.

Please get back to us once you have done that if you are still having issues.

Regards

--
Ali Afshar | www.googplus.org/ali | Google Developer Relations

Message has been deleted

kunalkfp

unread,
Mar 30, 2012, 1:42:49 AM3/30/12
to google-docum...@googlegroups.com
Though by building new put request and and setting the necessary headers values i m still getting the same Error as i have described previously.for more information please go through my code for uploading document as following:

static void uploadDocs() {
        File sdcard = Environment.
getExternalStorageDirectory();
        File file = new File(sdcard.getAbsolutePath() + "/download/iridium.pdf");

        InputStream stream = null;
        HttpRequest request;
        try {
            stream = new FileInputStream(file);
            InputStreamContent streamContent = new InputStreamContent(
                    "application/pdf", stream);
            request = rf.buildPostRequest(DocsUrl.forCreate(), streamContent);

            GoogleHeaders headers = (GoogleHeaders) request.getHeaders();
            headers.setSlugFromFileName("iridium.pdf");
            headers.set("X-Upload-Content-Length", file.length());
            headers.set("X-Upload-Content-Type", "application/pdf");
            headers.setContentType("application/pdf");

            request.setHeaders(headers);
            HttpResponse response = request.execute();
            String upUrl = response.getHeaders().getLocation();

            headers=null;
            response = null;
            long i = 0, j = 0;
            while (i < file.length()) {
                j = i + 524287;
                DocsUrl objDocsUrl = DocsUrl.forUpload(upUrl);

                HttpRequest rptrequest = rf.buildPutRequest(objDocsUrl,
                        streamContent);
                headers = (GoogleHeaders)rptrequest.getHeaders();
                if (j > file.length()) {
                    headers.setRange(
                            "bytes " + String.valueOf(i) + "-"
                                    + String.valueOf(file.length()) + "/"
                                    + String.valueOf(file.length()));
                } else {
                    headers.setRange(
                            "bytes " + String.valueOf(i) + "-"
                                    + String.valueOf(j) + "/"
                                    + String.valueOf(file.length()));

                    i = j + 1;
                }               
                headers.setSlugFromFileName("iridium.pdf");
                headers.set("X-Upload-Content-Type", "application/pdf");
                headers.setContentType("application/pdf");
               
                rptrequest.setHeaders(headers);
                response = rptrequest.execute();
                upUrl = response.getHeaders().getLocation();
                rptrequest = null;
                headers = null;
                response = null;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
Reply all
Reply to author
Forward
0 new messages