Android下实现电话号码归属地的查询

 

需要使用到WebService的服务,这里选择www.webxml.com.cn提供的服务来查询电话号码归属地,使用方法网页上有介绍,这里使用一个实例来演示如何在Android下实现电话号码归属地的查询:

0.使用webxml的soap方式:mobilesoap.xml在src目录下

Xhtml代码
  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>$mobile</mobileCode> 
  6.       <userID></userID> 
  7.     </getMobileCodeInfo> 
  8.   </soap12:Body> 
  9. </soap12:Envelope> 

1.MainActivity.java

Java代码
  1. public class MainActivity extends Activity  
  2.     private EditText mobileText; 
  3.     private TextView addressView; 
  4.     private static final String TAG = "MainActivity"
  5.      
  6.     @Override 
  7.     public void onCreate(Bundle savedInstanceState)  
  8.     { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main); 
  11.          
  12.         mobileText = (EditText)this.findViewById(R.id.mobile); 
  13.         addressView = (TextView)this.findViewById(R.id.address); 
  14.         Button button = (Button)this.findViewById(R.id.button); 
  15.         button.setOnClickListener(new View.OnClickListener()  
  16.         {            
  17.             @Override 
  18.             public void onClick(View v)  
  19.             { 
  20.                 String mobile = mobileText.getText().toString(); 
  21.                 InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); 
  22.                 try  
  23.                 { 
  24.                     addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); 
  25.                 }  
  26.                 catch (Exception e) 
  27.                 { 
  28.                     Log.e(TAG, e.toString()); 
  29.                     Toast.makeText(MainActivity.this, "查询失败", 1).show(); 
  30.                 } 
  31.             } 
  32.         }); 
  33.     } 

2.MobileInfoService.java

Java代码
  1. public class MobileInfoService  
  2.     /**
  3.      * 获得电话号码归属地信息
  4.      * @param inStream
  5.      * @param mobile
  6.      * @return
  7.      * @throws Exception
  8.      */ 
  9.     public static String getMobileAddress(InputStream inStream, String mobile)throws Exception 
  10.     { 
  11.         //定义输入流 
  12.         String soap = readSoapFile(inStream, mobile); 
  13.         byte[] data = soap.getBytes(); 
  14.         URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); 
  15.         HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
  16.         conn.setRequestMethod("POST"); 
  17.         conn.setConnectTimeout(5 * 1000); 
  18.         //如果通过post提交数据,必须设置允许对外输出数据 
  19.         conn.setDoOutput(true); 
  20.         conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); 
  21.         conn.setRequestProperty("Content-Length", String.valueOf(data.length)); 
  22.         OutputStream outStream = conn.getOutputStream(); 
  23.         outStream.write(data); 
  24.         outStream.flush(); 
  25.         outStream.close(); 
  26.         if(conn.getResponseCode()==200
  27.         { 
  28.             return parseResponseXML(conn.getInputStream()); 
  29.         } 
  30.         return null
  31.     } 
  32.      
  33.     /**
  34.      * 读取Soap文件
  35.      * @param inStream
  36.      * @param mobile
  37.      * @return
  38.      * @throws Exception
  39.      */ 
  40.     private static String readSoapFile(InputStream inStream, String mobile) throws Exception 
  41.     { 
  42.         //读取输入流 
  43.         byte[] data = StreamTool.readInputStream(inStream); 
  44.         String soapxml = new String(data); 
  45.         Map<String, String> params = new HashMap<String, String>(); 
  46.         params.put("mobile", mobile); 
  47.         return replace(soapxml, params); 
  48.     } 
  49.     /**
  50.      * 替换占位符方法
  51.      * @param xml
  52.      * @param params
  53.      * @return
  54.      * @throws Exception
  55.      */ 
  56.     public static String replace(String xml, Map<String, String> params)throws Exception 
  57.     { 
  58.         String result = xml; 
  59.         if(params!=null && !params.isEmpty()) 
  60.         { 
  61.             //循环替换掉所有占位符 
  62.             for(Map.Entry<String, String> entry : params.entrySet()) 
  63.             { 
  64.                 //需要对$进行转义 
  65.                 String name = "//$"+ entry.getKey(); 
  66.                 //使用正则表达式替换 
  67.                 Pattern pattern = Pattern.compile(name); 
  68.                 Matcher matcher = pattern.matcher(result); 
  69.                 if(matcher.find()) 
  70.                 { 
  71.                     result = matcher.replaceAll(entry.getValue()); 
  72.                 } 
  73.             } 
  74.         } 
  75.         return result; 
  76.     } 
  77.      
  78.     /**
  79.      * 解析返回的XML字符串数据
  80.      * @param inStream
  81.      * @return
  82.      * @throws Exception
  83.      */ 
  84.     private static String parseResponseXML(InputStream inStream) throws Exception 
  85.     { 
  86.         XmlPullParser parser = Xml.newPullParser(); 
  87.         parser.setInput(inStream, "UTF-8"); 
  88.         //产生第一个事件 
  89.         int eventType = parser.getEventType(); 
  90.         //只要不是文档结束事件 
  91.         while(eventType!=XmlPullParser.END_DOCUMENT) 
  92.         { 
  93.             switch (eventType)  
  94.             {    
  95.                 case XmlPullParser.START_TAG: 
  96.                     //获取解析器当前指向的元素的名称 
  97.                     String name = parser.getName(); 
  98.                     if("getMobileCodeInfoResult".equals(name)) 
  99.                     { 
  100.                         return parser.nextText(); 
  101.                     } 
  102.                     break
  103.             } 
  104.             eventType = parser.next(); 
  105.         } 
  106.         return null
  107.     } 

