artiya4u / google-http-java-client

Automatically exported from code.google.com/p/google-http-java-client
0 stars 0 forks source link

HTTP Post Request Exception Occured with second request (Unexpected end of stream) #245

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Version of google-http-java-client (e.g. 1.15.0-rc)?
 1.17.0-rc version

Java environment (e.g. Java 6, Android 2.3, App Engine)?
Android 4.1.2

Describe the problem.
I got a exception when I request a Http Post request with second request.
My code is 
 HttpRequestFactory requestFactory =
                            HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
                                @Override
                                public void initialize(HttpRequest request) {
                                    request.setParser(new JsonObjectParser(JSON_FACTORY));
                                    request.getHeaders().set("http.keepAlive", "false");
                                }
                            });

                    BulkUploadUrl url = BulkUploadUrl.uploadUrl();

                    byte [] sendBytes = new Gson().toJson(logs).getBytes();
                    Log.d(TAG, String.format("send data %d bytes-----", sendBytes.length));
                    System.setProperty("http.keepAlive", "false");

                    HttpContent content = new ByteArrayContent("application/json", sendBytes);
                    UploadResponse uRes = null;
                    try {
                        HttpRequest request = requestFactory.buildPostRequest( url, content );

                        HttpResponse response = request.execute();

                        uRes = response.parseAs(UploadResponse.class);

                        response.disconnect();
                    } catch (IOException e) {
                        Log.e(TAG, e.getMessage(), e);
                        // report to crashlytics
                        Crashlytics.logException(e);

                    } catch(Exception e) {
                        Log.e(TAG, e.getMessage(), e);
                        // report to crashlytics
                        Crashlytics.logException(e);
                    }

But first request ends up successfully...
In second request( HttpRequest.execute() ) I got a exception below.

09-16 14:58:23.061  12139-13628/com.a2t.aipreferences E/BulkUploadService﹕ 
unexpected end of stream
        java.io.IOException: unexpected end of stream
        at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:81)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
        at com.a2t.aipreferences.service.BulkDeviceLogUploadService$1.doInBackground(BulkUploadService.java:91)
        at com.a2t.aipreferences.service.BulkDeviceLogUploadService$1.doInBackground(BulkUploadService.java:64)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)

How would you expect it to be fixed?

Original issue reported on code.google.com by wishop...@gmail.com on 16 Sep 2013 at 6:16