Android发送SOAP数据给服务器调用webservice,实现手机号归属地查询

本文主要是介绍Android发送SOAP数据给服务器调用webservice,实现手机号归属地查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/mm2223/article/details/7102118

创建android工程MobileBelong,设置网络访问权限。

 

资源

view plain copy to clipboard
  1. <string name="hello">Hello World, MainActivity!</string>  
  2. <string name="app_name">手机号归属地查询</string>  
  3. <string name="mobile">手机号</string>  
  4. <string name="button">查询</string>  
  5. <string name="error">网络连接失败</string>  

布局

view plain copy to clipboard
  1. TextView  
  2.         android:layout_width="fill_parent"  
  3.         android:layout_height="wrap_content"  
  4.         android:text="@string/mobile" />  
  5.   
  6.     <EditText  
  7.         android:id="@+id/mobile"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="13472283596" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/button"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/button" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/result"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content" />  

在src目录下创建mobilesoap.xml,并将网址文档中提供的代码复制其中,如下

view plain copy to clipboard
  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>  

业务类:MobileService

注意访问目标地址是:

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

可以有协议中得到。


view plain copy to clipboard
  1. package cn.class3g.service;  
  2. …  
  3. public class MobileService {  
  4.   
  5. public static String getMobileAddress(String mobile) throws Exception {  
  6.   
  7.         InputStream inStream = MobileService.class.getClassLoader()  
  8.                 .getResourceAsStream("mobilesoap.xml");  
  9.         byte[] data = StreamTool.readInputStream(inStream);  
  10.         String xml = new String(data);  
  11.         String soap = xml.replaceAll("\\$mobile", mobile);  
  12.   
  13.         /**  
  14.          * 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile  
  15.          * 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;  
  16.          */  
  17.         String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
  18.         data = soap.getBytes();// 得到了xml的实体数据  
  19.         URL url = new URL(path);  
  20.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  21.         conn.setConnectTimeout(5 * 1000);  
  22.         conn.setRequestMethod("POST");  
  23.         conn.setDoOutput(true);  
  24.         conn.setRequestProperty("Content-Type",  
  25.                 "application/soap+xml; charset=utf-8");  
  26.         conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
  27.         OutputStream outStream = conn.getOutputStream();  
  28.         outStream.write(data);  
  29.         outStream.flush();  
  30.         outStream.close();  
  31.         if (conn.getResponseCode() == 200) {  
  32.             InputStream responseStream = conn.getInputStream();  
  33.             return parseXML(responseStream);  
  34.         }  
  35.         return null;  
  36.     }  
  37.   
  38.     /**  
  39.      * 解析返回xml数据  
  40.      *   
  41.      * @param responseStream  
  42.      * @return  
  43.      * @throws Exception  
  44.      */  
  45.     private static String parseXML(InputStream responseStream) throws Exception {  
  46.         XmlPullParser parser = Xml.newPullParser();  
  47.         parser.setInput(responseStream, "UTF-8");  
  48.         int event = parser.getEventType();  
  49.         while (event != XmlPullParser.END_DOCUMENT) {  
  50.             switch (event) {  
  51.             case XmlPullParser.START_TAG:  
  52.                 if ("getMobileCodeInfoResult".equals(parser.getName())) {  
  53.                     return parser.nextText();  
  54.                 }  
  55.                 break;  
  56.             }  
  57.             event = parser.next();  
  58.         }  
  59.         return null;  
  60.     }  
  61. }  
