通用类的AsyncTask在Android的?AsyncTask、Android

2023-09-12 21:31:19 作者:死不足惜

我有一个共同的类地说,例如A级延伸的AsyncTask ,并已实施,即所有方法上preExecute doinbackground onPostExecute

I have a common class say for eg Class A which extends AsyncTask and has all the methods implemented i.e. onPreExecute, doinbackground and onPostExecute.

现在,有一些希望使用类其他类的对象。

Now, there are other classes which want to use Class A object.

说B级采用A级在下面的方式

Say Class B uses class A in the below manner

A a = new A(context)
a.execute(url)

然后我取结果get方法。但得到的方法是不使用的AsyncTask的正确方法。我想获得的结果 onPostExecute 。对于我试图用一个布尔参数,该参数将得到真正的只在 onpostexecute 。 B类将检查,直到它变得真实而当它得到真正的将取回结果。

Then i fetch the result in get method. But get method is not the proper way of using AsyncTask. I will like to get the result in onPostExecute. For that i tried using a boolean parameter which will get true only in onpostexecute. The class B will check till it gets true and when it gets true it will fetch the result.

但是,这在某种程度上阻止该应用程序。

But this is somehow blocking the application.

我已经放在code下面AsyncTask的。

I have placed the code for asynctask below.

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;
private static boolean isResult = false;

private ProgressDialog progressDialog = null; 

public BabbleVilleSyncTask(Context context)
{
    this.context = context; 
    progressDialog = new ProgressDialog(this.context);
}

protected void onPreExecute() 
{
    progressDialog.setMessage("Please Wait....");
    progressDialog.show();
}

protected String doInBackground(String... urls) 
{
    try 
    {
        //urls[0] = URLEncoder.encode(urls[0], "UTF-8");

        HttpGet httpget = new HttpGet(urls[0]);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        content = httpClient.execute(httpget, responseHandler);
    }
    /*catch(UnsupportedEncodingException ue)
    {
        error = ue.getMessage();
    }*/
    catch (ClientProtocolException e) 
    {
        //error = e.getMessage();
        cancel(true);
    }
    catch (IOException e) 
    {
        //error = e.getMessage();
        cancel(true);
    }

    httpClient.getConnectionManager().shutdown();

    return content;
}

protected void onPostExecute(String result) 
{
    finalResult = result;
    progressDialog.dismiss();
    System.out.println("on Post execute called");
    isResult = true;
}  

public boolean getIsResult()
{
    return isResult;
}

public void setIsResult(boolean flag)
{
    isResult = flag;
}

public String getResult()
{
    return finalResult;
}
}

有人可以让我知道这个问题可能?

Can someone let me know what the issue may be?

问候

苏尼尔

推荐答案

一个干净的方式来使用的AsyncTask得到的结果是使用一个回调接口。

A clean way to use AsyncTask to get a result would be to use a callback interface.

下面是这个概念的一个简单的例子:

Here is a simple example of this concept:

interface AsyncTaskCompleteListener<T> {
   public void onTaskComplete(T result);
}

然后在B级:

then in your B class :

class B implements AsyncTaskCompleteListener<String> {

    public void onTaskComplete(String result) {
        // do whatever you need
    }

    public void launchTask(String url) {
        A a = new A(context, this);
        a.execute(url);
    }
}

您现在应该添加以下code到你的A类:

you should now add the following code to your A class:

class A extends AsyncTask<String, Void, String> {
    private AsyncTaskCompleteListener<String> callback;

    public A(Context context, AsyncTaskCompleteListener<String> cb) {
        this.context = context;
        this.callback = cb;
    }

    protected void onPostExecute(String result) {
       finalResult = result;
       progressDialog.dismiss();
       System.out.println("on Post execute called");
       callback.onTaskComplete(result);
   }  
}

这种方式,你不需要等待明确地为您的任务来完成,而不是,你的主要code(这可能是在主UI线程),正在等待正常的android事件循环,而onTaskComplete方法会被自动调用,使处理任务结果出现。

This way, you don't need to wait explicitely for your task to complete, instead, your main code (which is probably the main UI thread), is waiting in the normal android event loop, and the onTaskComplete method will be automatically called, allowing to handle the task result there.

 
精彩推荐
图片推荐