Android入门:调用WebService
来源:互联网 发布:转化vr视频软件 编辑:程序博客网 时间:2024/05/17 08:19
一、Android调用WebServices原理
WebServices通俗的说就是在网络上提供的API,与本地的API不同,我们不能直接调用此方法,而必须按照预先定义的SOAP协议传输给Web服务,然后Web服务接收到XML数据进行处理后,返回XML数据;
发送过去的XML数据中存在需要调用的函数及参数;
接收的XML数据存在函数的返回值,客户端需要从XML数据中解析出结果;
从以上可以看出客户端要做的只是发送XML数据和接收XML数据,因此如果要调用WebService,则客户端的语言是无限制的,可以用C++、Java等任何语言调用Web服务;
二、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>
因此我们先要把此XML数据存到本地文件;
HTTP请求头:
POST /WebServices/MobileCodeWS.asmx HTTP/1.1 //pathHost: webservice.webxml.com.cn //urlContent-Type: application/soap+xml; charset=utf-8Content-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,并返回inin = 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
- XML文件的读取用XmlDocument和XmlReader的比较
- 查询序列中的最长递增子序列的长度
- 代码大全学习-4
- 我的java之路之java的变量
- eclipse启动tomcat访问不到主页
- Android入门:调用WebService
- android listview优化及其解释
- Java Class文件深度格式解析
- oprofile整理
- Delphi 数据类型列表
- ext2文件系统
- FL_RIDCore1.0 注册组件
- dll和so文件区别与构成
- 手动搭建ssh框架