3.工具类

Java代码
  1. /**
  2. * 上传文件
  3. */ 
  4. public class FormFile  
  5.     /* 上传文件的数据 */ 
  6.     private byte[] data; 
  7.     private InputStream inStream; 
  8.     private File file; 
  9.     /* 文件名称 */ 
  10.     private String filname; 
  11.     /* 请求参数名称*/ 
  12.     private String parameterName; 
  13.     /* 内容类型 */ 
  14.     private String contentType = "application/octet-stream"
  15.      
  16.     public FormFile(String filname, byte[] data, String parameterName, String contentType)  
  17.     { 
  18.         this.data = data; 
  19.         this.filname = filname; 
  20.         this.parameterName = parameterName; 
  21.         if(contentType!=null) this.contentType = contentType; 
  22.     } 
  23.      
  24.     public FormFile(String filname, File file, String parameterName, String contentType) 
  25.     { 
  26.         this.filname = filname; 
  27.         this.parameterName = parameterName; 
  28.         this.file = file; 
  29.         try  
  30.         { 
  31.             this.inStream = new FileInputStream(file); 
  32.         }  
  33.         catch (FileNotFoundException e)  
  34.         { 
  35.             e.printStackTrace(); 
  36.         } 
  37.         if(contentType!=null) this.contentType = contentType; 
  38.     } 
  39.      
  40.     public File getFile()  
  41.     { 
  42.         return file; 
  43.     } 
  44.     public InputStream getInStream()  
  45.     { 
  46.         return inStream; 
  47.     } 
  48.     public byte[] getData()  
  49.     { 
  50.         return data; 
  51.     } 
  52.     public String getFilname()  
  53.     { 
  54.         return filname; 
  55.     } 
  56.     public void setFilname(String filname) 
  57.     { 
  58.         this.filname = filname; 
  59.     } 
  60.     public String getParameterName()  
  61.     { 
  62.         return parameterName; 
  63.     } 
  64.     public void setParameterName(String parameterName)  
  65.     { 
  66.         this.parameterName = parameterName; 
  67.     } 
  68.     public String getContentType()  
  69.     { 
  70.         return contentType; 
  71.     } 
  72.     public void setContentType(String contentType)  
  73.     { 
  74.         this.contentType = contentType; 
  75.     }    

Java代码
  1. /**
  2. * 上传文件
  3. */ 
  4. public class FormFile  
  5.     /* 上传文件的数据 */ 
  6.     private byte[] data; 
  7.     private InputStream inStream; 
  8.     private File file; 
  9.     /* 文件名称 */ 
  10.     private String filname; 
  11.     /* 请求参数名称*/ 
  12.     private String parameterName; 
  13.     /* 内容类型 */ 
  14.     private String contentType = "application/octet-stream"
  15.      
  16.     public FormFile(String filname, byte[] data, String parameterName, String contentType)  
  17.     { 
  18.         this.data = data; 
  19.         this.filname = filname; 
  20.         this.parameterName = parameterName; 
  21.         if(contentType!=null) this.contentType = contentType; 
  22.     } 
  23.      
  24.     public FormFile(String filname, File file, String parameterName, String contentType) 
  25.     { 
  26.         this.filname = filname; 
  27.         this.parameterName = parameterName; 
  28.         this.file = file; 
  29.         try  
  30.         { 
  31.             this.inStream = new FileInputStream(file); 
  32.         }  
  33.         catch (FileNotFoundException e)  
  34.         { 
  35.             e.printStackTrace(); 
  36.         } 
  37.         if(contentType!=null) this.contentType = contentType; 
  38.     } 
  39.      
  40.     public File getFile()  
  41.     { 
  42.         return file; 
  43.     } 
  44.     public InputStream getInStream()  
  45.     { 
  46.         return inStream; 
  47.     } 
  48.     public byte[] getData()  
  49.     { 
  50.         return data; 
  51.     } 
  52.     public String getFilname()  
  53.     { 
  54.         return filname; 
  55.     } 
  56.     public void setFilname(String filname) 
  57.     { 
  58.         this.filname = filname; 
  59.     } 
  60.     public String getParameterName()  
  61.     { 
  62.         return parameterName; 
  63.     } 
  64.     public void setParameterName(String parameterName)  
  65.     { 
  66.         this.parameterName = parameterName; 
  67.     } 
  68.     public String getContentType()  
  69.     { 
  70.         return contentType; 
  71.     } 
  72.     public void setContentType(String contentType)  
  73.     { 
  74.         this.contentType = contentType; 
  75.     }    

Java代码
  1. public class StreamTool  
  2.     /**
  3.      * 从输入流读取数据
  4.      * @param inStream
  5.      * @return
  6.      * @throws Exception
  7.      */ 
  8.     public static byte[] readInputStream(InputStream inStream) throws Exception 
  9.     { 
  10.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); 
  11.         byte[] buffer = new byte[1024]; 
  12.         int len = 0
  13.         while( (len = inStream.read(buffer)) !=-1
  14.         { 
  15.             outSteam.write(buffer, 0, len); 
  16.         } 
  17.         outSteam.close(); 
  18.         inStream.close(); 
  19.         return outSteam.toByteArray(); 
  20.     } 

4.程序运行效果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值