android getresponsecode -1,getHttpResponseCode()在android 2.2中返回-1

在Android应用中,使用HttpURLConnection进行POST请求时遇到一个问题:在2.2及以上版本的设备上,如果请求闲置超过30秒,下次请求会返回-1作为HTTP响应代码,仅在HTTPS网址上出现此问题。开发者不想使用HttpsURLConnection,因为可能也需要处理HTTP请求。代码检查了连接设置、超时和压缩响应,但问题仍然存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我的

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.

我没有关闭连接只是为了保持连接活着.我究竟做错了什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值