【问题标题】:need to return value from AsyncTask [duplicate]需要从 AsyncTask 返回值 [重复]
【发布时间】:2013-06-14 22:26:43
【问题描述】:

我正在调用soap webservice,需要显示返回的内容。但我不能这样做,因为 AsyncTask 很复杂,我不知道如何正确使用它。请告诉我如何通过 asynctask 从被调用函数返回数据?

这是我的代码

public class WebserviceTool {

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://192.168.0.11:9289/Service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/get_currency";
private final String METHOD_NAME = "get_currency";

public static void main(String[] args) {
    // TODO Auto-generated method stub
}

public String execute_barcode_webservice(String s1, String s2) {
    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("date",s1);
    request.addProperty("cur_code",s2);
    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.encodingStyle = SoapEnvelope.ENC;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            Object response;
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        response = (Object) envelope.getResponse();
        Log.i("my_error", response.toString());
    } catch (Exception e) {
        Log.i("my_error", e.getMessage().toString());
    }
    return "testarif";
}

public class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        try {
            execute_barcode_webservice(params[0], params[1]);
        } catch (Exception e) {
            // TODO: handle exception
        }   
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }
}
}

这是执行所有工作并返回数据的函数 execute_barcode_webservice()。但由于我调用 execute_barcode_webservice() 视图 AsyncTask,我不知道如何返回它。我该怎么做?

【问题讨论】:

标签: java android web-services android-asynctask


【解决方案1】:

异步任务执行的结果是execute_barcode_webservice()产生的response对象。但是,不要将异步任务视为将执行并向您返回值的东西。相反,在 onPostExecute() 方法中,您必须获取 response 对象并对其进行处理(提取其值并将它们显示在列表中,或者您想用它做什么)。异步任务只是在单独的线程中执行一些代码然后返回主线程(UI线程)并处理结果的一种方式,这是在onPostExecute()中完成的。

我的建议:重写 execute_barcode_webservice() 以返回 response 对象而不是 String(如果操作失败,则可以是 null 的对象)并将该对象传递给 onPostExecute() 方法。您必须将异步任务更改为:

public class AsyncCallWS extends AsyncTask<String, Void, Object> {
    @Override
    protected Object doInBackground(String... params) {
        Object response = null;
        try {
            response = execute_barcode_webservice(params[0], params[1]);
        } catch (Exception e) {
            // TODO: handle exception
        }   
        return response;
    }

    @Override
    protected void onPostExecute(Object response) {
        if (response != null) {
            // display results in a list or something else
        }
    }

【讨论】:

  • 在doinbackground函数中,你返回的是object,但是返回类型是void
  • @ayilmaz:谢谢,我已经修复了代码。看,你已经掌握了这个概念!
  • 如何在 onPostExecute() 中显示返回的数据?我想返回调用 asynctask 的数据
  • 那么,为什么在这种情况下需要使用 AsyncTask 呢?
  • “我想返回调用 asynctask 的数据” - 这不是一个好主意,因为您必须等待异步任务执行才能获得数据(使用 while() 表示例如),这违反了并行执行的要点。无论您打算对结果数据做什么,都可以在onPostExecute()doInBackground() 中执行。如果要显示数据,必须在onPostExecute()中完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
  • 2020-07-29
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多