android getresponsecode -1,getHttpResponseCode()在android 2.2中返回-1
在我的
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.
我没有关闭连接只是为了保持连接活着.我究竟做错了什么?
android getresponsecode -1,getHttpResponseCode()在android 2.2中返回-1相关推荐
- android 代码获取图片信息吗,Android 通过网络获取图片的代码
Android 通过网络获取图片的代码 主activity package com.netimg; import android.app.Activity; import android.graphi ...
- android网络请求分析工具,android网络数据请求
网络数据请求在android中应用很广泛,也很重要,所以我记录了上课讲的获取数据: 大家都知道,网络通信,发送请求有两种方式,GET和POST,这里也不例外. 1.HttpURLConnection的 ...
- android通知栏应用程序更新,Android App自动更新之通知栏下载
本文实例为大家分享了Android App自动更新通知栏下载的具体代码,供大家参考,具体内容如下 版本更新说明 这里有调用UpdateService启动服务检查下载安装包等 1. 文件下载,下完后写入 ...
- Android开发基础:利用 Android Studio开发看美女应用(4)
利用 Android Studio开发看美女应用 今天我们会利用 Android Studio开发一个简单的看美女应用,通过它查看互联网上的美女图片.通过本篇文章,你将会学到Android应用简单开发 ...
- convert android layout xml,详解Android之解析XML文件三种方式(DOM,PULL,SAX)
1.xml文件代码 ${fq.content} ${fq.time} 2.XML网页效果图 3.Android代码 1.布局文件 xmlns:tools="http://schemas.an ...
- android 9 pie公司,谷歌Android 9 Pie,真正的安卓派
IT之家8月7日消息 今天谷歌正式宣布了Android 9 Pie正式版系统,此前的Android P终于定名为Android Pie,该更新首先面向谷歌Pixel设备和Essential Phone ...
- android控件触摸缩放,Android控件之ZoomControls缩放使用
先看一下效果 正常 缩小 放大 一.简介 ZoomControls是一组可缩放的控件.它包含俩个按钮(放大按钮.缩小按钮) 二.重要方法 hasFocus():判断焦点 hide():隐藏 onTou ...
- android平台 arcgisr_ArcGIS Runtime For Android 开发 (7)
第七课 图层管理 在前边的课程中,我们学习了如何加载图层,符号化图层,那么如果一个项目里边包含了很多的图层,但又需要进行查看特定的图层,怎么办?是的,我们需要对加载的图层进行管理. 那么如何来实现图层 ...
- android列表实现置顶,Android利用RecyclerView实现全选、置顶和拖拽功能示例
Android利用RecyclerView实现全选.置顶和拖拽功能示例 发布时间:2020-08-23 16:26:42 来源:脚本之家 阅读:159 作者:爱开发 前言 今天给大家分享是如何在Rec ...
最新文章
- docker上传自己的镜像
- 第五届中国企业服务年会:洞见2022数智化的“光与火”
- CVPR 2019 | 旷视研究院提出新型损失函数:改善边界框模糊问题
- QT4 自定义槽和信号
- http get extension information - another way to get host url and port number of current application
- zookeeper 负载_ZooKeeper,策展人以及微服务负载平衡的工作方式
- 前端学习(702):while循环
- 20190808面试记录
- openairinterface 中手动安装编译 UHD, Ubuntu 16.04
- 【sklearn第七讲】数据集加载工具
- 最新手机号段 归属地数据库(2021年10月476338条,包括最新的号段)
- 【代码之路】我和代码的这两年,一路风雨,终见彩虹
- MYSQL union 联合查询
- MVP是什么,不是什么
- 用C语言实现求水仙花数
- Geany全黑主题首发教程
- 如何实现vue表单验证cron表达式?【亲测有效】
- javascript时间差插件
- Flink入门教程(三)——窗口(一)
- 2007你“值”多少钱(薪水,薪酬,工资,待遇,年薪,月薪)