2

Web サービスの出力を表示する Android アクティビティがあります。AsyncTask を拡張する別のプライベート クラス内で Web サービスを呼び出し、メイン UI スレッドに値を返したいと考えています。

これが私の活動です。

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 に文字列の結果値を返し、その値をテキストビューに設定できるようにすることです。

String result;

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

どうすればこれを行うことができますか?

4

1 に答える 1

2

メソッドを使用する必要OverrideがありonPostExecute()ます。

このようなもの :

        @Override
    protected void onPostExecute(String result) {
        TextView tv = findViewById(R.id.textView1);
        tv.setText(result);
    }
于 2012-08-23T05:56:39.170 に答える