第一種:使用KSOAP2庫
原帖例子來自
http://www.cnblogs.com/ghj1976/archive/2011/04/26/2028904.html
這東西看起來好是好,可我最終還是以失敗告終。
第一次崩潰是因為版本問題,首頁上下載的最新2.5.7不知怎麼搞的,編譯沒問題,一運行就程式崩潰。後來換成2.4的總算成功了。
第二次也不知道什麼原因。例子能跑通,我自己的程式卻死活不行,再次崩潰。
原帖分析的不錯,copy一下。
1、指定 WebService 的命名空間和調用方法
import org.ksoap2.serialization.SoapObject;private static final String NAMESPACE = "http://WebXml.com.cn/";private static final String METHOD_NAME = "getWeatherbyCityName";SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
SoapObject類的第一個參數表示WebService的命名空間,可以從WSDL文檔中找到WebService的命名空間。
第二個參數表示要調用的WebService方法名。
2、設定調用方法的參數值,如果沒有參數,可以省略,設定方法的參數值的代碼如下:
rpc.addProperty("theCityName", "北京");
要注意的是,addProperty方法的第1個參數雖然表示調用方法的參數名,但該參數值並不一定與服務端的WebService類中的方法參數名一致,只要設定參數的順序一致即可。
3、產生調用Webservice方法的SOAP請求資訊。
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = rpc;envelope.dotNet = true;envelope.setOutputSoapObject(rpc);
建立SoapSerializationEnvelope對象時需要通過SoapSerializationEnvelope類的構造方法設定SOAP協議的版本號碼。
該版本號碼需要根據服務端WebService的版本號碼設定。
在建立SoapSerializationEnvelope對象後,不要忘了設定SOAPSoapSerializationEnvelope類的bodyOut屬性,
該屬性的值就是在第一步建立的SoapObject對象。
4、建立HttpTransportsSE對象。
這裡不要使用 AndroidHttpTransport ht = new AndroidHttpTransport(URL); 這是一個要到期的類
private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
5、使用call方法調用WebService方法
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";ht.call(SOAP_ACTION, envelope);
網上有人說這裡的call的第一個參數為null,但是經過我的測試,null是不行的。
第2個參數就是在第3步建立的SoapSerializationEnvelope對象。
6、獲得WebService方法的返回結果
有兩種方法:
1、使用getResponse方法獲得返回資料。
private SoapObject detail;detail =(SoapObject) envelope.getResponse();
2、使用 bodyIn 及 getProperty。
private SoapObject detail;SoapObject result = (SoapObject)envelope.bodyIn;detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
7、 這時候執行會出錯,提示沒有許可權訪問網路
需要修改 AndroidManifest.xml 檔案,賦予相應許可權
簡單來說就是增加下面這行配置:<uses-permission android:name="android.permission.INTERNET"></uses-permission>
第二種方法 直接寫和讀XML串
原帖來自
http://hi.baidu.com/lbp0408/blog/item/7971ae10d229b30c203f2e12.html/cmtid/d50a2ff4ca6692e07709d7e7
挺好用,就是往WebService發一段符合要求的XML字串,再讀返回的XML字串。
不過XML的解析還得費點功夫
final String SERVER_URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; // 定義需要擷取的內容來源地址URL url = new URL(SERVER_URL);URLConnection con = url.openConnection();con.setDoOutput(true);con.setRequestProperty("Pragma:", "no-cache");con.setRequestProperty("Cache-Control", "no-cache");con.setRequestProperty("Content-Type", "text/xml");OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream()); String xmlInfo = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +"<soap:Body>\n" +"<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">\n" +"<theCityName>beijing</theCityName>\n" +"</getWeatherbyCityName>\n" +"</soap:Body>\n" +"</soap:Envelope>\n";Toast.makeText(this, xmlInfo, Toast.LENGTH_LONG).show();發送out.write(new String(xmlInfo.getBytes("UTF-8")));out.flush();out.close();// 取傳回值BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));StringBuilder sBuilder = new StringBuilder();String line = "";for (line = br.readLine(); line != null; line = br.readLine()) { sBuilder.append(line);}