在Android 中使用KSOAP2调用WebService
WebService 是一种基于SOAP协议的远程调用标准。通过WebService可以将不同操作系统平台,不同语言、不同技术整合到一起。在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方类库(KSOAP2)来调用WebService。在本文将介绍在Android 中调用WebService的具体细节,并在最后给出一个完整的例子来演示如何使用KSOAP2来调用WebService。
SoapObject request = new SoapObject("http://service", "getName");
request.addProperty("param1", "value1"); request.addProperty("param2", "value2");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = request;
HttpTransportSE ht = new HttpTransportSE("http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl");
ht.call(null, envelope);
SoapObject soapObject = (SoapObject) envelope.getResponse();
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.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 { @Override public void onClick(View view) { EditText etProductName = (EditText)findViewById(R.id.etProductName); TextView tvResult = (TextView)findViewById(R.id.tvResult); // WSDL文档的URL,192.168.17.156为PC的ID地址 String serviceUrl = "http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl"; // 定义调用的WebService方法名 String methodName = "getProduct"; // 第1步:创建SoapObject对象,并指定WebService的命名空间和调用的方法名 SoapObject request = new SoapObject("http://service", methodName); // 第2步:设置WebService方法的参数 request.addProperty("productName", etProductName.getText().toString()); // 第3步:创建SoapSerializationEnvelope对象,并指定WebService的版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // 设置bodyOut属性 envelope.bodyOut = request; // 第4步:创建HttpTransportSE对象,并指定WSDL文档的URL HttpTransportSE ht = new HttpTransportSE(serviceUrl); try { // 第5步:调用WebService ht.call(null, envelope); if (envelope.getResponse() != null) { // 第6步:使用getResponse方法获得WebService方法的返回结果 SoapObject soapObject = (SoapObject) envelope.getResponse(); // 通过getProperty方法获得Product对象的属性值 String result = "产品名称:" + soapObject.getProperty("name") + "\n"; result += "产品数量:" + soapObject.getProperty("productNumber") + "\n"; result += "产品价格:" + soapObject.getProperty("price"); tvResult.setText(result); } else { tvResult.setText("无此产品."); } } catch (Exception e) { } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnSearch = (Button) findViewById(R.id.btnSearch); btnSearch.setOnClickListener(this); } }
- 在 第2步中addProperty方法的第1个参数值是productName,该值虽然是getProduct方法的参数名,但addProperty方 法的第1个参数值并不限于productName,读者可以将这个参数设为其他的任何字符串(但该值必须在XML中是合法的,例如,不是设为 “<”、“>”等XML预留的字符串)。
- 通过SoapObject类的getProperty方法可以获得Product对象的属性值,这些属性名就是图4所示的测试结果中的属性名。
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); } }