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 ...
随机推荐
- 【python】-- IO多路复用(select、poll、epoll)介绍及实现
IO多路复用(select.poll.epoll)介绍及select.epoll的实现 IO多路复用中包括 select.pool.epoll,这些都属于同步,还不属于异步 一.IO多路复用介绍 1. ...
- 开发者眼中的Spring与JavaEE
JavaEE与Spring 在Java社区中,Spring与Java EE之争是个永恒的话题.在这场争论中,来自两个阵营的布道师.架构师 与铁杆粉丝都在不遗余力地捍卫着本方的尊严,并试图说服对方加入到 ...
- Django多对多的创建
1.多对多创建的应用场景: 在某表中创建一行数据是,有一个可以多选的下拉框 例如:创建用户信息,需要为用户指定多个爱好 2.创建方式: 方式一:自定义关系表,手动创建一张表用于关联其他多张表的关系 c ...
- Java中的String,StringBuilder,StringBuffer三者的区别(转发:https://www.cnblogs.com/su-feng/p/6659064.html)
最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...
- 3.07课·········if分支语句
语句分类:顺序语句,选择语句(分支语句),循环语句 分支语句:(一)if(表达式) //表达式返回值是True或False{}说明:1.表达式返回的是bool值:2.小括号和花括号后面不需要加分号. ...
- hadoop自带例子SecondarySort源码分析MapReduce原理
这里分析MapReduce原理并没用WordCount,目前没用过hadoop也没接触过大数据,感觉,只是感觉,在项目中,如果真的用到了MapReduce那待排序的肯定会更加实用. 先贴上源码 pac ...
- runtime-分类为什么不生成setter和getter
前言 前几天有人问我一个问题:为什么分类不能自动创建get set方法.老实说,笔者从来没有去思考过这个问题.于是这次通过代码实践跟runtime源码来探究这个问题. 准备工作 为了能减少输出类数据的 ...
- python3 mysql 多表查询
python3 mysql 多表查询 一.准备表 创建二张表: company.employee company.department #建表 create table department( id ...
- mini2440移植uboot 2014.04(四)
我修改的代码已经上传到github上,地址:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440.git 参考文章: <mini2440移植u ...
- 什么是shell【TLCL】
常用命令 date cal df——report file system disk space usage free——display amount of free and used memory i ...