java - 使用 AsyncTask 并将值返回到 UI 主线程

标签 java android-asynctask

我有一个 Android Activity 来显示网络服务的输出。我在另一个扩展 AsyncTask 的私有(private)类中调用了 Web 服务,并希望向主 UI 线程返回一个值。

这是我的 Activity 。

public class CallCalcService extends Activity {

private String METHOD_NAME = "sum"; // our webservice method name
private String NAMESPACE = "http://backend.android.web.org"; 

private String SOAP_ACTION = NAMESPACE + METHOD_NAME; 

private static final String URL = "http://192.168.1.14:8080/AndroidBackend1/services/Calculate?wsdl"; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_calc_service);

    //TextView tv = (TextView) findViewById(R.id.textView1);
    //Object res=new HardWorkThread().execute();
    //tv.setText("Addition : "+ res.toString());

    new HardWorkThread()
    {
        public void onPostExecute(String result)
        {
            TextView txt = (TextView) findViewById(R.id.textView1);
            txt.setText("Addition : "+result);
        }
    }.execute("");



}

private class HardWorkThread extends AsyncTask{

    @Override
    protected String doInBackground(Object... params) {
        // TODO Auto-generated method stub
        String result=null;
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("i", 5);
            request.addProperty("j", 15);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION,envelope);

             result = envelope.getResponse().toString();
            System.out.println("Result : " + result);
            //((TextView) findViewById(R.id.textView1)).setText("Addition : "+ result.toString());
        } catch (Exception E) {
            E.printStackTrace();
            //((TextView) findViewById(R.id.textView1)).setText("ERROR:"
                //  + E.getClass().getName() + ":" + E.getMessage());
        }
        return result;
    }



}

}

我想做的是将字符串结果值返回到主 UI,以便我可以将该值设置到 TextView 。

String result;

TextView tv=findViewById(R.id.textView1);
tv.setText(result);

我该怎么做?

最佳答案

您应该覆盖 onPostExecute() 方法。

像这样:

        @Override
    protected void onPostExecute(String result) {
        TextView tv = findViewById(R.id.textView1);
        tv.setText(result);
    }

关于java - 使用 AsyncTask 并将值返回到 UI 主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12084885/

相关文章:

java - 验证请求参数中的日期(ISO-8601)(spring-boot)

java - 如何在不创建新进程的情况下通过代码运行 java 命令?

android - 如何在mysql和sqlite之间同步数据?

Android runOnUiThread/AsyncTask 无法解析 CalledFromWrongThreadException

java - Android 应用程序因来电而被终止

java - 如何将数据从 JavaScript 传递到 JavaFX

java - String foo = "bar"与 String foo = new String ("bar") 在 Android 中?

java - 实例变量的值在两个类中是不同的。为什么? (+更多问题)

java - 如何确保 google speech api 返回值 - java