android模拟器timeout,Android-setSoTimeout无法正常工作

所以我遇到一个无法正常工作的套接字超时.我按照现有帖子给出的所有说明进行操作,但是它仍然不起作用(我从未收到套接字超时异常).这是我的代码:

AsyncTask task = new AsyncTask() {

@Override

protected String doInBackground(String... params) {

String location = params[0];

try {

HttpGet httpGet = new HttpGet(location);

HttpParams httpParameters = new BasicHttpParams();

// Set the timeout in milliseconds until a connection is established.

// The default value is zero, that means the timeout is not used.

int timeoutConnection = 0;

HttpConnectionParams.setConnectionTimeout(httpParameters,

timeoutConnection);

// Set the default socket timeout (SO_TIMEOUT)

// in milliseconds which is the timeout for waiting for data.

int timeoutSocket = 2000;

HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient client = new DefaultHttpClient(httpParameters);

Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");

HttpResponse response = client.execute(httpGet);

Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");

if (statusLine.getStatusCode() == HttpStatus.SC_OK) {

ByteArrayOutputStream out = new ByteArrayOutputStream();

response.getEntity().writeTo(out);

return new Scanner(out.toString()).useDelimiter("\A").next();

} else {

return "";

}

} catch (IOException e) {

e.printStackTrace();

return null;

} catch (URISyntaxException e) {

e.printStackTrace();

return null;

}

}

@Override

protected void onPostExecute(String result) {

try {

// doStuff

} catch (Exception e) {

e.printStackTrace();

}

}

};

task.execute(location);

因此,我应该在两秒钟后收到一个套接字超时异常,但是客户端只是忽略了这一点.有什么帮助吗?

提前致谢

所以这是所有代码:

public void fetch(String location) {

AsyncTask task = new AsyncTask() {

@Override

protected String doInBackground(String... params) {

String location = params[0];

try {

HttpGet httpGet = new HttpGet(location);

HttpParams httpParameters = new BasicHttpParams();

// Set the timeout in milliseconds until a connection is established.

// The default value is zero, that means the timeout is not used.

int timeoutConnection = 0;

HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

// Set the default socket timeout (SO_TIMEOUT)

// in milliseconds which is the timeout for waiting for data.

int timeoutSocket = 2000;

HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient client = new DefaultHttpClient(httpParameters);

Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");

HttpResponse response = client.execute(httpGet);

Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");

if (statusLine.getStatusCode() == HttpStatus.SC_OK) {

ByteArrayOutputStream out = new ByteArrayOutputStream();

response.getEntity().writeTo(out);

return new Scanner(out.toString()).useDelimiter("\A").next();

} else {

return "";

}

} catch (IOException e) {

e.printStackTrace();

return null;

} catch (URISyntaxException e) {

e.printStackTrace();

return null;

}

}

@Override

protected void onPostExecute(String result) {

try {

//doStuff

}

} catch (Exception e) {

e.printStackTrace();

}

}

};

task.execute(location);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值