猿问

Android中AsyncTask的通用类?

我有一个共同的阶级说的如A级延伸AsyncTask,并已实施即所有的方法onPreExecute,doinbackground和onPostExecute。


现在,还有其他一些要使用Class A对象的类。


假设B类以以下方式使用A类


A a = new A(context)

a.execute(url)

然后我在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;

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;

}


有人可以让我知道可能是什么问题吗?


月关宝盒
浏览 439回答 3
3回答

慕雪6442864

使用AsyncTask获得结果的一种干净方法是使用回调接口。这是此概念的一个简单示例:interface AsyncTaskCompleteListener<T> {&nbsp; &nbsp;public void onTaskComplete(T result);}然后在你的B班上:class B implements AsyncTaskCompleteListener<String> {&nbsp; &nbsp; public void onTaskComplete(String result) {&nbsp; &nbsp; &nbsp; &nbsp; // do whatever you need&nbsp; &nbsp; }&nbsp; &nbsp; public void launchTask(String url) {&nbsp; &nbsp; &nbsp; &nbsp; A a = new A(context, this);&nbsp; &nbsp; &nbsp; &nbsp; a.execute(url);&nbsp; &nbsp; }}您现在应该将以下代码添加到您的A类中:class A extends AsyncTask<String, Void, String> {&nbsp; &nbsp; private AsyncTaskCompleteListener<String> callback;&nbsp; &nbsp; public A(Context context, AsyncTaskCompleteListener<String> cb) {&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; this.callback = cb;&nbsp; &nbsp; }&nbsp; &nbsp; protected void onPostExecute(String result) {&nbsp; &nbsp; &nbsp; &nbsp;finalResult = result;&nbsp; &nbsp; &nbsp; &nbsp;progressDialog.dismiss();&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("on Post execute called");&nbsp; &nbsp; &nbsp; &nbsp;callback.onTaskComplete(result);&nbsp; &nbsp;}&nbsp;&nbsp;}这样,您无需显式地等待任务完成,而是您的主代码(可能是主UI线程)在正常的android事件循环中等待,并且onTaskComplete方法将被自动调用,允许在那里处理任务结果。

达令说

你能再详细点吗?我目前正在尝试转换此方法:public static int invokeWebServiceRequest(String uri,String username,String password,String targetKey);&nbsp;在扩展AsyncTask的类中。我如何将那些参数(从旧方法中)传递给doInBackground(Params ...)?谢谢!
随时随地看视频慕课网APP

相关分类

Android
我要回答