Android 如何在没有get()方法的情况下将AsyncTask回调的返回值正确地传递给另一个类?
在我的应用程序中,我需要调用web服务从internet获取一些数据。为了避免阻塞UI,我使用AsyncTask来实现这一点。但我在正确传递返回值以供进一步使用时遇到问题。我想我在这里犯了一些建筑错误。这就是我的应用程序的结构: MainActivity:包含文本视图、按钮和SoapParser的活动,请参见下文。我想通过按下按钮调用web服务,解析结果并在TextView中显示它 SoapRequestTask:一个具有回调机制的AsyncTask,如。doInBackground的返回值是一个SoapObject,并在onPostExecute中作为回调方法listener.ontaskCompletedsOAPOObject中的参数传递给侦听器 SoapRequestManager:一个类,它应该有一个公共SoapObject makeRequestsomeParams方法。这个方法应该使用AsyncTask并从doInBackground返回SoapObject MySpecialRequestManager:扩展了SoapRequestManager,并使用不同的方法调用web服务以获得不同的结果。这些方法都使用超类的makeRequestsomeParams SoapParser:一个将SoapObject解析为字符串的类,以便它可以显示在我的活动的TextView中。它应该有一个类似于公共字符串parseResultsOAPOObject的方法Android 如何在没有get()方法的情况下将AsyncTask回调的返回值正确地传递给另一个类?,android,android-asynctask,callback,return-value,Android,Android Asynctask,Callback,Return Value,在我的应用程序中,我需要调用web服务从internet获取一些数据。为了避免阻塞UI,我使用AsyncTask来实现这一点。但我在正确传递返回值以供进一步使用时遇到问题。我想我在这里犯了一些建筑错误。这就是我的应用程序的结构: MainActivity:包含文本视图、按钮和SoapParser的活动,请参见下文。我想通过按下按钮调用web服务,解析结果并在TextView中显示它 SoapRequestTask:一个具有回调机制的AsyncTask,如。doInBackground的返回值是一
如何正确使用回调和返回值?哪个类应该实现回调接口?只是一个简单的片段,因为您需要一些代码。这可能会帮助你开始 在SoapRequestTask中的AsyncTask的postExecute方法中
Intent intent = new Intent("send_data");
intent.setAction("SEND_DATA"); // don't NEED this, as we implicitly
// declare our intent during initialization
intent.putExtra("data_return_key", mData); // where mData is what you want to return
sendBroadcast(intent);
那么对于SoapRequestManager
public class SoapRequestManager implements BroadcastReceiver{
int mData; // assuming our data is an int in this example
// Default constructor
public SoapRequestManager(){
registerReceiver(this, new IntentFilter("send_data"));
// if you use setAction use the below registration instead
// registerReceiver(this, new IntentFilter("SEND_DATA"));
}
//... All of the stuff you have in the class already
@Override
public void onReceive(Context context, Intent intent){
// For this example let's say mData is an integer value
// if you use setAction, check that the intent is the correct one via
// if (intent.getAction().equals("SEND_DATA"))
mData = intent.getIntExtra("data_return_key");
}
}
让SoapRequestManager实现BroadcastReceiver并发送一个广播,该广播包含一个包含有问题数据的包。@zgc7009您能给我一个代码示例吗?