android连接服务 超时时间,android 中http请求的超时异常

private class XmlAsyncLoader extends XmlResourceRequest { private

boolean mIsCancle = false; private HttpGet mGet; private HttpClient mHttp; public

XmlAsyncLoader(MxActivity>

activity, String url)

throws MalformedURLException { super(activity, url);

}

@Override protected void doTaskInBackground() { // 请求数据 if (mUrl.toLowerCase().startsWith("http://")) {

mGet = initHttpGet(mUrl);

mHttp = initHttp(); try {

HttpResponse response =

mHttp.execute(mGet);

if (mIsCancle) { return;

} if (response != null)

{ if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){

onResponseError("network

error");

Log.v(TAG, "the code is :"+response.getStatusLine().getStatusCode());

return;

}

notifyUpdateProgress(70);

Document doc =

getDocumet(response);

Element root =

doc.getDocumentElement();

NodeList appList =

root

.getElementsByTagName_r(Item_ELEMENT_NAME); final int len

= appList.getLength(); if (len

<= 0)

{// 没有items onFoundNoItems(); return;

} for (int

i = 0; i

< len; i++) {

Element item =

(Element)

appList.item(i); if (item.getNodeType() == Node.ELEMENT_NODE) {

HahaItemInfo info =

createHahaItemIno(item);

if (mIsCancle){ return;

}

onFoundItem(info, 80

+ 20 *

(i + 1)

/ len);

addUrlToQueue(info.userIconUrl);

}

};

}

}catch(ConnectTimeoutException e){

onResponseError("time

out");

} catch (ClientProtocolException e) {

--mCurrentPage;

e.printStackTrace();

} catch (IOException e) { --mCurrentPage;

e.printStackTrace();

} catch (XmlPullParserException e) {

--mCurrentPage;

e.printStackTrace();

}finally{

notifyLoadFinish();

notifyLoadImages();

mHttp.getConnectionManager().shutdown();

}

}

} private

HttpClient initHttp() {

HttpClient client =

new DefaultHttpClient();

client.getParams().setIntParameter(

HttpConnectionParams.SO_TIMEOUT, TIME_OUT_DELAY);

// 超时设置 client.getParams().setIntParameter(

HttpConnectionParams.CONNECTION_TIMEOUT,

TIME_OUT_DELAY);//

连接超时 return client;

} private

HttpGet initHttpGet(String mUrl)

{

HttpGet get =

new HttpGet(mUrl);

initHeader(get); return get;

}

@Override public

boolean tryCancel() {

Log.i(TAG, "tryCanle is working");

mGet.abort();

mIsCancle = true;

mHttp.getConnectionManager().shutdown();

notifyLoadFinish(); return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值