我有一个共同的阶级说的如A级延伸AsyncTask,并已实施即所有的方法onPreExecute,doinbackground和onPostExecute.
现在,还有其他类想要使用A类对象.
假设B类以下面的方式使用A类
A a = new A(context)
a.execute(url)
Run Code Online (Sandbox Code Playgroud)
然后我在get方法中获取结果.但是get方法不是使用AsyncTask的正确方法.我想得到结果onPostExecute.为此,我尝试使用一个布尔参数,该参数仅在onpostexecute.B类将检查直到它变为真,当它变为真时它将获取结果.
但这在某种程度上阻止了应用程序.
我已经在下面放置了asynctask的代码.
"
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
public class A extends AsyncTask<String, Void, String>
{
private Context context = null;
private final HttpClient httpClient = new DefaultHttpClient();
private String content = null;
//private String error = null;
private String finalResult = null; …Run Code Online (Sandbox Code Playgroud) 我的Android应用程序连接到我的网站以检索和上传信息,因此我使用AsyncTask线程.
在一个实例中,我需要我的线程将true或false值返回给我的主线程.
有没有办法从AsyncTask执行函数获取此返回值?
当我执行以下操作时:
Toast.makeText(Locate.this, "Testing : "+locationUpdate.execute(location), Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)
我只是得到了很多胡言乱语.
我认为我需要的是暂停主线程直到第二个线程完成的方法.第二个线程调用主线程中的函数来设置我的返回值.所以当第二个线程完成时,主线程可以取消暂停并访问第二个线程设置的返回值. 如果这个逻辑是合理的,请提供建议......谢谢!
回答完这个问题之后,我对使用Android的AsyncTask类的get()方法的意义/用处产生了疑问.
public final Result get ()
Waits if necessary for the computation to complete, and then retrieves its result.
Run Code Online (Sandbox Code Playgroud)
基本上,AsyncTask类的同步解决方案,阻止(冻结)UI直到后台操作完成.
除了测试目的,甚至在那些情况下,我无法真正想到它实际上是一个很好的解决方案,但我可能是错的,所以我感到好奇.
如果您需要用户实际等到AsyncTask完成,您可以显示Dialog或ProgressDialog,在每个时刻都可以控制UI.我知道它不完全相同,但恕我直言,它比使用该get()方法更好的方法.
我试图解析一些JSON数据.我的代码工作了一段时间,我不知道我改变了什么突然破坏了代码.当我运行我的代码时,我没有收到任何运行时错误或警告.我创建一个新的AsyncTask并执行它.当我调用.get()这个新任务时,调试器在这一行停止并花费超过30分钟.我无法获得调试器或在运行期间完成此任务.
JSON:
protected void setUp(Context context) {
_context = context;
getConfig();
}
// get config file
protected void getConfig() {
if (config != null)
return;
config = new Config();
String url = configURL;
AsyncTask<String, Integer, JSONObject> jsonTask = new DownloadJSONTask()
.execute(url);
JSONObject configItem = null;
try {
configItem = jsonTask.get(); //debugger pauses here
if (configItem == null)
return;
config.configVersion = configItem.getString("field_configversion");
config.currentAppVersion = configItem
.getString("field_currentappversion");
config.getSupportURL = configItem.getString("field_getsupporturl");
config.getCatalogURL = configItem.getString("field_getcatalogurl");
config.getDataVersion = configItem.getString("field_dataversion");
config.getDataUrl = configItem.getString("field_dataurl");
config.getDataApiKey …Run Code Online (Sandbox Code Playgroud) 我的AsyncTask阻塞块按钮元素,下载图像和进度对话框显示延迟 - 它显示图像显示一段时间,但下载需要很长时间,按钮被阻止(橙色),对话框不显示.
public Bitmap download(String url, ProgressBar progressbar) throws InterruptedException, ExecutionException {
BitmapDownloaderTask task = new BitmapDownloaderTask(progressbar);
task.execute(url);
return task.get();
}
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
public BitmapDownloaderTask(ProgressBar progressbar) {
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(ShowActivity.this);
dialog.setMessage("Loading");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Bitmap doInBackground(String... Params) {
return imageLoader.getBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
dialog.dismiss();
}
}
Run Code Online (Sandbox Code Playgroud)
在按钮监听器中,只需调用下载功能,进度参数是因为我在imageview中有进度条圆 - 该对话框仅用于测试,找到为什么有延迟和阻塞.在另一个应用程序中我使用runable并且线程和元素没有被阻止,但是在教程中AsyncTask被提到作为更好的解决方案.
List完成后我有6种不同类型的结果AsyncTask.并且List结果应该返回到Activity.例如:List<A>,List<B>,List<C>,List<D>,List<E>和最后List<F>.
这是我的AsyncTask:
public class myAsync extends AsyncTask<String, String, List> {
private List resultList;
@Override
protected List doInBackground(String... params) {
//params[0] is url
//params[1] is type
callAPI api = new callAPI(params[0], params[1]);
// According to type api decides type of List and generates
return api.getJSON(); // returns a List which already parse JSON
}
@Override
protected void onPostExecute(List result) { …Run Code Online (Sandbox Code Playgroud)