在我的 android 应用程序中,我试图通过执行 POST 请求从服务器中提取数据。
我正在使用HttpURLConnection
类来发出请求,因为 ApacheHttpClient
不再由 android 维护。
这就是我正在做的事情。
private boolean callWS() {
try {
// To avoid the bug in httpurlconnection prior froyo which
// causes the getInputStream to return headers along with response
if (Build.VERSION.SDK_INT < 8)
System.setProperty("http.keepAlive", "false");
mHttpResponseCode = 0;
mErrorMessage = "";
// Initialize connection
URL connectURL = new URL(mServerUrl);
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
// Set some headers
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept-Encoding", "deflate, gzip");
connection.setRequestProperty("Content-Length", mParameters.length() + "");
// Connect to host
conn.connect();
// Write parameters to connection
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(mParameters);
writer.flush();
writer.close();
// Wait for http response code
mHttpResponseCode = conn.getResponseCode();
// Read response from connection
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int read = 0;
int bufSize = 1024;
byte[] buffer = new byte[bufSize];
while (true) {
read = bis.read(buffer);
if (read == -1)
break;
baf.append(buffer, 0, read);
}
// Decompress gzipped response
if (conn.getHeaderField("Content-Encoding") != null && conn.getHeaderField("Content-Encoding").contains("gzip"))
mResponseString = decompress(baf.toByteArray());
else
mResponseString = new String(baf.toByteArray());
mResponse.setResponse(mResponseString);
isWSCallSuccessfull = true;
} catch(UnknownHostException unknownHostException) {
isWSCallSuccessfull = false;
mErrorMessage = "Unknown host exception";
unknownHostException.printStackTrace();
mLogger.putStacktrace(unknownHostException);
} catch(SocketException socketException) {
isWSCallSuccessfull = false;
mErrorMessage = "Socket Exception";
socketException.printStackTrace();
mLogger.putStacktrace(socketException);
} catch(SocketTimeoutException socketTimeOutException) {
isWSCallSuccessfull = false;
mErrorMessage = "Socket Timeout Exception";
socketTimeOutException.printStackTrace();
mLogger.putStacktrace(socketTimeOutException);
} catch(SSLException sslException) {
isWSCallSuccessfull = false;
mErrorMessage = "SSL Exception";
sslException.printStackTrace();
mLogger.putStacktrace(sslException);
} catch(IOException ioException) {
isWSCallSuccessfull = false;
mErrorMessage = "IO Exception " + ioException.getMessage();
ioException.printStackTrace();
mLogger.putStacktrace(ioException);
}
mResponse.setHttpResponseCode(mHttpResponseCode);
mResponse.setErrorMessage(mErrorMessage);
mResponse.isWSCallSuccessful(isWSCallSuccessfull);
return isWSCallSuccessfull;
}
除了运行 2.2 的设备(没有在 2.1 上尝试过)之外,这在所有设备上都可以正常工作。
在 2.2 中,它工作正常。但是如果我让这部分代码闲置超过 30 秒,它会在下一次返回 -1 作为 http 响应代码。
另一件需要注意的是,这仅发生在 HTTPS 网址上,而不是 HTTP 网址。我不想使用 HttpsURLConnection 类,因为有时我可能也想使用 http。
我不会关闭连接只是为了保持连接处于活动状态。我究竟做错了什么?