Android入门:调用WebService
来源:互联网 发布:联合办公网络设计方案 编辑:程序博客网 时间:2024/06/05 03:31
二、WebService实例
http://www.webxml.com.cn/zh_cn/index.aspx
此网址给出了很多Web服务,我们可以调用此处给定的Web服务;
此处我们实现的功能是根据手机号查询归属地;
需要使用的网页为:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
我们使用SOAP 1.2协议;
从网页中可以看出,我们需要发送如下SOAP协议给Web服务:
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <soap12:Body>
- <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
- <mobileCode>string</mobileCode> <!--此处的string可以设置为手机号 -->
- <userID></userID> <!--此处可以不设置 -->
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
HTTP请求头:
- POST /WebServices/MobileCodeWS.asmx HTTP/1.1 //path
- Host: webservice.webxml.com.cn //url
- Content-Type: application/soap+xml; charset=utf-8
- Content-Length: length
如下HTTP请求头可以看出Web服务的URL为:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
接收的XML数据:
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <soap12:Body>
- <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
- <getMobileCodeInfoResult>string</getMobileCodeInfoResult><!--用PULL解析出string-->
- </getMobileCodeInfoResponse>
- </soap12:Body>
- </soap12:Envelope>
我们实现一个单元测试用来完成Android客户端调用Web服务;
- package org.xiazdong.webservice;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import org.xmlpull.v1.XmlPullParser;
- import android.test.AndroidTestCase;
- import android.util.Xml;
- import com.xiazdong.netword.http.util.HttpRequestUtil;
- public class WebServiceTest extends AndroidTestCase {
- public void testMobile() throws Exception {
- InputStream in = this.getClass().getResourceAsStream("mobilesoap.xml");
- String xml = HttpRequestUtil.read2String(in);
- xml = xml.replaceAll("string", "13795384758");//
- System.out.println(xml);
- //发送SOAP,并返回in
- in = HttpRequestUtil
- .postXml(
- "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",
- xml, "UTF-8");
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(in, "UTF-8");
- String value = "";
- int event = parser.getEventType();
- while (event != XmlPullParser.END_DOCUMENT) {
- switch (event) {
- case XmlPullParser.START_TAG:
- if ("getMobileCodeInfoResult".equals(parser.getName())) {
- value = parser.nextText();//取得
- }
- break;
- }
- event = parser.next();
- }
- System.out.println("地址为:"+value);
- }
- }
- Android入门:调用WebService
- Android入门:调用WebService
- Android入门:调用WebService
- Android入门:调用WebService
- Android开发入门之发送XML数据和调用webservice
- WebService入门及jdk调用
- Android调用WebService
- Android 调用.NET webservice
- android 调用 WebService
- Android访问调用Webservice
- Android 调用.NET webservice .
- android 调用webservice
- Android调用WebService
- android 调用webservice
- Android中调用WebService
- Android调用webservice
- Android调用WebService
- android 调用c# webservice
- ASP读取、添加、修改、删除数据库的代码
- 简化函数调用之五 :Parameterize Method(令函数携带参数)
- 程序人生之我们的故事:十年如歌(7)
- 简化函数调用之六 :Replace Parameter with Explicit Methods(以明确函数取代参数)
- hdu 1305 Immediate Decodability
- Android入门:调用WebService
- 什么是面向对象(1)
- pb使用msxml进行网页存取
- DROP DATABASE ORA-01504
- JAVA形参值传递,引用传递分析。
- 生成 guid 字符串化
- 简化函数调用之七 :Preserve Whole Object(保持对象完整)
- UIColor使用colorWithRed定义颜色
- 0032算法笔记——【回溯法】电路板排列问题和连续邮资问题