Android客户端调用服务器端发布的WebService(CXF)

本文详细介绍了Android应用如何通过EditText控件获取用户输入,然后利用SOAP对象、SoapSerializationEnvelope和HttpTransportsSE等组件调用本地部署的WebService进行登陆验证。包括参数设置、命名空间、方法调用及异常处理的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以登陆为例,代码如下:

//从EditText控件中获取填入的内容
String username = fieldusername.getText().toString();
String password = fieldpassword.getText().toString();
//设置要访问的命名空间,WebService地址和WebService调用的方法名
//android模拟器访问本地Tomcat上部署的WebService时,不能使用localhost或本机IP,android默认访问本地地址是10.0.2.2
String NAMESPACE = "http://services.crm.nova.org/";
String SERVICEURL = "http://10.0.2.2:8080/CRM/services/userservice";
String method_name = "loginUser";
String WebMethod = NAMESPACE + method_name;
//为SOAP对象指定命名空间和方法名
SoapObject rpc = new SoapObject(NAMESPACE, method_name);
//设置调用方法的参数值,如果没有参数,可以省略。 要注意的是,参数必须和服务声明的@WebParam里面的变量名对应
rpc.addProperty("username", username);
rpc.addProperty("password", password);
//生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = false;
envelope.setOutputSoapObject(rpc);
//创建HttpTransportsSE对象。通过AndroidHttpTransport类的构造方法可以指定WebService的WSDL文档的URL
HttpTransportSE htse = new HttpTransportSE(SERVICEURL);
htse.debug = true;
        
try {
    htse.call(WebMethod, envelope);
    if(envelope.getResponse()!=null){
        SoapObject object = (SoapObject) envelope.bodyIn;
        TblUser tblUser = parseTblUser(object);
        Intent intent = new Intent();
        intent.setClass(AndroidCRM.this, UserView.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("KEY_TBLUSER", tblUser);
        intent.putExtras(bundle);
        startActivity(intent);
    }else{
        Toast.makeText(AndroidCRM.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
            }
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(AndroidCRM.this, e.getMessage(), Toast.LENGTH_LONG).show();
} catch (XmlPullParserException e) {
    e.printStackTrace();
    Toast.makeText(AndroidCRM.this, e.getMessage(), Toast.LENGTH_LONG).show();
}

NAMESPACE要看服务器端的WSDL文件获取,ServiceUrl就是将访问WebService的地址中的localhost用10.0.2.2这个ip来替换掉,envelope.dotNet这个值如果不是调用.Net的WebService最好设置成false,否则有可能参数那报错。
注意:从服务器端返回来的数据并不是我们期望的可序列化的对象,而是字符串,需要自己写方法(parseTblUser(object))来解析,这里只返回一个对象还比较简单,当返回集合的时候就会比较复杂了。

转载于:https://www.cnblogs.com/devilcx/archive/2012/05/08/2490566.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值