标签:www roi csdn 复杂 发包 lis enter runnable nat
WebService 是一种基于SOAP协议的远程调用标准。通过WebService可以将不同操作系统平台,不同语言、不同技术整合到一起。在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方类库(KSOAP2)来调用WebService。在本文将介绍在Android 中调用WebService的具体细节,并在最后给出一个完整的例子来演示如何使用KSOAP2来调用WebService。
将下载后的jar文件复制到Eclipse工程的lib目录中(如果没有该目录,可以新建一个,当然,也可以放在其他的目录中)。并在Eclipse工程中引用这个jar包,引用后的Eclipse工程目录结构如图1所示。
SoapObject request = new SoapObject("http://service", "getName");
SoapObject类的第1个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。第2个参数表示要调用的WebService方法名。
request.addProperty("param1", "value1");
request.addProperty("param2", "value2");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = request;
1 package net.blogjava.mobile.wsclient; 2 3 import org.ksoap2.SoapEnvelope; 4 import org.ksoap2.serialization.SoapObject; 5 import org.ksoap2.serialization.SoapSerializationEnvelope; 6 import org.ksoap2.transport.HttpTransportSE; 7 import android.app.Activity; 8 import android.os.AsyncTask; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.EditText; 14 import android.widget.TextView; 15 16 public class Main extends Activity implements OnClickListener 17 { 18 private EditText etProductName; 19 private TextView tvResult; 20 21 class WSAsyncTask extends AsyncTask 22 { 23 String result = ""; 24 @Override 25 protected Object doInBackground(Object... params) 26 { 27 try 28 { 29 String serviceUrl = "http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl"; 30 String methodName = "getProduct"; 31 SoapObject request = new SoapObject("http://service", 32 methodName); 33 request.addProperty("productName", etProductName.getText().toString()); 34 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 35 SoapEnvelope.VER11); 36 envelope.bodyOut = request; 37 HttpTransportSE ht = new HttpTransportSE(serviceUrl); 38 39 ht.call(null, envelope); 40 if (envelope.getResponse() != null) 41 { 42 SoapObject soapObject = (SoapObject) envelope.getResponse(); 43 result = "产品名称:" + soapObject.getProperty("name") + "\n"; 44 result += "产品数量:" + soapObject.getProperty("productNumber") 45 + "\n"; 46 result += "产品价格:" + soapObject.getProperty("price"); 47 48 } 49 else 50 { 51 result = "无此产品."; 52 } 53 } 54 catch (Exception e) 55 { 56 result = "调用WebService错误."; 57 } 58 // 必须使用post方法更新UI组件 59 tvResult.post(new Runnable() 60 { 61 @Override 62 public void run() 63 { 64 tvResult.setText(result); 65 66 } 67 }); 68 return null; 69 } 70 71 } 72 @Override 73 public void onClick(View view) 74 { 75 // 异步执行调用WebService的任务 76 new WSAsyncTask().execute(); 77 } 78 @Override 79 public void onCreate(Bundle savedInstanceState) 80 { 81 super.onCreate(savedInstanceState); 82 setContentView(R.layout.main); 83 Button btnSearch = (Button) findViewById(R.id.btnSearch); 84 btnSearch.setOnClickListener(this); 85 etProductName = (EditText) findViewById(R.id.etProductName); 86 tvResult = (TextView) findViewById(R.id.tvResult); 87 88 } 89 }
在Android 中使用KSOAP2调用WebService(转)
标签:www roi csdn 复杂 发包 lis enter runnable nat
原文地址:http://www.cnblogs.com/leis/p/7656664.html