现在的位置: 首页 > 综合 > 正文

iphone ios 用xcode4.2开发 访问web service的功能

2013年11月17日 ⁄ 综合 ⁄ 共 2641字 ⁄ 字号 评论关闭

1。后台利用 cxf 构建一个web service服务。

  • HelloWorld.java
[java] view
plain
copy

  1. /** 
  2.  *  
  3.  */  
  4. package com.alcor.jws.test;  
  5.   
  6. import javax.jws.WebMethod;  
  7. import javax.jws.WebService;  
  8.   
  9. import org.apache.cxf.feature.Features;  
  10.   
  11. /** 
  12.  * @author 徐泽宇(roamer) 
  13.  *  
  14.  *         2010-7-10 
  15.  */  
  16. @WebService  
  17. @Features(features = "org.apache.cxf.feature.LoggingFeature")  
  18. public interface HelloWorld {  
  19.   
  20.     @WebMethod   
  21.     String sayHi(String text);  
  22.     @WebMethod   
  23.       
  24.     boolean userLogon(String username,String userpasswd);  
  25. }  
  • HelloWorldImpl.java

[java] view
plain
copy

  1. /** 
  2.  *  
  3.  */  
  4. package com.alcor.jws.test;  
  5.   
  6. import org.apache.cxf.feature.Features;  
  7. import org.apache.log4j.Logger;  
  8.   
  9. import javax.jws.WebMethod;  
  10. import javax.jws.WebService;  
  11.   
  12.   
  13. /** 
  14.  * @author 徐泽宇(roamer) 
  15.  * 
  16.  * 2010-7-10 
  17.  */  
  18. @WebService  
  19. @Features(features = "org.apache.cxf.feature.LoggingFeature")  
  20. public class HelloWorldImpl implements HelloWorld {  
  21.     /** 
  22.      * Logger for this class 
  23.      */  
  24.     private static final Logger logger = Logger.getLogger(HelloWorldImpl.class);  
  25.       
  26.       
  27.         @WebMethod   
  28.         public String sayHi(String text) {  
  29.         if (logger.isDebugEnabled()) {  
  30.             logger.debug("sayHi(String) - start"); //$NON-NLS-1$  
  31.         }  
  32.   
  33.         String returnString = "Hello,你好: " + text;  
  34.         if (logger.isDebugEnabled()) {  
  35.             logger.debug("返回内容:"+returnString);  
  36.             logger.debug("sayHi(String) - end"); //$NON-NLS-1$  
  37.         }  
  38.             return returnString;  
  39.         }  
  40.           
  41.         @WebMethod  
  42.         public boolean userLogon(String username ,String userpasswd)  
  43.         {  
  44.             logger.debug("用户名是:"+username+"口令是:"+userpasswd);  
  45.             if (username.equalsIgnoreCase("admin"))  
  46.             {  
  47.                 return true;  
  48.             }else{  
  49.                 return false;  
  50.             }  
  51.         }  
  52. }  

  • java 的web service 访问客户端

  1. /** 
  2.  *  
  3.  */  
  4. package com.alcor.jws.test;  
  5.   
  6. import org.apache.cxf.interceptor.LoggingInInterceptor;  
  7. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  8.   
  9. /** 
  10.  * @author 徐泽宇(roamer) 
  11.  *  
  12.  *         2010-7-10 
  13.  */  
  14. public class Client {  
  15.   
  16.     private Client() {  
  17.     }  
  18.   
  19.     public static void main(String args[]) throws Exception {  
  20.         /*第一种方法,通过配置文件来实现  begin 
  21.         ApplicationContext ctx = new ClassPathXmlApplicationContext(    "META-INF/WebServiceClient.xml"); 
  22.          
  23.         HelloWorld client = (HelloWorld) ctx.getBean("client"); 
  24.  
  25.         String result = client.sayHi("Roamer"); 
  26.         System.out.println(result); 
  27.          
  28.         boolean logonResult = client.userLogon("roamer", "passwd"); 
  29.         System.out.println(logonResult); 
  30.          

抱歉!评论已关闭.