1. 首先需要包ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar 下载: ksoap.rar
2. 在androidmanifest中添加访问网络的权限:<uses-permission android:name="android.permission.INTERNET"></uses-permission>
3. 代码编写:
public class AndroidWebServiceActivity extends Activity {
public final static String NAMESPACE = "http://Services.Jason.TSD/";
public static String wsdl = "http://192.168.1.6:8080/UserMgrService/UserInfoPort?wsdl";
String methodName = "getUserInfo";
String soupaction = NAMESPACE + "" + methodName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncTask<Void, Void, Void>() {
SoapSerializationEnvelope envelope;
@Override
protected Void doInBackground(Void... params) {
// 指定webservice的命名空间和调用的方法名
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
System.out.println("rpc" + rpc);
//设置调用方法的参数值,如果没有参数,可以省略
//rpc.addProperty(“param1”,”value”);
//创建HttpTransportsSE对象。通过HttpTransportsSE类的构造方法可以指定WebService的WSDL文档的URL
HttpTransportSE ht = new HttpTransportSE(wsdl);
ht.debug = true;
//生成调用Webservice方法的SOAP请求信息
envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
try {
//使用call方法调用WebService方法
ht.call(soupaction, envelope);
} catch (IOException e) {
System.out.println(2);
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
System.out.println(1);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//使用getResponse方法获得WebService方法的返回结果
//SoapObject soapObject =( SoapObject) envelope.getResponse();
// 获取返回的数据
SoapObject result2 = (SoapObject)envelope.bodyIn;
// 获取返回的结果
System.out.println(result2.getProperty(0).toString());
super.onPostExecute(result);
}
}.execute();
}
}