android怎么调用webservice

您所在的位置:网站首页 男孩鞋掉了一只视频大全集 android怎么调用webservice

android怎么调用webservice

2024-07-17 18:49:30| 来源: 网络整理| 查看: 265

WebService是一种基于SOAP协议的远程调用标准,通过webservice可以将不同操作系统平台、不同语言、不同技术整合到一块。在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方的SDK来调用WebService。PC版本的WEbservice客户端库非常丰富,例如Axis2,CXF等,但这些开发包对于Android系统过于庞大,也未必很容易移植到Android系统中。因此,这些开发包并不是在我们的考虑范围内。适合手机的WebService客户端的SDK有一些,比较常用的有Ksoap2,可以从http://code.google.com/p/ksoap2-android/downloads/list进行下载;将下载的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包复制到Eclipse工程的lib目录中,当然也可以放在其他的目录里。同时在Eclipse工程中引用这个jar包。

具体调用调用webservice的方法为:

(1) 指定webservice的命名空间和调用的方法名,如:

SoapObject request =new SoapObject(http://service,”getName”)

SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。第二个参数表示要调用的WebService方法名。

(2) 设置调用方法的参数值,如果没有参数,可以省略,设置方法的参数值的代码如下:

Request.addProperty(“param1”,”轮李value”)

Request.addProperty(“param2”,”value”)

要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的WebService类中的方法参侍桐铅数名一致,只要设置参数的顺序一致即可。

(3) 生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述,代码为:

SoapSerializationEnvelope envelope=new

SoapSerializationEnvelope(SoapEnvelope.VER11)

Envelope.bodyOut = request

创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。该版本号需要根据服务端WebService的版本号设置。在创建SoapSerializationEnvelope对象后,不要忘了设置SOAPSoapSerializationEnvelope类的bodyOut属性,该属性的值就是在第一步创建的SoapObject对象。

(4) 创建HttpTransportsSE对象。通过HttpTransportsSE类的构造方法可以指定WebService的WSDL文档的URL:

HttpTransportSE ht=new HttpTransportSE(“http://192.168.18.17:80

/axis2/service/SearchNewsService?wsdl”)

(5)使用call方法调用WebService方法,代码:

ht.call(null,envelope);

Call方法的第一个参数一般为null,第2个参数就是在第3步创建的SoapSerializationEnvelope对象。

(6)使用getResponse方法获得WebService方法的返回结果,代码:

SoapObject soapObject =( SoapObject) envelope.getResponse()

以下为简单的实现一个天气查看功能的例子老好:

publicclass WebService extends Activity {

privatestaticfinal String NAMESPACE ="http://WebXml.com.cn/"

// WebService地址

privatestatic String URL ="http://www.webxml.com.cn/

webservices/weatherwebservice.asmx"

privatestaticfinal String METHOD_NAME ="getWeatherbyCityName"

privatestatic String SOAP_ACTION ="http://WebXml.com.cn/

getWeatherbyCityName"

private String weatherToday

private Button okButton

private SoapObject detail

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.main)

okButton = (Button) findViewById(R.id.ok)

okButton.setOnClickListener(new Button.OnClickListener() {

publicvoid onClick(View v) {

showWeather()

}

})

}

privatevoid showWeather() {

String city ="武汉"

getWeather(city)

}

@SuppressWarnings("deprecation")

publicvoid getWeather(String cityName) {

try {

System.out.println("rpc------")

SoapObject rpc =new SoapObject(NAMESPACE, METHOD_NAME)

System.out.println("rpc"+ rpc)

System.out.println("cityName is "+ cityName)

rpc.addProperty("theCityName", cityName)

AndroidHttpTransport ht =new AndroidHttpTransport(URL)

ht.debug =true

SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(

SoapEnvelope.VER11)

envelope.bodyOut = rpc

envelope.dotNet =true

envelope.setOutputSoapObject(rpc)

ht.call(SOAP_ACTION, envelope)

SoapObject result = (SoapObject) envelope.bodyIn

detail = (SoapObject) result

.getProperty("getWeatherbyCityNameResult")

System.out.println("result"+ result)

System.out.println("detail"+ detail)

Toast.makeText(WebService.this, detail.toString(),

Toast.LENGTH_LONG).show()

parseWeather(detail)

return

} catch (Exception e) {

e.printStackTrace()

}

}

privatevoid parseWeather(SoapObject detail)

