ssh整合webservice cxf采用注解的方式+andriod客户端

定义接口和实现类

/**

*自定义service接口

*

*@time9:01:54PM

*@authorretacnyue

*@[email protected]

*/

@WebService

publicinterfacePUserService{

@WebMethod

publicStringseyHello(@WebParam(name="name")

Stringname);

@WebMethod

publicStringsaveUser(@WebParam(name="email")

Stringemail,@WebParam(name="nickName")

StringnickName,@WebParam(name="pwd")

Stringpwd);

@WebMethod

publicStringgetUser(@WebParam(name="email")

Stringemail,@WebParam(name="pwd")

Stringpwd);

}

/**

*自定义接口实现

*

*@time9:01:46PM

*@authorretacnyue

*@[email protected]

*/

@WebService

publicclassPUserServiceImplimplementsPUserService{

privateBaseDao<User>userDao;

//注入userdao

@Resource(name="userDao")

publicvoidsetUserDao(BaseDao<User>userDao){

this.userDao=userDao;

}

publicStringseyHello(Stringname){

System.out.println("进入服务器端的sayHello方法");

return"你好"+name;

}

/**

*注册用户

*/

publicStringsaveUser(Stringemail,StringnickName,Stringpwd){

System.out.println("进入服务器端的saveUser方法");

Useruser=newUser();

user.setEmail(email);

user.setNickName(nickName);

user.setPassword(pwd);

user.setRegDate(newDate());

//

userDao.saveEntity(user);

return"success";

}

/**

*查询用户

*/

publicStringgetUser(Stringemail,Stringpwd){

System.out.println("进入服务器端的getUser方法"+email+"===pwd==="+pwd);

List<User>userList=userDao.findEntityByHQL("fromUseruwhereu.email=?andu.password=?",email,pwd);

if(!ValidateUtil.isValidate(userList)){

return"error";

}

System.out.println("user==============="+userList.get(0).getEmail());

return"success";

}

}

web.xml中的配置

<!-- cxf的设置 -->

<servlet>

<servlet-name>CXFServlet</servlet-name>

<servlet-class>

org.apache.cxf.transport.servlet.CXFServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>CXFServlet</servlet-name>

<url-pattern>/services/*</url-pattern>

</servlet-mapping>

spring配置文件

<!-- service服务 -->

<beanid="pService"

class="cn.yue.lsp.service.impl.PUserServiceImpl">

</bean>

<!--访问路径-->

<jaxws:endpointid="puserService"implementor="#pService"

address="/puserService" />

注:如果在是作声明式事务通知,在接口中的方法一定不要随意声明,会报hibernate session no bond的错误

android 客户端

需要添加第三方包

ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar

/**

*userService接口实现

*

*@time上午9:57:01

*@authorretacnyue

*@[email protected]

*/

publicclassUserServiceImplimplementsUserService{

/**

*注册用户

*/

@Override

publicStringsaveEntity(Useruser){

Mylog.i("tag","addusersuccess!");

//=========================================================

//创建HttpTransportSE对象

HttpTransportSEhtse=newHttpTransportSE(Constants.SERVICE_URL);

//使用soap1.1协议,创建SoapSerializationEnvelope对象

SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);

//创建soapObject对象,参数为命名空间和webService方法

SoapObjectrequest=newSoapObject(Constants.SERVICE_NAMESPACE,Constants.METHOD_ADDUSER);

//添加参数

//request.addProperty("name",user.getEmail());

request.addProperty("email",user.getEmail());

request.addProperty("nickName",user.getNickName());

request.addProperty("pwd",user.getPassword());

//设置soap消息体

//envelope.setOutputSoapObject(request);

envelope.bodyOut=request;

Stringstr=null;

try{

//调用对象的call方法,

htse.call(null,envelope);

if(envelope.getResponse()!=null){

Log.i("tag","返回正常");

//取得返回的soapObject对象

SoapObjectresult=(SoapObject)envelope.bodyIn;

str=result.getProperty(0).toString();

Log.i("tag","返回值为:"+str);

returnstr;

}

}catch(IOExceptione){

e.printStackTrace();

}catch(XmlPullParserExceptione){

e.printStackTrace();

}

Log.i("tag","无返回值");

returnstr;

}

}

未设置超时时间,网络出问题需要作处理

相关推荐