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

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4