android的webserviceWord格式.docx
《android的webserviceWord格式.docx》由会员分享,可在线阅读,更多相关《android的webserviceWord格式.docx(37页珍藏版)》请在冰豆网上搜索。
axis2.war
axis2.war文件放到tomcat服务器的webapps目录下
axis2.war文件放到tomcat服务器的webapps目录下。
浏览器地址:
点击打开链接
表明axis2服务器端部署成功。
客户端可以用axis2-bin包中提供的wsdl2java.bat工具实现。
首先得配置环境。
(1)设置环境变量:
找到%AXIS2_HOME%\bin目录下的wsdl2java.bat.打开修改一下。
找到set_RUNJAVA="
%JAVA_HOME%\bin\java"
这一行下面加上一行
setJAVA_OPTS="
-Djava.ext.dirs=%AXIS2_HOME%\lib"
eclipse下创建axis2clientproject
cmd进入axis2client目录生成存根代码
运行命令:
WSDL2java–uri
http:
//localhost:
8080/axis2/services/helloService?
wsdl
服务端
新建—>
javaweb项目—>
在上面的页面中,配置好Axis2的基本的信息配置,点击ok,
目录如下:
在eclipse的标头上多了一个
接下来我们可以在此测试我们的axis2服务器
在服务端实现一个简单的加法运算
服务端的java代码:
packagexawl.unit.org;
publicclassService{
publicintadd(intx,inty){
System.out.println("
**********调用了add方法**************"
);
returnx+y;
}
publicintsub(intx,inty){
**********调用了sub方法**************"
returnx-y;
publicStringgetName(){
return"
********************"
;
}
客户端:
在客户端实现得到在web服务端的到的结果返回给android的客户端。
实现的代码如下:
packagecom.example.androidclient;
importjava.io.IOException;
importorg.ksoap2.SoapEnvelope;
importorg.ksoap2.serialization.SoapObject;
importorg.ksoap2.serialization.SoapSerializationEnvelope;
importorg.ksoap2.transport.HttpTransportSE;
importorg.xmlpull.v1.XmlPullParserException;
importandroid.annotation.SuppressLint;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassMainActivityextendsActivityimplementsOnClickListener{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Buttonbutton=(Button)this.findViewById(R.id.button1);
button.setOnClickListener(this);
publicbooleanonCreateOptionsMenu(Menumenu){
getMenuInflater().inflate(R.menu.activity_main,menu);
returntrue;
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.button1:
Stringnamespace="
//org.unit.xawl"
Stringwsalurl="
//10.0.2.2:
8080/webtest/services/Service"
Stringwebmethod="
add"
//doCalculate是Calculate类中的方法名zi
//
StringsoapAction=namespace+webmethod;
SoapObjectrequest=newSoapObject(namespace,webmethod);
//doCalculate(num1,num2)
request.addProperty("
x"
14);
y"
15);
//调用
SoapSerializationEnvelopeenvelope=
newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
//保存服务返回的值
HttpTransportSEse=newHttpTransportSE(wsalurl);
try{
//envelope装的是带有参数的一个核心的对象
//
se.call(soapAction,envelope);
if(envelope.getResponse()!
=null){
Objectresult=envelope.getResponse();
//SoapObjectresult1=(SoapObject)envelope.getResponse();
Toast.makeText(this,result.toString(),Toast.LENGTH_LONG).show();
}
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(XmlPullParserExceptione){
}
break;
default:
}
实现的效果如下:
Android调用天气预报的WebService简单例子
新建一个android的项目,构建项目的类库,将
ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar添加到类库中
接下来按以下的步骤来调用webservice
1.指定WebService的命名空间和调用方法
privatestaticfinalStringNAMESPACE="
//WebX
privatestaticfinalStringMETHOD_NAME="
getWeatherbyCityName"
SoapObjectrpc=newSoapObject(NAMESPACE,METHOD_NAME);
SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。
第二个参数表示要调用的WebService方法名。
2.设置调用方法的参数,如果没有参数,可以省略。
rpc.addProperty("
theCityName"
"
北京"
3.生成webservice的SOAP请求。
SoapSerializationEnvelopeenvelope=newSoapSerializatio