首先介紹下網上常用的webservice調用方法,例子很多,我就不詳細介紹了,簡單說下流程:
// 建立soapObject對象,參數為命名空間和調用方法名,也就是soap_action. 這個可以在WSDL中擷取.
SoapObject object = new SoapObject(NAMESPACE, METHOD_NAME);
object.addProperty("theCityName", cityName);// 設定屬性
如果屬性過多則一一設定.
// 根據版本號碼建立SoapSerializationEnvelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = object;
envelope.dotNet = true;
envelope.setOutputSoapObject(object);
//建立HttpTransportSE HttpTransportSE httpTransportSE = new HttpTransportSE(URL);try {httpTransportSE.call(SOAP_ACTION, envelope); //soapObject = (SoapObject) envelope.getResponse();(1)soapObject = (SoapObject) envelope.bodyIn;(2) 上面兩中效果都可以擷取soapObject對象,但是擷取的不一樣. 第一個擷取到的是除去根節點,第二個則沒有除去根節點,不過可以這樣處理 soapObject = (SoapObject) envelope.bodyIn; (SoapObject)soapObject.getProperty(0);//這時候擷取到的對象相當於1步擷取到的對象。} catch (IOException e) {e.printStackTrace();} catch (XmlPullParserException e) {e.printStackTrace();}SoapObject object2=(SoapObject)soapObject.getProperty(0);String ss = object2.getProperty(5).toString();Log.v(tag, ss);return object2.getProperty(5).toString(); 上面這是一種方式,我感覺這種方式很簡單,解析資料也比較簡單,下面我介紹另外一種方式,是我之前經理用的,感覺比較繁瑣,不過理解起來會比較容易, 首先我們要按照這幾步執行:/**** 拼接請求webservice* * @return*/private static StringEntity getEntity(String thecity) {StringEntity entity = null; //可以參照WSDL...String str = String.format("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+ "<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/\">"+ "<soap:Body>"+ "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"+ "<theCityName>%1$s</theCityName>"+ "</getWeatherbyCityName>" + "</soap:Body>"+ "</soap:Envelope>", thecity);try {entity = new StringEntity(str);} catch (UnsupportedEncodingException e) {e.printStackTrace();}return entity;} /**** webservice 請求*/public static HttpPost httpPost(StringEntity entity, String SOAP_ACTION) {HttpPost request = new HttpPost(URL);request.addHeader("SOAPAction", SOAP_ACTION);request.addHeader("Host","www.webxml.com.cn");request.addHeader("Content-Type", "text/xml; charset=utf-8");request.setEntity(entity);return request;}/**** 擷取響應webservice HttpResponse*/public static HttpResponse getHttpResponse(HttpPost httpPost) {HttpClient client = new DefaultHttpClient();client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,TimeoutBySecond * 1000);HttpResponse httpResponse = null;try {httpResponse = client.execute(httpPost);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return httpResponse;} /**** 擷取內容 解析httpresponse*/public static String getContent(HttpResponse httpResponse) {InputStream inputStream;BufferedReader in = null;StringBuffer sb = new StringBuffer();try {inputStream = httpResponse.getEntity().getContent();in = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));String inputLine;while ((inputLine = in.readLine()) != null) {sb.append(inputLine);sb.append("\n");// 換行}in.close();} catch (IllegalStateException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return sb.toString();}
最後我們擷取到的字串是一個完整的xml檔案,所以我們可以根據dom,sax,pull進行解析,這裡就不再述說.
這種很像我們之前經常用的請求---響應,不過有點太麻煩了,不建議使用,第一種方式已經幫我們把能封裝的都封裝好了,所以會比較簡單,總之只要能達到效果就ok,
最後說一個問題:也是糾結我好久了,就是解析xml問題,不知道有沒有同學遇到過沒:就是在你請求webservice擷取到的是一個完整的RSS檔案,也就是xml.如果用第一種方法,我想應該沒有問題,但是如果用第二種,就會遇到個問題,也就是最外層是一個xml檔案,但裡面又包含了一個xml,這時候你用dom,sax,解析肯定出現bug,後來發現,因為這個就不是一個正確的xml檔案。,所以我的解決辦法,先String截取到裡面的完整RSS檔案,這樣就可以進行解析。不過現在想想自己饒的太遠了.
demo下載