在我的Android应用程序中,我试图通过执行POST请求从服务器提取数据。
我正在使用HttpURLConnection
类来发出请求,因为Apache的HttpClient
不再由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。
我没有关闭连接只是为了保持连接活着。我究竟做错了什么?
答案 0 :(得分:0)
如果您想同时使用Https和Http并且不想创建单独的连接 - 如果HttpsUrlConnection
解决了您的“ - 1问题”,您可以使用以下方法:
URLConnection conn = new URL(url).openConnection();
if (conn instanceof HttpsURLConnection) {
// do stuff with cast to HttpsUrlConection
}
else {
// do stuff with cast to HttpUrlConnection
}
我将this回答作为参考