我使用以下代码。 (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 Code 200但没有内容。我在其他服务调用上使用了确切的方法,它具有正文响应(JSON)并且工作正常。我在这里看到了类似的帖子,试图在没有运气的情况下应用这些修补程序(这就是为什么你可以看到&#34;连接&#34;极其设置)。我还使用 System.setProperty(&#34; http.keepAlive&#34;,&#34; false&#34;); 以防万一。
更新
这是堆栈跟踪
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)