anandsekar272 / google-api-java-client

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

EOFException when performing request #869

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Version of google-api-java-client : google-http-client-1.17.0-rc

Java environment: Android 4.3

Describe the problem.

During certain requests I receive the following:

04-09 12:52:31.937      I/ApiClient REQUEST: POST : /api/stream/start body: {
    "mediaType" : {
    "parameters" : {
    "charset" : "UTF-8"
    },
    "subType" : "x-www-form-urlencoded",
    "type" : "application",
    "charsetParameter" : "java.nio.charset.CharsetICU[UTF-8]"
    },
    "data" : {
    "uuid" : "6ecd6zd8-0452-4ff9-96be-341d8168d7a9",
    "private" : false,
    "title" : "04/09/2014 12:52 PM",
    "description" : "Example Description",
    "extra_info" : "{'foo': 'bar'}"
    },
    "length" : 159,
    "type" : "application/x-www-form-urlencoded; charset=UTF-8"
    }
04-09 12:52:32.107      W/ApiClient Unhandled Error: java.io.EOFException 
cannot be cast to com.google.api.client.http.HttpResponseException. Stack trace 
follows:
04-09 12:52:32.107      W/System.err﹕ java.io.EOFException
04-09 12:52:32.117      W/System.err﹕ at 
libcore.io.Streams.readAsciiLine(Streams.java:203)
04-09 12:52:32.117      W/System.err﹕ at 
libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)
04-09 12:52:32.117      W/System.err﹕ at 
libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827)
04-09 12:52:32.127      W/System.err﹕ at 
libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:28
3)
04-09 12:52:32.137      W/System.err﹕ at 
libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.jav
a:497)
04-09 12:52:32.147      W/System.err﹕ at 
libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.j
ava:134)
04-09 12:52:32.147      W/System.err﹕ at 
com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:3
6)
04-09 12:52:32.147      W/System.err﹕ at 
com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:94
)
04-09 12:52:32.147      W/System.err﹕ at 
com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
04-09 12:52:32.157      W/System.err﹕ at 
io.kickflip.sdk.api.KickflipApiClient.request(KickflipApiClient.java:367)
04-09 12:52:32.157      W/System.err﹕ at 
io.kickflip.sdk.api.KickflipApiClient.access$300(KickflipApiClient.java:43)
04-09 12:52:32.157      W/System.err﹕ at 
io.kickflip.sdk.api.KickflipApiClient$3.onSuccess(KickflipApiClient.java:342)
04-09 12:52:32.157      W/System.err﹕ at 
io.kickflip.sdk.api.OAuthClient$1.run(OAuthClient.java:103)
04-09 12:52:32.157      W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

How would you expect it to be fixed?

Group consensus on StackOverflow seems to suggest this is due to connection 
recycling. Setting a property on the HttpUrlConnection may address this issue:

see 
http://stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexcept
ion

Original issue reported on code.google.com by davidpbr...@gmail.com on 9 Apr 2014 at 8:02

GoogleCodeExporter commented 9 years ago
This seems to only happen when using NetHttpTransport [1]. Since you are on 
Android 4.3, you are recommended to use the ApacheHttpTransport, which you 
should get by default if you call AndroidHttp.newCompatibleTransport() [2]. 
NetHttpTransport depends on the old javanet library and is less preferable when 
other alternatives exist.

[1] http://stackoverflow.com/a/21618255/3882262
[2] 
https://code.google.com/p/google-http-java-client/source/browse/google-http-clie
nt-android/src/main/java/com/google/api/client/extensions/android/http/AndroidHt
tp.java

Original comment by wonder...@google.com on 29 Dec 2014 at 10:19

GoogleCodeExporter commented 9 years ago
Appreciate the update.

If I read correctly, AndroidHttp.newCompatibleTransport() returns 
NetHttpTransport, not ApacheHttpTransport if the Android API version exceeds 9 
(i.e 18 for Android 4.3).

public static HttpTransport newCompatibleTransport() {
    return AndroidUtils.isMinimumSdkLevel(9) ? new NetHttpTransport() : new ApacheHttpTransport();
  }

[2] states:

There is no guarantee that Apache HTTP transport will continue to work in 
future SDKs. Therefore, this method uses {@link NetHttpTransport} for 
Gingerbread or higher, and otherwise {@link ApacheHttpTransport}.

To be crystal clear on Google's recommendation: Avoid NetHttpTransport in all 
cases? Only where Android API < 9? Only where Android API < 18?

Original comment by davidpbr...@gmail.com on 13 Jan 2015 at 8:46

GoogleCodeExporter commented 9 years ago
Sorry I was mistaken. On platforms other than Android, ApacheHttpTransport is 
preferred over NetHttpTransport. But Android doesn't seem to get along with 
ApacheHttpTransport so you'd have to use NetHttpTransport. Plus, Android's 
HttpUrlConnection's implementation is better than JDK's so it's not too bad.

Using NetHttpTransport, you can manipulate request properties by overriding 
ConnectionFactory#openConnection(URL), in which you open a connection and set 
the property before returning.

https://github.com/google/google-http-java-client/blob/master/google-http-client
/src/main/java/com/google/api/client/http/javanet/ConnectionFactory.java

Original comment by wonder...@google.com on 1 Mar 2015 at 11:50