0

I'm using the following code. (ServiceResponse is an object just to wrap the response)

protected ServiceResponse executeHttpPostRequest(URL url,
        Map<String, String> parameters) throws IOException {
    String charset = "utf-8";
    Set<String> keys = parameters.keySet();
    StringBuffer query = new StringBuffer();
    for (String key : keys) {
        query.append(key);
        query.append("=");
        query.append(URLEncoder.encode(parameters.get(key), charset));
        query.append("&");
    }
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    if (Build.VERSION.SDK_INT > 13) {
        connection.setRequestProperty("Connection", "close");
    }
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(0);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset=" + charset);
    connection.setRequestProperty("Content-Length",
            Integer.toString(query.length()));

    OutputStream output = connection.getOutputStream();
    output.write(query.toString().getBytes(charset));

    ServiceResponse response = new ServiceResponse();
    response.setHttpCode(connection.getResponseCode());
    if (connection.getResponseCode() == 200 && connection.getContentLength() > 0) {
        response.setInputStream(connection.getInputStream());
    }

    output.close();

    return response;
}

I got the EOFException when calling HttpURLConnection.getResopnseCode, or HttpURLConnection.getContentLength and server has responded only with HTTP Code 200 but no content. I use the exact method on other service calls, that have body response (JSON) and works fine. I saw similar posts here and tried to apply the fixes without luck (that's why you can see "connection" extremely set up). I'm also using System.setProperty("http.keepAlive", "false"); just in case.

UPDATE

This is the stacktrace

05-07 14:16:09.275: E/com.foo.android.InternalService(19594): java.io.EOFException
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:206)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:541)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:844)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at com.foo.android.net.BaseServiceCall.executeHttpPostRequest(BaseServiceCall.java:100)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at com.foo.android.InternalService.authentitate(InternalService.java:95)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at com.foo.android.ui.LauncherActivity$1.work(LauncherActivity.java:70)
05-07 14:16:09.275: E/com.foo.android.InternalService(19594):   at com.coredroid.util.BackgroundTask$BackgroundThread.run(BackgroundTask.java:74)
fr4gus
  • 396
  • 7
  • 18
  • HttpURLConnection.getContentLength() doesn't throw EOFExceptions. Post the stacktrace. – koral May 07 '13 at 19:57
  • @droid Just added the stacktrace. – fr4gus May 07 '13 at 20:22
  • It seems that server sets `Content-Encoding: gzip` but does not return valid GZIP data. What is returned by getContentLength() ? Call it before getResponseCode(). – koral May 07 '13 at 21:43

0 Answers0