安卓中处理http超时
来源:互联网 发布:千牛淘宝客服斗图 编辑:程序博客网 时间:2024/05/29 07:44
http超时限制可以用一下两句:
HttpConnectionParams.setConnectionTimeout(post.getParams(), CONNECTION_TIOMOUT); HttpConnectionParams.setSoTimeout(post.getParams(), CONNECTION_TIOMOUT);但是这只能限定连接超时和soket超时,对于dns查找超时和传输内容过多而导致的超时是无能为力的。
应付这些超时,可以使用一条额外的超时检查线程来把超时的http请求线程强制interrupt。
调用httpPostRequest的线程会等待网络线程完成,若网络线程在限定时间内完成请求,则调用notify唤醒等待线程。若网络线程超时,检查线程调用interrupt,中止网络线程,同时调用notify唤醒等待线程。
public static String httpPostRequest(String url, JSONObject jsonObject ) { try { final HttpClient httpClient = new DefaultHttpClient(); final HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "application/json"); post.setEntity(new StringEntity(jsonObject.toString())); HttpConnectionParams.setConnectionTimeout(post.getParams(), CONNECTION_TIOMOUT); HttpConnectionParams.setSoTimeout(post.getParams(), CONNECTION_TIOMOUT); HttpExecutehread th = new HttpExecutehread(httpClient, post); th.start(); new Timer(true).schedule(new CheckTimeOutTask(th), OVERALL_TIOMOUT); synchronized (th) { th.wait(); } LogUtils.d(TAG, "after waite " + th); if (th.mHttpResponse != null && th.mHttpResponse.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(th.mHttpResponse.getEntity(), "utf-8"); LogUtils.d(TAG, "get post result"); return result; } } catch (Exception e) { e.printStackTrace(); } return null; } private static class CheckTimeOutTask extends TimerTask { private HttpExecutehread th; public CheckTimeOutTask(HttpExecutehread th) { this.th = th; } @Override public void run() { if (th != null && th.mHttpResponse == null) { th.interrupt(); synchronized (th) { th.notify(); } LogUtils.d(TAG, "post thread " + th + " interrupt and notify"); } } } private static class HttpExecutehread extends Thread { private HttpPost mHttpPost; private HttpClient mHttpClient; public HttpResponse mHttpResponse; public HttpExecutehread(HttpClient httpClient, HttpPost httpPost) { mHttpClient = httpClient; mHttpPost = httpPost; } @Override public void run() { try { mHttpResponse = mHttpClient.execute(mHttpPost); } catch (IllegalArgumentException ec) { LogUtils.d(TAG, ec.toString()); interrupted(); } catch (ClientProtocolException e) { LogUtils.d(TAG, e.toString()); interrupted(); } catch (IOException e) { LogUtils.d(TAG, e.toString()); interrupted(); }finally { LogUtils.d(TAG, "post notify"); synchronized (this) { notify(); } } } }
0 0
- 安卓中处理http超时
- java中处理http连接超时
- java中处理http连接超时
- 处理http连接超时的方法
- Http请求通过线程添加超时处理
- 针对http接口访问超时处理
- 简单的重试技术来处理HTTP链接超时
- java中处理http连接超时的方法
- java中处理http连接超时的方法
- org.apache.http.client.httpclient4.1.3 超时处理
- Nodejs HTTP请求的超时处理 Nodejs HTTP Client Request Timeout Handle
- HTTP 设置超时
- HTTp 请求json 超时
- Android HTTP连接超时
- http 超时设置
- Http超时设置
- http超时设置
- HTTP网络超时
- 四种解析方法
- 谈谈最近OLCS项目的开发感受
- Aizu 2538 Stack Maze【记忆化搜索】
- 接口
- 时间复杂度和空间复杂度
- 安卓中处理http超时
- stm8-discovery uart2 iar实现
- 表情列表发送功能
- haar 特征计算
- 初识 SyntaxHighlighter 代码高亮控件
- gcc与g++
- MFC 截图操作详解。
- windows环境下如何区分单击和双击
- 写程序过程中的积累,不断更新。。。