view plaincopy to clipboardprint? package net.blogjava.mobile.wsclient; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Main extends Activity implements OnClickListener { private EditText etProductName; private TextView tvResult; class WSAsyncTask extends AsyncTask { String result = ""; @Override protected Object doInBackground(Object... params) { try { String serviceUrl = "http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl"; String methodName = "getProduct"; SoapObject request = new SoapObject("http://service", methodName); request.addProperty("productName", etProductName.getText().toString()); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = request; HttpTransportSE ht = new HttpTransportSE(serviceUrl); ht.call(null, envelope); if (envelope.getResponse() != null) { SoapObject soapObject = (SoapObject) envelope.getResponse(); result = "产品名称:" + soapObject.getProperty("name") + "/n"; result += "产品数量:" + soapObject.getProperty("productNumber") + "/n"; result += "产品价格:" + soapObject.getProperty("price"); } else { result = "无此产品."; } } catch (Exception e) { result = "调用WebService错误."; } // 必须使用post方法更新UI组件 tvResult.post(new Runnable() { @Override public void run() { tvResult.setText(result); } }); return null; } } @Override public void onClick(View view) { // 异步执行调用WebService的任务 new WSAsyncTask().execute(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnSearch = (Button) findViewById(R.id.btnSearch); btnSearch.setOnClickListener(this); etProductName = (EditText) findViewById(R.id.etProductName); tvResult = (TextView) findViewById(R.id.tvResult); } }