超时不在AsyncTask类中工作

时间:2014-06-17 06:46:59

标签: android android-asynctask

超时似乎不适用于异步类

int timeoutConnection = 3000;
url=urls[0].getUrl();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urls[0].getUrl());
HttpConnectionParams.setConnectionTimeout(httppost.getParams(), timeoutConnection);
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httppost.getParams(), timeoutSocket);
httppost.setEntity(new UrlEncodedFormEntity(urls[0].getParameters()));
// Execute HTTP Post Request
HttpResponse responsePOST = httpclient.execute(httppost);
resEntity = responsePOST.getEntity();
response=EntityUtils.toString(resEntity);

请帮助我如何在AsyncTask课程中设定时间?

2 个答案:

答案 0 :(得分:0)

尝试this

// Set connection timeout
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httppost.getParams(), timeoutConnection);
// set socket timeout
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httppost.getParams(), timeoutSocket);

// Create the client
HttpClient httpclient = new DefaultHttpClient();
url=urls[0].getUrl();

// Create the post
HttpPost httppost = new HttpPost(urls[0].getUrl());
httppost.setEntity(new UrlEncodedFormEntity(urls[0].getParameters()));
// Execute HTTP Post Request and get response
HttpResponse responsePOST = httpclient.execute(httppost);
resEntity = responsePOST.getEntity();
response=EntityUtils.toString(resEntity);

答案 1 :(得分:0)

您必须将参数分配给HttpClient。

请看这篇文章;

https://stackoverflow.com/a/1565243/233048