androidc 调用webservicee方式有哪些

在Android中访问WebService接口的方法
字体:[ ] 类型:转载 时间:
  最近公司有个项目需要从Android平台访问WebService接口,实现向发布的函数传递对象。在网上找了一些资料,发现使用ksoap2可以调用WebService传递对象。
需要引入ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar 代码如下:  //WebService的命名空间  static final String namespace = "http://impl.";  //服务器发布的url  static final String url = http://10.100.3.41/axis2/services/UploadS  final String methodName = "upload"; // 函数名  final int sessionID = "111111";& //sessionID  //创建HttpTransportSE对象,通过HttpTransportSE类的构造方法可以指定WebService的url  HttpTransportSE transport = new HttpTransportSE(url);  transport.debug =  //指定WebService的命名空间和函数名  SoapObject soapObject = new SoapObject(namespace, methodName);  //设置调用方法参数的值  soapObject.addProperty("sessionID", sessionID); //sessionID  soapObject.addProperty("data", cds); //cds是需要传递的对象  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);  envelope.bodyOut =  envelope.setOutputSoapObject(soapObject);  //使用call方法调用WebService方法  transport.call(null, envelope);  SoapObject sb = (SoapObject) envelope.bodyIn;  String xmlMessage = sb.toString(); // 获取从服务器端返回的XML字符串
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具00:17 提问
android app开发时WEBSERVICE一般用什么技术?
最近试着写一个ANDROID的APP,比较短的时间内把客户端的内容过了一遍,现在
需要跟后台进行数据交互,数据库部分已经搞定。以前没有写过WEBSERVICE,想大
致了解一下需要用到什么技术,和简单的技术流程。
功能很简单,要求客户端提交简单的数据,WEBSERVICE完成接收数据,并操作服务器
端MYSQL数据库的功能,并返回从库中取出的值。应该如何开发WEB SERVICE?
按时间排序
webserive你直接用java来开发应可以了
可以参考以下链接
java开发webservice的几种方式
各种语言各种技术都可以,而且都很简单。比如说用asp.net mvc/web api,或者用node.js,或者用ruby on rails
说到WebSerivce,就必须要知道SOAP和WSDL,它们到底和WebSerice有着怎么的关系?Web Services是建立在HTTP、SOAP、WSDL等通用协议的基础之上。
SOAP(Simple Object Access Protocol,简单对象访问协议)是一种轻量级的、简单的、基于XML的协议,被设计用于在分布式环境中交换格式化和固化信息的简单协议。也就是说,要进行通信,进行数据访问传输,就必须依赖于一定的协议,而SOAP正是WebService通信中所依赖的一种协议。目前经常使用的SOAP协议有两个版本:SOAP 1.1 和 SOAP 1.2。
WSDL(Web Services Description Language,即Web服务描述语言)是一种用来描述Web服务的XML语言,它描述了Web服务的功能、接口、参数、返回值等,便于用户绑定和调用服务。它以一种和具体语言无关的方式定义了给定Web服务调用和应答的相关操作和消息。
在Android平台调用Web Service需要依赖于第三方类库ksoap2,它是一个SOAP Web service客户端开发包,主要用于资源受限制的Java环境如Applets或J2ME应用程序(CLDC/ CDC/MIDP)。
现在各家都支持开发webservice,JAVA, asp.net, python, node.js, ruby on rails,. 都有很多现成的框架,上手快
13501关注|1531收录
其他相似问题
相关参考资料5922人阅读
Android/OMS(322)
首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准。通过WebService可以将不同操作系统平台,不同语言、不同技术整合到一起。详细见:
Android中访问WebService总结有两种:1、通过链接 2、通过第三方类库
先说说第一种:
比较简单贴代码了:
final String SERVER_URL = &http://192.168.1.55/PosWebServices/WebUI.asmx&; // 定义需要获取的内容来源地址
URL url = new URL(SERVER_URL);
URLConnection con = url.openConnection();
//一些请求设置
con.setDoOutput(true);
con.setRequestProperty(&Pragma:&, &no-cache&);
con.setRequestProperty(&Cache-Control&, &no-cache&);
con.setRequestProperty(&Content-Type&, &text/xml&);
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
out.write(new String(xmlInfo.getBytes(&UTF-8&))); //这里可以发参数的,字符串应该是XML格式的
out.flush();
out.close();
// 取返回值
BufferedReader br = new BufferedReader(new InputStreamReader(con
.getInputStream()));
StringBuilder sBuilder = new StringBuilder();
String line = &&;
for (line = br.readLine(); line != line = br.readLine()) {
sBuilder.append(line);
// 解析XML
Pattern patternname = pile(&&Name&.*?&/Name&&);
Matcher matchername = patternname.matcher(sBuilder.toString());
if (matchername.find()) {
String name = matchername.group();
TextView lblname = (TextView) findViewById(R.id.lbl_name);
lblname.setText(URLDecoder.decode(name.substring(name
.indexOf(&&&) + 1, name.lastIndexOf(&&&))));
Pattern patternage = pile(&&Age&.*?&/Age&&);
Matcher matcherage = patternage.matcher(sBuilder.toString());
if (matcherage.find()) {
String age = matcherage.group();
TextView lblage = (TextView) findViewById(R.id.lbl_age);
lblage.setText(age.substring(age.indexOf(&&&) + 1, age
.lastIndexOf(&&&)));
} catch (Exception e) {
String str = e.getMessage();
很简单 不多说了 还可以用HttpPost加HttpResponse的方式。
需要下载一个第三方Jar包:ksoap2
相比旧版本增强了网络链接等等同时用HttpTransportSE 替代了AndroidHttpTransport ,建议用新版本
下载后导入工程,不多解释了
1. 指定WebService的命名空间和调用的方法名,代码如下:
SoapObject request = new SoapObject(&http://service&, &getName&);
2. 设置调用方法的参数值,如果方法没有参数,可以省略这一步。设置方法的参数值的代码如下:
request.addProperty(&m1&, &v1&);
request.addProperty(&m2&, &v2&);
3. 生成调用WebService方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述,代码如下: SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); envelope.bodyOut = 如果是.NET的WebService还需加:
4. 创建HttpTransportSE对象。通过HttpTransportSE类的构造方法可以指定WebService的WSDL文档的URL,代码如下:
HttpTransportSE ht = new HttpTransportSE(&http://192.168.1.55/PosWebServices/WebUI.asmx?wsdl&);
HttpTransportSE.dotNet=
5. 使用call方法调用WebService方法,代码如下: ht.call(null, envelope); call方法的第1个参数一般为null,第2个参数就是在第3步创建的SoapSerializationEnvelope对象。 6. 使用getResponse方法获得WebService方法的返回结果,代码如下: SoapObject soapObject = (SoapObject) envelope.getResponse();
7.取值
soapObject.getProperty(&这个名字你懂的&);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2654776次
积分:23835
积分:23835
排名:第171名
原创:20篇
转载:969篇
评论:321条
(2)(10)(1)(7)(13)(6)(4)(3)(9)(10)(3)(13)(6)(22)(14)(13)(19)(26)(50)(9)(12)(13)(47)(22)(31)(29)(18)(26)(12)(6)(23)(5)(12)(4)(10)(12)(79)(44)(86)(12)(9)(7)(21)(6)(10)(14)(23)(30)(4)(1)(3)(5)(3)(9)(2)(16)(3)(17)(5)(5)(8)(2)(3)(3)(20)(10)(14)(3)当前访客身份:游客 [
当前位置:
发布于 日 15时,
/**&&&*&查询服务器端的信息&&&*&@param&methodName&调用的方法名称&&&*&@param&params&调用WebService接口需要传入的参数&&&*&@return&&&*&@throws&Exception&&&&*/&&public&static&String&queryRemoteInfor(String&methodName,&String&params)&throws&Exception{&&&&String&result&=&&&;&&&&String&soapAction&=&Constants.NAME_SPACE&+&methodN//&SOAP&Action&&&&SoapObject&rpc&=&new&SoapObject(Constants.NAME_SPACE,&methodName);//&指定WebService的命名空间和调用的方法名&&&&//&设置需调用WebService接口需要传入的参数&&&&rpc.addProperty(&arg0&,&params);&&&&&&&&SoapSerializationEnvelope&envelope&=&new&SoapSerializationEnvelope(&&&&&&&&SoapEnvelope.VER10);//&生成调用WebService方法的SOAP请求信息,并指定SOAP的版本&&&&envelope.bodyOut&=&&&&&envelope.setOutputSoapObject(rpc);&&&&HttpTransportSE&transport&=&new&HttpTransportSE(&Constants.ENDPOINT&,60000);&&//&&HttpTransportSE&transport&=&SingletonHttpTransportSe.getInstance();&&&&try&{&&&&&&transport.call(soapAction,&envelope);&&&&&&//&获取返回的数据&&&&&&&&Object&response&=&envelope.getResponse();&&&&&&result&=&String.valueOf(response);&&&&&&if&(null&==&result&||result.equals(&anyType{}&)&||&result.equals(&[]&))&{&&&&&&&&result=&&;&&&&&&}&&&&&&&&&&return&&&&&}&catch&(Exception&e)&{&&&&&&e.printStackTrace();&&&&&&throw&new&Exception();&&&&}&&}
代码片段(1)
1.&[代码][Java]代码&&&&
* 查询服务器端的信息
* @param methodName 调用的方法名称
* @param params 调用WebService接口需要传入的参数
* @throws Exception
public static String queryRemoteInfor(String methodName, String params) throws Exception{
String result = "";
String soapAction = Constants.NAME_SPACE + methodN// SOAP Action
SoapObject rpc = new SoapObject(Constants.NAME_SPACE, methodName);// 指定WebService的命名空间和调用的方法名
// 设置需调用WebService接口需要传入的参数
rpc.addProperty("arg0", params);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
envelope.bodyOut =
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE( Constants.ENDPOINT ,60000);
// HttpTransportSE transport = SingletonHttpTransportSe.getInstance();
transport.call(soapAction, envelope);
// 获取返回的数据
Object response = envelope.getResponse();
result = String.valueOf(response);
if (null == result ||result.equals("anyType{}") || result.equals("[]")) {
result="";
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
开源中国-程序员在线工具:
相关的代码(1480)
开源从代码分享开始
-wangming-的其它代码tony_action 的BLOG
用户名:tony_action
文章数:70
评论数:415
访问量:601257
注册日期:
阅读量:3416
阅读量:279631
阅读量:1008208
阅读量:154551
51CTO推荐博文
&Android客户端调用WebService时,返回anyType{} ,实际上是因为调用WebService方法时,参数顺序错了,按照wsdl中定义的参数顺序写就不会有问题了.本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)

我要回帖

更多关于 c 调用webservice接口 的文章

 

随机推荐