Android HttpGet和HttpPost设置超时
HttpPost:
private Runnable runnable = new Runnable() {
@Override
public void run() {
String url = BaseServicesInfo.SERVER_BASE_PATH + fileName;
HttpPost httpRequest = new HttpPost(url);
try{
HttpEntity entity = new UrlEncodedFormEntity(params);
httpRequest.setEntity(entity);
HttpClient client = new DefaultHttpClient();
// 请求超时
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
// 读取超时
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000 );
HttpResponse response = client.execute(httpRequest);
if(response.getStatusLine().getStatusCode() == 200){
str = EntityUtils.toString(response.getEntity());
Message msg = new Message();
Bundle data = new Bundle();
data.putString("value", str);
msg.setData(data);
handler.sendMessage(msg);
}else{
str = String.valueOf(response.getStatusLine().getStatusCode());
Message msg = new Message();
Bundle data = new Bundle();
data.putString("error", str);
msg.setData(data);
handler.sendMessage(msg);
}
}catch(Exception e){
e.printStackTrace();
Message msg = new Message();
Bundle data = new Bundle();
data.putString("error", str);
msg.setData(data);
handler.sendMessage(msg);
}
}
};
HttpGet:
Runnable runnable = new Runnable() {
@Override
public void run() {
HttpURLConnection conn = null;
InputStream inputStream = null;
try {
URL url = new URL(baseUrl);
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(8000);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "text/html");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
inputStream = conn.getInputStream();
byte[] buffer = null;
if(conn.getResponseCode() == 200){
buffer = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
while ((len = inputStream.read(buffer)) != -1)
{
out.write(buffer, 0, len);
}
buffer = out.toByteArray();
}
mCallback.HandleAsync(buffer);
SendMsg("value",buffer);
} catch (Exception e) {
e.printStackTrace();
Log.e("sjr","Network-error");
}
finally{
try {
if(inputStream != null){
inputStream.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
Log.e("sjr","InvokeWebServiceHelper类中释放资源出错");
}
}
}
};
Android HttpGet和HttpPost设置超时的更多相关文章
- android httpclient 设置超时
3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...
- Android中使用HttpGet和HttpPost访问HTTP资源
需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 public String getResultFo ...
- HttpClient 如何设置超时时间
今天分享一个巨坑,就是 HttpClient.这玩意有多坑呢?就是每个版本都变,近日笔者深受其害. 先看一下代码,我要发送请求调用一个c++接口. public static String doPos ...
- CloseableHttpClient设置超时
Java开发我们常常需要和第三方系统进行通信,通信的方式有多种,如dubbo方式,webservice,微服务和CloseableHttpClient等方式,常涉及到超时问题,这里主要说的是Close ...
- HTTPClient模块的HttpGet和HttpPost
HttpClient常用HttpGet和HttpPost这两个类,分别对应Get方式和Post方式. 无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源. 1.创 ...
- HttpClient库设置超时
HttpClient库API跟Lucene一样,每个版本的API都变化很大,这有点让人头疼.就好比创建一个HttpClient对象吧,每一个版本的都不一样. 3.X是正常的Java语法 HttpCli ...
- httpClient创建对象、设置超时
从老版本和新版本进行比较说明: 1.创建HttpClient对象 3.X: HttpClient httpClient = new DefaultHttpClient(); 4.3: Closeabl ...
- Java实现HttpGet和HttpPost请求
maven引入JSON处理jar <dependency> <groupId>com.alibaba</groupId> <artifactId>fas ...
- 【转】Android Studio-1.2版本设置教程
如果重新安装Android Studio的话要重新配置风格选项啥的,这篇是个很好的教程,原文链接:http://blog.csdn.net/skykingf/article/details/45485 ...
随机推荐
- 配置过滤器filter对跨站脚本攻击XSS实现拦截
1.web.xml中配置filter [html] view plain copy <filter> <filter-name></filter-name> & ...
- wecenter 问答社区 dockerfile,不用纠结于物理机的运行环境
FROM webdevops/php-nginx:centos-7-php56 ADD . /app RUN ["chmod", "777", "/a ...
- SAP初始账号
方法1:有其中某Client的登录帐号1. 用已有帐号登录某个Client2. 运行Tcode SE303. 单击“tips and tricks“按钮4. 在Performance Tips an ...
- C调用Lua中的函数解析table
Passing Tables to Lua Functions A use case that happens often is the passing of tables to and from L ...
- C#DataSet/DataAdapter
DataReader必须持续连接,所以在调用方法SqlDataReader作为返回类型时候,必须在方法外关闭流,很不方便. DataAdapter用于对数据源检索数据并填充到DataSet中的表.Da ...
- Luogu-2600 [ZJOI2008]瞭望塔
把地面看成半平面,能看到所有位置的点所在的区域即为半平面的交 因为分段函数的极值只会在转折处或边界取到,所以对于半平面上和地面上的每一个交点都求一下距离就好了 #include<cmath> ...
- g++能过,c++过不了
可能原因: 1.在递归的时候,递归函数中忘记加返回return.(详见Wrong Answer,Memory Limit Exceeded) 代码1:错误 g++--------accepted c+ ...
- python代码docstring生成文档之sphinx
在使用python中,我们一般在模块,类,函数下使用docstring添加字符串说明性文档,使开发人员更好的可以看懂此代码是做什么用的.然而写了那么多的注释,我们想要一篇文档怎么办,第一种办法不可能将 ...
- html设置编码
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- jQuery购物数量数字加减运算效果
<a href="###" id="add" value="+">+</a> <input type=&quo ...