Android Httpsurlconnection Eofexception

HttpsUrlConnection EOFException

Try to change your code like this:

    conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset: UTF-8");
byte[] output = data.getBytes("UTF-8");
conn.setFixedLengthStreamingMode(output.length);

os = conn.getOutputStream();
os.write(output);
os.flush();
os.close();

DisplayResponseHeaders(conn);

if (conn.getResponseCode() == 200) { // or other 2xx code like 204

s = readStream(conn.getInputStream());
Log.d("body", s);
}
else {

// handle error conditions like 404, 400, 500, ...

// now it may be necessary to read the error stream

InputStream errorStream = conn.getErrorStream();

// ...
}

AFAIK you should always close all streams you opened. I'm not sure whether conn.disconnect() is doing that for you.

If you want to code your HTTP(S) requests more conveniently, you can have a look at DavidWebb where you have a list of libraries helping you to avoid using cumbersome HttpURLConnection.

Android HttpsUrlConnection eofexception

Someone requested I answer my own question instead of editing. So here is the answer again.

This was not a well documented answer. It appears in some of the newer versions of android, there is a bug with recycled url connections. To fix this (although there may be some performance issues), I needed to add:

if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
urlConnect.setRequestProperty("Connection", "close");
}

Android HttpUrlConnection EOFException

Our conclusion is that there is an issue in the Android platform. Our workaround was to catch the EOFException and retry the request N number of times. Below is the pseudo code:

private static final int MAX_RETRIES = 3;

private ResponseType fetchResult(RequestType request) {
return fetchResult(request, 0);
}

private ResponseType fetchResult(RequestType request, int reentryCount) {
try {
// attempt to execute request
} catch (EOFException e) {
if (reentryCount < MAX_RETRIES) {
fetchResult(request, reentryCount + 1);
}
}
// continue processing response
}

Android EOFException when using HttpURLConnection headers

Looks like it may be a problem with certain versions of Android according to this question.

Try adding conn.setRequestProperty("Connection", "close") to your request.

Android's HttpURLConnection throws EOFException on HEAD requests

Turned out this is a known bug in Android's class implementation. Calling Connection.setRequestProperty( "Accept-Encoding", "" ); before connecting can be used as workaround.

https://code.google.com/p/android/issues/detail?id=24672

Android HttpUrlConnection EOFException on internet

I think you can check response code. If it's not 200 (OK), getInputStream perhaps returns null, then use getErrorStream. Hope this help since I met the same issue yesterday.

            urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpStatus.SC_OK) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
}


Related Topics



Leave a reply



Submit