throws UnsupportedEncodingException {

String date = detail.getProperty(6).toString()

weatherToday ="今天:"+ date.split("")[0]

weatherToday = weatherToday +"\n天气:"+ date.split("")[1]

weatherToday = weatherToday +"\n气温:"

+ detail.getProperty(5).toString()

weatherToday = weatherToday +"\n风力:"

+ detail.getProperty(7).toString() +"\n"

System.out.println("weatherToday is "+ weatherToday)

Toast.makeText(WebService.this, weatherToday,

Toast.LENGTH_LONG).show()

}

}

具体调用调用webservice的方法为:

(1) 指定webservice的命名空间和调用的方法名,如:

SoapObject request =new SoapObject(http://service,”getName”)

SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。第二个参数表示要调用的WebService方法名。

(2) 设置调用方法的参数值,如果没有参数,可以省略,设置方法的参数值的代码如下:

Request.addProperty(“param1”,”value”)

Request.addProperty(“param2”,”value”)

要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致虚岩即可。

(3) 生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述,代码为:

SoapSerializationEnvelope envelope=new

SoapSerializationEnvelope(SoapEnvelope.VER11)

Envelope.bodyOut = request

创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。该版本号需要根据服务端WebService的版本号设置。在创建SoapSerializationEnvelope对象后,不要忘了设置SOAPSoapSerializationEnvelope类的bodyOut属性,该属性的值就是在第一步创建的SoapObject对象。

(4) 创建HttpTransportsSE对象。通过HttpTransportsSE类的构造方法可以指定WebService的WSDL文档的URL:

HttpTransportSE ht=new HttpTransportSE(“SearchNewsService?wsdl”)

(5)使用call方法调用WebService方法,代码:

ht.call(null,envelope);

Call方法的第一个参数一般为null,第2个参数就是氏锋在第3步创建的SoapSerializationEnvelope对象。

(6)使用getResponse方法获得差核御WebService方法的返回结果,代码:

SoapObject soapObject =( SoapObject) envelope.getResponse()

以下为简单的实现一个天气查看功能的例子:

复制代码

publicclass WebService extends Activity {

privatestaticfinal String NAMESPACE =""

// WebService地址

privatestatic String URL ="weatherwebservice.asmx"

privatestaticfinal String METHOD_NAME ="getWeatherbyCityName"

privatestatic String SOAP_ACTION ="getWeatherbyCityName"

private String weatherToday

private Button okButton

private SoapObject detail

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.main)

okButton = (Button) findViewById(R.id.ok)

okButton.setOnClickListener(new Button.OnClickListener() {

publicvoid onClick(View v) {

showWeather()

}

})

}

privatevoid showWeather() {

String city ="武汉"

getWeather(city)

}

@SuppressWarnings("deprecation")

publicvoid getWeather(String cityName) {

try {

System.out.println("rpc------")

SoapObject rpc =new SoapObject(NAMESPACE, METHOD_NAME)

System.out.println("rpc"+ rpc)

System.out.println("cityName is "+ cityName)

rpc.addProperty("theCityName", cityName)

AndroidHttpTransport ht =new AndroidHttpTransport(URL)

ht.debug =true

SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(

SoapEnvelope.VER11)

envelope.bodyOut = rpc

envelope.dotNet =true

envelope.setOutputSoapObject(rpc)

ht.call(SOAP_ACTION, envelope)

SoapObject result = (SoapObject) envelope.bodyIn

detail = (SoapObject) result

.getProperty("getWeatherbyCityNameResult")

System.out.println("result"+ result)

System.out.println("detail"+ detail)

Toast.makeText(WebService.this, detail.toString(),

Toast.LENGTH_LONG).show()

parseWeather(detail)

return

} catch (Exception e) {

e.printStackTrace()

}

}

privatevoid parseWeather(SoapObject detail)

throws UnsupportedEncodingException {

String date = detail.getProperty(6).toString()

weatherToday ="今天:"+ date.split("")[0]

weatherToday = weatherToday +"\n天气:"+ date.split("")[1]

weatherToday = weatherToday +"\n气温:"

+ detail.getProperty(5).toString()

weatherToday = weatherToday +"\n风力:"

+ detail.getProperty(7).toString() +"\n"

System.out.println("weatherToday is "+ weatherToday)

Toast.makeText(WebService.this, weatherToday,

Toast.LENGTH_LONG).show()

}

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://outofmemory.cn/yw/8238250.html



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