寻找使用GET()与机器人的的AsyncTask的好榜样好榜样、机器人、GET、AsyncTask

由网友(感同身兽)分享简介:我很好奇中的AsyncTask的 GET(长,java.util.concurrent.TimeUnit中)的功能,但我有一个很难定位的例子它的用法。 I'm curious about the get(long, java.util.concurrent.TimeUnit) function in AsyncTas...

我很好奇中的AsyncTask的 GET(长,java.util.concurrent.TimeUnit中)的功能,但我有一个很难定位的例子它的用法。

I'm curious about the get(long, java.util.concurrent.TimeUnit) function in AsyncTask, but I'm having a hard time locating an example of it's usage.

get(long, java.util.concurrent.TimeUnit中)

任何人都可以提供一个例子是使用?

Can anyone provide an example of it's use?

推荐答案

看来好像AsyncTask.get()阻塞调用者线程,其中AsyncTask.execute()才不是。

It appears as though AsyncTask.get() blocks the caller thread, where AsyncTask.execute() does not.

您可能需要使用 AsyncTask.get()测试情况下,您希望测试特定的Web服务调用,但你并不需要它是异步的,你想控制需要多长时间才能完成。或者,任何时候你想测试的Web服务中的一个测试套件。

You might want to use AsyncTask.get() for test cases where you want to test a particular Web Service call, but you do not need it to be asynchronous and you would like to control how long it takes to complete. Or any time you would like to test against your web service in a test suite.

语法是一样的执行:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}

new DownloadFilesTask().get(5000, TimeUnit.MILLISECONDS);
阅读全文

相关推荐

最新文章