在Async Task中设置计时器以进行webservice响应。计时器完成后显示Alrt框。
请帮我怎么做,
我搜索了这个,但没有得到任何满意的答案。
提前致谢
我的一些代码是:
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));
}
}
答案 0 :(得分:0)
private Timer timer = new Timer();
private final long DELAY = 500; // in ms
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
//TODO alert dialog code
}
});
}
}, DELAY);
我不太确定你是否在寻找这个。请验证。 如果它没有回答你的问题。请分享一些代码。
答案 1 :(得分:0)
请尝试此代码......
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;
}
}
答案 2 :(得分:0)
如果您使用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 */
答案 3 :(得分:0)
您应该设置连接和套接字超时并处理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,然后显示超时警告对话框。
如果您设置了计时器并且发生了超时并且您显示了警告对话框,那么这是错误的方法,因为您实际上并未关闭连接,因此最终您的连接仍将处于活动状态。