1.第一步去下載ksoap jar包 放在在android工程libs中,以後添加這個jar包 java
2.在manifest.xml添加 網絡訪問權限 android
以後代碼: web
package com.example.webservice; import java.util.Properties; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; public class WebService { //webService的名字空間 private static final String NAMESPACE="http://WebXml.com.cn/"; //天氣預報 webservice地址 private static String addr = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx"; //action 名字空間+webservice方法名 private static String soapAction = "http://WebXml.com.cn/getWeather"; // private static final String WATHER_METHOD_NAME="getWeather"; public static String getWeather(String cityName){ //建立 soap對象 SoapObject rpc=new SoapObject(NAMESPACE,WATHER_METHOD_NAME); SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); //結果字符串 String result="empty"; // envelope.bodyOut=rpc; //是不是.net webservice envelope.dotNet=true; //輸出的soapObject envelope.setOutputSoapObject(rpc); // HttpTransportSE transport=new HttpTransportSE(addr); rpc.addProperty("theCityCode",cityName); try{ transport.call(soapAction, envelope); }catch(Exception e){ e.printStackTrace(); } SoapObject object =(SoapObject)envelope.bodyIn; if(object!=null){ result=object.getProperty(0).toString(); } return result; } }
jar包寫好現成的方法了 ,很簡單。另外 webservice 也能夠以url 形式訪問,以後從url中獲取返回值 進行xml解析便可 網絡