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设置超时的更多相关文章

  1. android httpclient 设置超时

    3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...

  2. Android中使用HttpGet和HttpPost访问HTTP资源

    需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 public String getResultFo ...

  3. HttpClient 如何设置超时时间

    今天分享一个巨坑,就是 HttpClient.这玩意有多坑呢?就是每个版本都变,近日笔者深受其害. 先看一下代码,我要发送请求调用一个c++接口. public static String doPos ...

  4. CloseableHttpClient设置超时

    Java开发我们常常需要和第三方系统进行通信,通信的方式有多种,如dubbo方式,webservice,微服务和CloseableHttpClient等方式,常涉及到超时问题,这里主要说的是Close ...

  5. HTTPClient模块的HttpGet和HttpPost

    HttpClient常用HttpGet和HttpPost这两个类,分别对应Get方式和Post方式. 无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源. 1.创 ...

  6. HttpClient库设置超时

    HttpClient库API跟Lucene一样,每个版本的API都变化很大,这有点让人头疼.就好比创建一个HttpClient对象吧,每一个版本的都不一样. 3.X是正常的Java语法 HttpCli ...

  7. httpClient创建对象、设置超时

    从老版本和新版本进行比较说明: 1.创建HttpClient对象 3.X: HttpClient httpClient = new DefaultHttpClient(); 4.3: Closeabl ...

  8. Java实现HttpGet和HttpPost请求

    maven引入JSON处理jar <dependency> <groupId>com.alibaba</groupId> <artifactId>fas ...

  9. 【转】Android Studio-1.2版本设置教程

    如果重新安装Android Studio的话要重新配置风格选项啥的,这篇是个很好的教程,原文链接:http://blog.csdn.net/skykingf/article/details/45485 ...

随机推荐

  1. oracle 如何完全删除干净

    在安装oracle的时候如果出现了,指定的SID在本机上已经存在.这样的报错的话.这边你肯定是第二次在安装你的oracle了,这里出现这样的错误是你没有吧原先的那些关于oracle的东西给清理干净,这 ...

  2. 信息搜集之google语法

    总结的比较全,无耻的转了.D: http://blog.csdn.net/chaosa/article/details/1828301 说起Google,可谓无人不知无人不晓.作为世界第一的搜索引擎, ...

  3. 【python】-- 信号量(Semaphore)、event(红绿灯例子)

    信号量(Semaphore) 之前讲的线程锁(互斥锁) 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里 ...

  4. Django 之 ModelForm 组件

    Django的model form组件 扩展:Django 之Form组件 首先我们要知道 Model 和 Form 分别时干什么的 Model  生成表数据 Form  对表单.字段进行校验 Dja ...

  5. spring AOP理解和相关术语

    一.AOP理解 AOP:横向抽取机制,底层使用代理方式实现. 示例: 现有LogDAO接口以及实现Log接口的Log类.类有add的方法,现在要打印add方法的开始时间和结束时间.(即增强Log的ad ...

  6. 关于ionic开发中遇到的坑与总结

    这次是第二次使用ionic开发混合app,今天算是对这个框架做一个总结,基础的我就不再重复了,网上都有教程.我就说说自己的心得和遇见的各种坑, 之后会陆续补充,想到什么说什么吧. 1.关于ionic效 ...

  7. 【leetcode刷题笔记】Substring with Concatenation of All Words

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  8. 小学生都能看懂的数位dp

    前言 数位dp其实很久前就知道了,也做过几道和其他算法混在一起的题目,其实通过手玩是能做的 但毕竟是种算法,还是系统学下比较好(节省手玩时间) 模板题 P2602 [ZJOI2010]数字计数 化简题 ...

  9. 在Delphi2007下安装ReportMachine6.5

    如何在Delphi2007下安装ReportMachine6.5: 一.在安装ReportMachine6.5之前要安装如下组件: (1).llPdfLib3.6,用于导出PDF文件: (2).Ehl ...

  10. Struts2 内核之我见

    Struts2 内核之我见 完整分析 Struts2 内核中文文档 本文首先探讨了 Struts2 核心控制器的源码,以帮助解读 Struts2 的工作流程.接着讲解相关外围类.最后对 Struts ...