0

我正在使用以下代码。(ServiceResponse 是一个仅用于包装响应的对象)

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;
}

我在调用 HttpURLConnection.getResopnseCode 或 HttpURLConnection.getContentLength 时收到 EOFException,并且服务器仅响应 HTTP 代码 200 但没有内容。我在其他具有正文响应 (JSON) 并且工作正常的服务调用上使用了确切的方法。我在这里看到了类似的帖子,并试图在没有运气的情况下应用修复程序(这就是为什么你可以看到“连接”设置得非常好)。我也在使用System.setProperty("http.keepAlive", "false"); 以防万一。

更新

这是堆栈跟踪

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)
4

0 回答 0