Android 利用Soap协议查询电话号码归属地


//目录结构

 //webservicerequest.xml

这个是内容是http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo SOPA 1.2的请求部份内容,至于那些main.xml strings.xml很简单的自己写就行了。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <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">  
  3.   <soap12:Body>  
  4.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/">  
  5.       <mobileCode>#phone</mobileCode>  
  6.       <userID></userID>  
  7.     </getMobileCodeInfo>  
  8.   </soap12:Body>  
  9. </soap12:Envelope>  

//响应部份主要是你通过HTTP请求过后,如果成功的话服务器端会给你发送XML响应数据。自己并通过程序解析即使获得响应值。也就是响应部份的<>string<>这个值

//GetMobileAddressService.java 业务的Service类,主要实现SOAP 协议的电话号码查询

  1. package sn.len.getmbaddress.service;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8. import java.util.regex.Matcher;  
  9. import java.util.regex.Pattern;  
  10.   
  11. import org.xmlpull.v1.XmlPullParser;  
  12.   
  13. import Android.util.Log;  
  14. import android.util.Xml;  
  15.   
  16. public class GetMobileAddressService   
  17. {  
  18.     //1,向webService发送XML请求数据参数   
  19.     public static String getMobileAddress(InputStream inStream,String phone) throws Exception  
  20.     {  
  21.         //返回的是一个输入流对象,所以要把inStream转换成二进制数据,写一个StreamToByte方法,以便以后使用   
  22.         byte reqeustByte[]=StreamToByte(inStream);  
  23.         //既然已经得到了xml的byte数据,则要把它变成String类型,再用正则把里面的电话电话号码给替换掉   
  24.         String requestXmlData=new String(reqeustByte);  
  25.         //写一个正则方法replacePhone()去替换内容。   
  26.         String replaced=replacePhone(requestXmlData,phone);  
  27.         byte replacedByte[]=replaced.getBytes();  
  28.         Log.i("REGCONTENT", replaced);  
  29.         //建立URL链接,向服务器端发送内容实体数据   
  30.         URL url=new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");  
  31.         HttpURLConnection conn=(HttpURLConnection)url.openConnection();  
  32.         conn.setRequestMethod("POST");  
  33.         conn.setReadTimeout(5*1000);  
  34.         conn.setDoOutput(true);  
  35.         //设置HTTP协议的数据请求类型和数据长度   
  36.         conn.setRequestProperty("Content-Type""application/soap+xml; charset=utf-8");  
  37.         conn.setRequestProperty("Content-Length", String.valueOf(replacedByte.length));  
  38.         //把XML数据向服务器端写入   
  39.         OutputStream outStream=conn.getOutputStream();  
  40.         outStream.write(replacedByte);  
  41.         outStream.flush();  
  42.         outStream.close();  
  43.         String address=null;  
  44.           
  45.         Log.i("CODE",conn.getResponseCode()+"");  
  46.         //如果写入成功,服务器端会给你返回一个以XML文件保存的响应数据给你   
  47.           
  48.         if(conn.getResponseCode()==200)  
  49.         {  
  50.             InputStream responeInStream=conn.getInputStream();  
  51.             //写一个方法parserResponseXML(),用PULL解析这个XML文档   
  52.             address=parserResponseXML(responeInStream);  
  53.         }  
  54.         return address;  
  55.           
  56.     }  
  57.     //把流转换成二进制数据   
  58.     public static byte[] StreamToByte(InputStream inStream) throws Exception  
  59.     {  
  60.         byte casebyte[]=new byte[1024];  
  61.         ByteArrayOutputStream byteOutStream=new ByteArrayOutputStream();  
  62.         int n=0;  
  63.         while((n=inStream.read(casebyte))!=-1)  
  64.         {  
  65.             byteOutStream.write(casebyte,0,n);  
  66.         }  
  67.         inStream.close();  
  68.         byteOutStream.close();  
  69.         return byteOutStream.toByteArray();  
  70.     }  
  71.     //接收响应的数据并解析   
  72.     public static String parserResponseXML(InputStream responseInStream) throws Exception  
  73.     {  
  74.         XmlPullParser pullParser=Xml.newPullParser();  
  75.         pullParser.setInput(responseInStream, "utf-8");  
  76.         int evenType=pullParser.getEventType();  
  77.         String responseValue=null;//响应值   
  78.         while(evenType!=XmlPullParser.END_DOCUMENT)  
  79.         {  
  80.             switch(evenType)  
  81.             {  
  82.                 case XmlPullParser.START_TAG:  
  83.                 {  
  84.                     String tag_name=pullParser.getName();  
  85.                     if("getMobileCodeInfoResult".equals(tag_name))  
  86.                     {  
  87.                         responseValue=pullParser.nextText();  
  88.                         return responseValue;  
  89.                     }  
  90.                 }break;  
  91.                 default:break;  
  92.             }  
  93.             evenType=pullParser.next(); //NND检查了好久。。。   
  94.         }  
  95.         return null;  
  96.     }  
  97.     public static String replacePhone(String requestXmlData,String phone)  
  98.     {  
  99.         //预编译正则表达示,也就是你想要替换的内容xml里面的#phone   
  100.         Pattern p=Pattern.compile("\\#phone");  
  101.         //把整个XML的数据与正则匹配,并把比对后值存放在m适配器中   
  102.         Matcher m=p.matcher(requestXmlData);  
  103.         //因为第一个XML可能节点匹配找到匹配的,所以要向下寻找,继续匹配   
  104.         if(m.find())  
  105.         {  
  106.             requestXmlData=m.replaceAll(phone);  
  107.         }  
  108.         return requestXmlData;  
  109.     }  
  110. }  
  • 1
  • 2
  • 下一页

相关内容