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


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

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

  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

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

2.MobileInfoService.java

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

3.工具类

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

相关内容