Android 异步任务计时器
在webservice responce的异步任务中设置计时器。并在计时器完成后显示Alrt框 请帮我怎么做 我寻找这个,但没有得到任何满意的答案 提前谢谢 我的一些代码是:Android 异步任务计时器,android,asynchronous,timer,Android,Asynchronous,Timer,在webservice responce的异步任务中设置计时器。并在计时器完成后显示Alrt框 请帮我怎么做 我寻找这个,但没有得到任何满意的答案 提前谢谢 我的一些代码是: private class TestTask extends AsyncTask<Integer, Void, Boolean> { private long time; @Override protected void onPreExecute() {
private class TestTask extends AsyncTask<Integer, Void, Boolean> {
private long time;
@Override
protected void onPreExecute() {
super.onPreExecute();
time = System.currentTimeMillis();
}
@Override
protected Boolean doInBackground(Integer... params) {
try {
// Callint Web service
String final_link = "";
final_link = Method_All.removeSpace(final_link);
Log.d("Bhavik", " " + final_link);
try {
HttpGet get = new HttpGet(new URI(final_link));
Log.d("Parser", "XML Get");
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
// If Time Difference is 1 minut Show Alert Box.
Log.d("TestTask1", "Currenttime = " + (System.currentTimeMillis()));
Log.d("TestTask2", "time = "+ time);
Log.d("TestTask3", "difftime = "
+ (System.currentTimeMillis() - time));
}
}
我不太确定你是否在哪里找这个。请验证。
如果它没有回答你的问题。请共享一些代码。请尝试此代码
public class MyWebservice extends AsyncTask {
String wSResponse = "";
@Override
protected String doInBackground(String... params) {
/*starting an individual thread for checking
webservice response after specified time */
new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
this.wait(your response time in milliss);
}
if(wSResponse.equals("")){
//display alert box and your logic
}
}
}).start();
// call web service
wSResponse = webServiceCall(your parameter);
return null;
}
}
如果您使用的是http请求,请也尝试一下这个
URL URLObj = null;
HttpURLConnection ConnObj = null;
URLObj = new URL("Http://Url.to.the/webservice");
ConnObj = (HttpURLConnection) URLObj .openConnection();
ConnObj .setConnectTimeout(5000);
/* This will set the desired time out for the connection request */
您应该设置连接和套接字超时,并处理SocketTimeoutException
try{
HttpGet httpGet = new HttpGet(url);
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 = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
}catch (SocketTimeoutException e){
return false;
}catch (Exception e){
return false;
}
return true;
现在,当连接或套接字超时发生时,将引发SocketTimeoutException,并将在catch块中进行处理,因此从那里可以返回false,在onPostExecute中可以检查结果是否为false,然后显示超时警报对话框
如果您设置了计时器,并且超时发生,并且您显示了一个警报对话框,那么这是错误的做法,因为最终您的连接将仍然处于活动状态,因为您实际上没有关闭连接。@PiYusHGuPtA我很抱歉
try{
HttpGet httpGet = new HttpGet(url);
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 = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
}catch (SocketTimeoutException e){
return false;
}catch (Exception e){
return false;
}
return true;