Android调用WebService
这两天给老师做地铁app的demo,与后台的交互要用WebService,还挺麻烦的。所以想写点,希望有用。
Web Services(Web服务)是一个用于支持网络间不同机器互操作的软件系统,它是一种自包含、自描述和模块化的应用程序,它可以在网络中被描述、发布和调用,可以将它看作是基于网络的、分布式的模块化组件。它建立在HTTP,SOAP,WSDL这些通信协议之上,可以轻松的跨平台。
我们用的WebService就是服务器公布的一个接口,连上之后可以交互。WSDL是一份XML文档,它描述了Web服务的功能、接口、参数、返回值等,便于用户绑定和调用服务。它以一种和具体语言无关的方式定义了给定Web服务调用和应答的相关操作和消息。
比如我们内部的wsdl地址:http://172.16.1.59:8081/authpay/DataProcessBean?wsdl(这是内网的地址外面登不进去啦)。
开头是这样,
<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://dataprocess.sw.hhjt.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="DataProcessBeanService" targetNamespace="http://dataprocess.sw.hhjt.com/">
<wsdl:types>
<xs:schema xmlns:tns="http://dataprocess.sw.hhjt.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://dataprocess.sw.hhjt.com/" version="1.0">
<xs:element name="Vey_Bnd_Req" type="tns:Vey_Bnd_Req"/>
<xs:element name="Vey_Bnd_ReqResponse" type="tns:Vey_Bnd_ReqResponse"/>
<xs:element name="Vey_Data_Col" type="tns:Vey_Data_Col"/>
<xs:element name="Vey_Data_ColResponse" type="tns:Vey_Data_ColResponse"/>
<xs:element name="Vey_Data_Send" type="tns:Vey_Data_Send"/>
<xs:element name="Vey_Data_SendResponse" type="tns:Vey_Data_SendResponse"/>
<xs:element name="Vey_Sell_Ticket" type="tns:Vey_Sell_Ticket"/>
<xs:element name="Vey_Sell_TicketResponse" type="tns:Vey_Sell_TicketResponse"/>
<xs:element name="Vey_Token_Download" type="tns:Vey_Token_Download"/>
<xs:element name="Vey_Token_DownloadResponse" type="tns:Vey_Token_DownloadResponse"/>
第一行有targetNamespace也即命名空间,在下面有调用的方法名称,比如我用了Vey_Token_Download,EndPoint一般是将WSDL地址末尾的"?wsdl"去除后剩余的部分;而SOAP Action通常为命名空间 + 调用的方法名称。
// 命名空间
String nameSpace = "http://dataprocess.sw.hhjt.com/";
// 调用的方法名称
String methodName = "Vey_Token_Download";
// EndPoint
String endPoint = "http://172.16.1.59:8081/authpay/DataProcessBean";
// SOAP Action
String soapAction = "http://dataprocess.sw.hhjt.com/Vey_Token_Download";
//调用Service 成功之后 new Ticket 存入数据库 失败则告诉他失败了
SoapObject rpc = new SoapObject(nameSpace,methodName); BindTicket bt = new BindTicket();
bt.setProperty(4,simnum);
bt.setProperty(6, useraccount);
bt.setProperty(7, userpassword); PropertyInfo proInfo = new PropertyInfo();
proInfo.setName("arg0");
proInfo.setValue(bt);
proInfo.setType(bt.getClass());
// 设置需调用WebService接口需要传入的参数
rpc.addProperty(proInfo); // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = false;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc); HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 调用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
} // 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
// String result = object.getProperty("ret").toString();
String result = object.getProperty(0).toString();
Log.v("result", result);
String flag = result.split(";")[1].split("=")[1];
Log.v("flag",flag);
exMessage = result.split(";")[0].split("=")[1];
Log.v("exmessage",exMessage);
值类型的传递通过ksoap可以直接进行传递,这里就不多说了!利用ksoap,值类型的变量即可作参数,也可以当作返回值。
但是这里要传的是对象,要编写实体类,且按照参数序列化设置好,demo里的实体类就照搬了一下。
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo; import java.util.Hashtable; public class BindTicket implements KvmSerializable { protected String bndReqDate;
protected String bndTcomAccount;
protected String bndTcomPass;
protected long bndTcomType;
protected String bndTelNo;
protected long bndUID;
protected String bndUserName;
protected String bndUserPass;
protected String isSelled;
protected String password; @Override
public Object getProperty(int i) {
switch (i){
case 0:
return bndReqDate;
case 1:
return bndTcomAccount;
case 2:
return bndTcomPass;
case 3:
return bndTcomType;
case 4:
return bndTelNo;
case 5:
return bndUID;
case 6:
return bndUserName;
case 7:
return bndUserPass;
case 8:
return isSelled;
case 9:
return password;
}
return null;
} @Override
public int getPropertyCount() {
return 10;
} @Override
public void setProperty(int i, Object o) {
switch (i){
case 0:
bndReqDate = o.toString();
break;
case 1:
bndTcomAccount = o.toString();
break;
case 2:
bndTcomPass = o.toString();
break;
case 3:
bndTcomType = Long.parseLong(o.toString());;
break;
case 4:
bndTelNo = o.toString();
break;
case 5:
bndUID = Long.parseLong(o.toString());;
break;
case 6:
bndUserName = o.toString();
break;
case 7:
bndUserPass =o.toString();
break;
case 8:
isSelled = o.toString();
break;
case 9:
password = o.toString();
break;
default:
break;
}
} @Override
public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
switch (i){
case 0:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "bndReqDate";
break;
case 1:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "bndTcomAccount";
break;
case 2:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "bndTcomPass";
break;
case 3:
propertyInfo.type = PropertyInfo.LONG_CLASS;
propertyInfo.name = "bndTcomType";
break;
case 4:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "bndTelNo";
break;
case 5:
propertyInfo.type = PropertyInfo.LONG_CLASS;
propertyInfo.name = "bndUID";
break;
case 6:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "bndUserName";
break;
case 7:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "bndUserPass";
break;
case 8:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "isSelled";
break;
case 9:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "password";
break;
default:
break;
}
}
}
最后记得在AndroidManifest.xml中配置添加访问网络的权限
<uses-permission android:name="android.permission.INTERNET" />
Android调用WebService的更多相关文章
- Android调用WebService(转)
Android调用WebService WebService是一种基于SOAP协议的远程调用标准,通过 webservice可以将不同操作系统平台.不同语言.不同技术整合到一块.在Android SD ...
- 纠正网上乱传的android调用Webservice方法。
1.写作背景: 笔者想实现android调用webservice,可是网上全是不管对与错乱转载的文章,结果不但不能解决问题,只会让人心烦,所以笔者决定将自己整理好的能用的android调用webser ...
- 第十五章:Android 调用WebService(.net平台)
什么是webservice? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和 ...
- Android 调用webService(.net平台)
什么是webservice? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和 ...
- 【Android进阶】Android调用WebService的实现
最近想自己搞搞服务器,就从最简单的webservice开始吧 先上效果图 项目结构 开始贴代码,注释都有,有问题的请留言 MainActivity.java package com.example.w ...
- 网摘Android调用WebService
这边特别注意调用的.net WCF 接口的绑定方式.以前一直用的wxHttpbinding,一直连不上.改成BasicHTTPbinding就能连上了 上篇文章已经对Web Service及其相关知识 ...
- android调用webservice接口获取信息
我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...
- Android 调用 WebService
1.WebService简介 PS:如果看完上面简介还不是很清楚的话,那么就算了,之前公司就用C#搭的一个WebService! 本节我们并不讨论如何去搭建一个WebService,我们仅仅知道如何去 ...
- ksoap2 android 调用WebService
webService,soap,wsdl的基本概念? 详情请看维基百科 基于soap 1.1, soap 1.2 的请求和响应数据源 查找了很久都是基于json格式传输数据,但是最终还是找到了基于xm ...
随机推荐
- Tomcat数据源(DataSource)简介
JDBC2.0提供了javax.sql.DataSource接口,它负责建立与数据库的连接,在应用程序中访问数据库时不必编写连接数据库的代码,可以直接从数据源获得数据库连接 1.数据库和连接池 在Da ...
- MySql 修改列的注释信息的方法
1. 问题 已经有很多数据的按照业务逻辑分表的一系列表修改一个字段(类型,如-1:默认值,1:表示'人员id',2:表示'公司id')的注释2. 解决方法 1> 使用alter ...
- mysql主从之slave-skip-errors和sql_slave_skip_counter
一般来说,为了保险起见,在主从库维护中,有时候需要跳过某个无法执行的命令,需要在slave处于stop状态下,执行 set global sql_slave_skip_counter=1以跳过命令.但 ...
- ArcGIS Engine开发之量测功能
1.距离测量 距离测量时,片段长度通过两点之间距离计算得到,全部长度通过片段长度的和计算得到.主要用到INewLineFeedback和IScreenDisplay两个接口. 1)INewLineFe ...
- Android Weekly Notes Issue #230
Android Weekly Notes Issue #230 November 6th, 2016 Android Weekly Issue #230. Android Weekly笔记, 本期内容 ...
- RecyclerView如何消除底部的分割线
最近遇到一个问题,用RecyclerView显示数据,纵向列表显示,添加默认分割线. 问题是:底部也会显示分割线,这很影响美观. 怎么解决这个问题呢?我想了很多办法,毫无头绪... 最后, ...
- Android InputType详解
android:inputType 如果设置android:inputType = "number",则默认弹出的输入键盘为数字键盘,且输入的内容只能为数字. InputType文 ...
- IOS 网络-深入浅出(一 )-> 三方SDWebImage
首要我们以最为常用的UIImageView为例介绍实现原理: 1)UIImageView+WebCache: setImageWithURL:placeholderImage:options: 先显 ...
- seL4之hello-3征途
seL4之hello-3征途 回顾上周 了解seL4的启动流程和初始化线程 了解seL4的几种内核对象和权能机制 完成hell0-2的运行. 补充上周 1.找到根任务(初始化线程)的创建具体的位置(那 ...
- 关于bundle install 的一点补充
在第一次运行bundle install之后,生成了Gemfile.lock文件,里面记录gem的具体版本号,按照官方文档说明,以后运行bundle install就不会再依据Gemfile,而是根据 ...