[html]  view plain copy
  1. package cn.class3g.service;  
  2. …  
  3. public class MobileService {  
  4.   
  5. public static String getMobileAddress(String mobile) throws Exception {  
  6.   
  7.         InputStream inStream = MobileService.class.getClassLoader()  
  8.                 .getResourceAsStream("mobilesoap.xml");  
  9.         byte[] data = StreamTool.readInputStream(inStream);  
  10.         String xml = new String(data);  
  11.         String soap = xml.replaceAll("\\$mobile", mobile);  
  12.   
  13.         /**  
  14.          * 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile  
  15.          * 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;  
  16.          */  
  17.         String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
  18.         data = soap.getBytes();// 得到了xml的实体数据  
  19.         URL url = new URL(path);  
  20.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  21.         conn.setConnectTimeout(5 * 1000);  
  22.         conn.setRequestMethod("POST");  
  23.         conn.setDoOutput(true);  
  24.         conn.setRequestProperty("Content-Type",  
  25.                 "application/soap+xml; charset=utf-8");  
  26.         conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
  27.         OutputStream outStream = conn.getOutputStream();  
  28.         outStream.write(data);  
  29.         outStream.flush();  
  30.         outStream.close();  
  31.         if (conn.getResponseCode() == 200) {  
  32.             InputStream responseStream = conn.getInputStream();  
  33.             return parseXML(responseStream);  
  34.         }  
  35.         return null;  
  36.     }  
  37.   
  38.     /**  
  39.      * 解析返回xml数据  
  40.      *   
  41.      * @param responseStream  
  42.      * @return  
  43.      * @throws Exception  
  44.      */  
  45.     private static String parseXML(InputStream responseStream) throws Exception {  
  46.         XmlPullParser parser = Xml.newPullParser();  
  47.         parser.setInput(responseStream, "UTF-8");  
  48.         int event = parser.getEventType();  
  49.         while (event != XmlPullParser.END_DOCUMENT) {  
  50.             switch (event) {  
  51.             case XmlPullParser.START_TAG:  
  52.                 if ("getMobileCodeInfoResult".equals(parser.getName())) {  
  53.                     return parser.nextText();  
  54.                 }  
  55.                 break;  
  56.             }  
  57.             event = parser.next();  
  58.         }  
  59.         return null;  
  60.     }  
  61. }  

工具类StreamTool

view plain copy to clipboard
  1. package cn.class3g.utils;  
  2. …  
  3. public class StreamTool {  
  4.     /**  
  5.      * 从输入流读取数据  
  6.      * @param inStream  
  7.      * @return  
  8.      * @throws Exception  
  9.      */  
  10.     public static byte[] readInputStream(InputStream inStream) throws Exception{  
  11.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  12.         byte[] buffer = new byte[1024];  
  13.         int len = 0;  
  14.         while( (len = inStream.read(buffer)) !=-1 ){  
  15.             outSteam.write(buffer, 0, len);  
  16.         }  
  17.         outSteam.close();  
  18.         inStream.close();  
  19.         return outSteam.toByteArray();  
  20.     }  
  21. }  
  22.   
  23. Activity类MobileBelongActivity  
  24. package cn.class3g.mobile;  
  25. …  
  26. public class MobileBelongActivity extends Activity {  
  27.   
  28.     private static final String TAG = "MainActivity";  
  29.     private EditText mobileText;  
  30.     private TextView resultView;  
  31.   
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.   
  37.         mobileText = (EditText) this.findViewById(R.id.mobile);  
  38.         resultView = (TextView) this.findViewById(R.id.result);  
  39.         Button button = (Button) this.findViewById(R.id.button);  
  40.         button.setOnClickListener(new View.OnClickListener() {  
  41.             @Override  
  42.             public void onClick(View v) {  
  43.                 String mobile = mobileText.getText().toString();  
  44.                 try {  
  45.                     String address = MobileService.getMobileAddress(mobile);  
  46.                     resultView.setText(address);  
  47.                 } catch (Exception e) {  
  48.                     Log.e(TAG, e.toString());  
  49.                     Toast.makeText(MobileBelongActivity.this, R.string.error, 1).show();  
  50.                 }  
  51.             }  
  52.         });  
  53.     }  
  54. }  

这篇关于Android发送SOAP数据给服务器调用webservice,实现手机号归属地查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1071099

相关文章

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息