Android中使用webservice验证用户登录的示例

前段时间做了2个Android方面的项目,现在想抽空对其中的有些知识点进行下总结,也算进一步的学习了。

由于开发手机客户端一般都要和服务器打交道,因此用户的登录验证在一般的应用中都少不了。因而我将以前做的项目中的使用webservice验证的这块

单独写出来了。我们的手机应用的服务器端采用的是Asp.net开发的,因而webservice的开发也是用C#开发的,发布在IIS上的。

在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方的SDK来调用WebService。PC版本的WebService库非常丰富,但这些对Android来说过于庞大。适合手机的WebService客户端的SDK有一些,比较常用的是KSOAP2。

KSOAP2 地址:http://code.google.com/p/ksoap2-android/

我们在项目中使用的是: ksoap2-android-assembly-2.4-jar-with-dependencies.jar。

在项目中引用KSOAP2后,我们就要引入下面几个包了:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

然后我们就要编写调用验证用户登录的方法,并在其中调用webservice方法了,代码如下:

public String GetUserWS(String methodName, String[] parameterList) {
// 创建SoapObject对象,并指定WebService的命名空间和调用的方法名
SoapObject request = new SoapObject(Config.NAMESPACE, methodName);
// 调用的函数如果有参数,这里可以设置需要传递的参数 注意:第一个参数使用arg0 多个参数依次类推 arg1,arg2...
if (parameterList != null) {
// for (int i = 0; i < parameterList.length; i++) {
request.addProperty("key", parameterList[0]);
request.addProperty("userName", parameterList[1]);
request.addProperty("passWord", parameterList[2]);

// }
}

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// envelope.setOutputSoapObject(request);
// 上边的一句等价于下边的这句 将SoapObject对象赋给envelope对象
envelope.bodyOut = request;
// 当前开发的是.net WS 这里需要不调用Java WS
envelope.dotNet = true;

/*
* 这里不要使用 AndroidHttpTransport ht = new AndroidHttpTransport(URL);
* 这是一个要过期的类
* 创建HttpTransportSE对象。通过HttpTransportSE类的构造方法可以指定WebService的WSDL文档的URL
*/

//这里的SOAP_GETUSERINFOACTION = "http://172.16.xx.xxx:3366/Service/EWineService.asmx?op=Mobile_GetUserInfo";
HttpTransportSE ht = new HttpTransportSE(Config.SOAP_GETUSERINFOACTION);

try {
// 请求WS
ht.call(Config.SOAP_ACTION, envelope);
if (envelope.getResponse() != null) {
// 获得WS函数返回值信息
// System.out.println(envelope.getResponse().toString());
Log.d("wine", "GetUserWS Result:"
+ envelope.getResponse().toString());
return envelope.getResponse().toString();
}

} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
Log.d("wine", "GetUserWS Error:" + e.getMessage());
}
return null;
}


具体的调用代码如下:

// 点确定按钮所执行的东东
String[] parameterList = new String[3];
parameterList[0] = LOGINKEY;
parameterList[1] = TxtUser.getText().toString();
parameterList[2] = TxtPassword.getText().toString();

//注意Config.METHOD_GETUSERINFO是具体调用的webservice中方法名,例如:METHOD_GETUSERINFO = "Mobile_GetUserInfo";
// 调用webService
String strRemoteInfo = GetUserWS(Config.METHOD_GETUSERINFO,
parameterList);





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Spring Boot集成Apache Axis2发布Web Service服务端的示例: 1. 首先,需要在pom.xml添加Axis2和Spring Boot的依赖: ```xml <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-webapp</artifactId> <version>1.7.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 创建一个简单的Web Service服务端: ```java package com.example.demo; import org.apache.axis2.context.MessageContext; import org.apache.axis2.transport.http.HTTPConstants; import org.springframework.stereotype.Component; @Component public class MyService { public String sayHello() { MessageContext messageContext = MessageContext.getCurrentMessageContext(); String userAgent = (String) messageContext.getProperty(HTTPConstants.HEADER_USER_AGENT); return "Hello, " + userAgent + "!"; } } ``` 3. 创建一个发布Web Service服务端的配置类: ```java package com.example.demo; import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; import org.apache.axis2.description.AxisService; import org.apache.axis2.engine.AxisConfiguration; import org.apache.axis2.transport.http.server.AxisHttpService; import org.apache.axis2.transport.http.server.HttpServiceHandler; import org.apache.axis2.transport.http.server.SimpleHttpServer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.net.InetSocketAddress; @Configuration public class WebServiceConfiguration { private SimpleHttpServer server; @Autowired private MyService myService; @PostConstruct public void start() throws Exception { ConfigurationContext configurationContext = ConfigurationContextFactory.createDefaultConfigurationContext(); AxisConfiguration axisConfiguration = configurationContext.getAxisConfiguration(); AxisService axisService = new AxisService("MyService"); axisService.addParameter("ServiceClass", MyService.class.getName()); axisService.addMethod("sayHello"); axisConfiguration.addService(axisService); HttpServiceHandler handler = new HttpServiceHandler(configurationContext); AxisHttpService axisHttpService = new AxisHttpService(axisConfiguration, handler); server = new SimpleHttpServer(); server.setBindAddress(new InetSocketAddress(8080)); server.deploy(axisHttpService); server.start(); } @PreDestroy public void stop() throws AxisFault { server.stop(); } @Bean public MyService myService() { return new MyService(); } } ``` 4. 运行Spring Boot应用程序并访问http://localhost:8080/axis2/services/MyService?wsdl,应该能够看到服务的WSDL描述。 现在,您已经成功地使用Spring Boot集成Apache Axis2发布Web Service服务端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值