//目錄結構
//webservicerequest.xml
這個是內容是http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo SOPA 1.2的請求部份內容,至於那些main.xml strings.xml很簡單的自己寫就行了。
- <?xml version="1.0" encoding="utf-8"?>
- <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <soap12:Body>
- <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
- <mobileCode>#phone</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
//響應部份主要是你通過HTTP請求過後,如果成功的話服務器端會給你發送XML響應數據。自己並通過程序解析即使獲得響應值。也就是響應部份的<>string<>這個值
//GetMobileAddressService.java 業務的Service類,主要實現SOAP 協議的電話號碼查詢
- package sn.len.getmbaddress.service;
-
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- import org.xmlpull.v1.XmlPullParser;
-
- import Android.util.Log;
- import android.util.Xml;
-
- public class GetMobileAddressService
- {
- //1,向webService發送XML請求數據參數
- public static String getMobileAddress(InputStream inStream,String phone) throws Exception
- {
- //返回的是一個輸入流對象,所以要把inStream轉換成二進制數據,寫一個StreamToByte方法,以便以後使用
- byte reqeustByte[]=StreamToByte(inStream);
- //既然已經得到了xml的byte數據,則要把它變成String類型,再用正則把裡面的電話電話號碼給替換掉
- String requestXmlData=new String(reqeustByte);
- //寫一個正則方法replacePhone()去替換內容。
- String replaced=replacePhone(requestXmlData,phone);
- byte replacedByte[]=replaced.getBytes();
- Log.i("REGCONTENT", replaced);
- //建立URL鏈接,向服務器端發送內容實體數據
- URL url=new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
- HttpURLConnection conn=(HttpURLConnection)url.openConnection();
- conn.setRequestMethod("POST");
- conn.setReadTimeout(5*1000);
- conn.setDoOutput(true);
- //設置HTTP協議的數據請求類型和數據長度
- conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
- conn.setRequestProperty("Content-Length", String.valueOf(replacedByte.length));
- //把XML數據向服務器端寫入
- OutputStream outStream=conn.getOutputStream();
- outStream.write(replacedByte);
- outStream.flush();
- outStream.close();
- String address=null;
-
- Log.i("CODE",conn.getResponseCode()+"");
- //如果寫入成功,服務器端會給你返回一個以XML文件保存的響應數據給你
-
- if(conn.getResponseCode()==200)
- {
- InputStream responeInStream=conn.getInputStream();
- //寫一個方法parserResponseXML(),用PULL解析這個XML文檔
- address=parserResponseXML(responeInStream);
- }
- return address;
-
- }
- //把流轉換成二進制數據
- public static byte[] StreamToByte(InputStream inStream) throws Exception
- {
- byte casebyte[]=new byte[1024];
- ByteArrayOutputStream byteOutStream=new ByteArrayOutputStream();
- int n=0;
- while((n=inStream.read(casebyte))!=-1)
- {
- byteOutStream.write(casebyte,0,n);
- }
- inStream.close();
- byteOutStream.close();
- return byteOutStream.toByteArray();
- }
- //接收響應的數據並解析
- public static String parserResponseXML(InputStream responseInStream) throws Exception
- {
- XmlPullParser pullParser=Xml.newPullParser();
- pullParser.setInput(responseInStream, "utf-8");
- int evenType=pullParser.getEventType();
- String responseValue=null;//響應值
- while(evenType!=XmlPullParser.END_DOCUMENT)
- {
- switch(evenType)
- {
- case XmlPullParser.START_TAG:
- {
- String tag_name=pullParser.getName();
- if("getMobileCodeInfoResult".equals(tag_name))
- {
- responseValue=pullParser.nextText();
- return responseValue;
- }
- }break;
- default:break;
- }
- evenType=pullParser.next(); //NND檢查了好久。。。
- }
- return null;
- }
- public static String replacePhone(String requestXmlData,String phone)
- {
- //預編譯正則表達示,也就是你想要替換的內容xml裡面的#phone
- Pattern p=Pattern.compile("\\#phone");
- //把整個XML的數據與正則匹配,並把比對後值存放在m適配器中
- Matcher m=p.matcher(requestXmlData);
- //因為第一個XML可能節點匹配找到匹配的,所以要向下尋找,繼續匹配
- if(m.find())
- {
- requestXmlData=m.replaceAll(phone);
- }
- return requestXmlData;
- }
- }