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. php解析xml文件为数组

    $xml = simplexml_load_file($fullfilename); $arr = json_decode(json_encode($xml),true); echo "&l ...

  2. - symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library icu has the wrong version installed or is missing from your system, ma

    $ composer install Loading composer repositories with package information Installing dependencies (i ...

  3. 解决oracle锁表

    1.查看被锁住的表select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects ...

  4. Bootstrap学习1--响应式导航栏

    备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html <nav class="navbar n ...

  5. iOS base64编码 MD5 加密

    //创建一个Base64编码的NSString对象 //字符串 转二进制 NSData *nsdata = [@"iOS Developer Tips encoded in Base64&q ...

  6. 重新认识Java中的程序入口即主函数各组成部分

    主函数各组成部分深入理解 public static void main(String[] agrs) 主函数:是一个特殊的函数,作为程序的入口,可以被JVM调用 主函数的定义: public:代表着 ...

  7. systemverilog FAQ(zz)

    1. What is clocking block? Ans: Clocking block can be declared using the keywords clocking and endcl ...

  8. 20145229吴姗珊 《Java程序设计》第7周学习总结

    20145229吴姗珊 <Java程序设计>第7周学习总结 教材学习内容总结 第13章时间与日期 即使标注为GMT(格林威治时间),实际上谈到的的是UTC(Unix时间)时间. 秒的单位定 ...

  9. jQuery学习(2)

    <script type="text/javascript"> //给网页中所有的<p>元素添加onclick事件 $("p").cli ...

  10. Fireworks(whole page)

    <!DOCTYPE HTML> <html> <head> <title>Canvas 实现放烟花特效</title> <meta c ...