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

相关文章

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被