`
萧_瑟
  • 浏览: 161138 次
社区版块
存档分类
最新评论

基于CXF的webService本地数据交互----PC端与Android端(一)

阅读更多

1.下载CXF(我下载的是2.4.6)

http://cxf.apache.org/download.html

 

2.新建一个web项目,项目结构如附件所示。

为了方便,我们将下载的CXF包lib文件夹下的jar包全部导入到工程中。

(实际用到的jar包可看附件图)

 

3.服务端

 

   a.建一个服务接口     IHelloWorld.java

package com.royal.service;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {

	public String sayHi(@WebParam(name = "text") String text);

	public int getNumber();

	public String getNameAndAge(@WebParam(name = "nameid") String nameid, @WebParam(name = "number") int number);

	public List getList();

}

   其实我测试过了,不用@WebParam("")声明也行。

 

   b.服务接口的实现类       HelloWorldImpl.java

 

package com.royal.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import com.royal.service.IHelloWorld;

/**
 * 接口实现
 * 
 * @author Royal 注意:@webservice自动注入一定要写
 * 
 */
@WebService(endpointInterface = "com.royal.service.IHelloWorld", serviceName = "HelloWorldService")
public class HelloWorldImpl implements IHelloWorld {

	public String sayHi(String text) {
		return text + "Hi";
	}

	public int getNumber() {
		return 100;
	}

	public String getName(String nameid) {
		return nameid;
	}

	public List getList() {
		List list = new ArrayList<String>();
                list.add("萧瑟");
                list.add(10);	
		return null;
	}

}

 

   c.服务接口集合spring的配置      beanRefServer.xml

 

<?xml version="1.0" encoding="UTF-8"?>

	<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">


	<!-- Import Apache CXF Bean Definition -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<!-- implementor :接口类的实现类 address: 要和注释里面神秘的服务名对应, -->
	<jaxws:endpoint id="helloWorld"
		implementor="com.royal.serviceImpl.HelloWorldImpl" address="/HelloWorldService" />

</beans>

 

   d.web.xml的配置      web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<!-- Spring Config Location -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/beanRefServer.xml</param-value>
    </context-param>
    <!-- Spring ContextLoaderListener -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
     <!-- Apache CXFServlet -->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- CXFServlet Mapping -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 

  OK,这样服务端的配置就完成了,启动tomcat。

 

4.在浏览器中输入:http://localhost:服务器端口/项目名/services

     如果看见 :WSDL : {http://serviceImpl.royal.com/}HelloWorldService  , 则代表成功。

    点击WSDL链接,可看见WSDL文件内容。

 

 

=========================================================

小结:

  其配置流程步骤可认为

 

------sevlet:  拦截    注意name及mapping

<servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- CXFServlet Mapping -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

 

------新增自定义xml文件:

       作用:声明地址访问信息,根据指定的地址找到实现类

<!-- implementor :接口类的实现类 address: 要和注释里面神秘的服务名对应, -->
	<jaxws:endpoint id="xxxxxxx"
		implementor="xxxxxxxxx.xxxImpl" address="/xxxxxx" />

       (之后将该文件配置进web.xml,这样服务启动的时候就被加入了)

 

-------实现类:

        作用:匹配自定义xml文件中的地址名

                 可切入一个接口,暴露提供给用户

@WebService(endpointInterface = "xxxxx", serviceName = "xxxxxx")
public class xxxxxImpl implements xxxxxx {
}

 

这部分当做个人小结,可无视。

==================================================================

 

 

5.客户端

  写一个客户端测试类     CXFClient.java

 

package com.royal.client;

import java.util.List;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.royal.service.IHelloWorld;

public class CXFClient {
	
	public CXFClient(){
		
	}
	
	public static void main(String args[]) throws Exception {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.setServiceClass(IHelloWorld.class);  
		factory.setAddress("http://localhost:8090/cxf_webservice/services/HelloWorldService");
		IHelloWorld client = (IHelloWorld) factory.create();

		String hi = client.sayHi("萧_瑟");  
		int number = client.getNumber();
		String NameAge = client.getNameAndAge("萧_瑟",18);
		List list = client.getList();
		
		System.out.println("hi----" + hi);
		System.out.println("number----" + number);
		System.out.println("NameAge----" + NameAge);
		System.out.print("list----");
		for(int i = 0; i < list.size(); i++){
			System.out.print(list.get(i) + ",");
		}
	}

}

 

6.  打印结果:

hi----萧_瑟 Hi
number----100
NameAge----萧_瑟18
list----10,萧_瑟,

 

参考:http://rargers.iteye.com/blog/196121

 

android端代码

package com.royal.cxf_webservice;

import java.util.List;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Cxf_webservice_androidActivity extends Activity {
	private static String NAMESPACE = "http://service.royal.com/";
	// webService地址
	private static String URL = "http://10.0.2.2:8090/cxf_webservice/services/HelloWorldService";
	// private static String URL =
	// "http://192.168.17.76:8090/cxf_webservice/services/HelloWorldService";
	private String method_name = null;

	private Button okButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		okButton = (Button) findViewById(R.id.ok);
		okButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showTest();
			}
		});
	}

	private void showTest() {
		method_name = "sayHi";
		String text = "萧_瑟";
		sayHi(method_name, text);

		method_name = "getNumber";
		getNumber(method_name);  
		
		method_name = "getNameAndAge";
		String name = "萧_瑟";
		int age = 18;
		getNameAndAge(method_name,name,age);
		
		method_name = "getList";
		getList(method_name);
	}

	private void sayHi(String method_name, String text) {
		// (1) 指定webservice的命名空间和调用的方法名
		SoapObject rpc = new SoapObject(NAMESPACE, method_name);
		/**
		 * (2) 设置调用方法的参数值,如果没有参数,可以省略。 要注意的是,参数必须和服务声明的@WebParam里面的变量名对应
		 */
		rpc.addProperty("text", text);
		// (3) 生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// envelope.bodyOut = rpc;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		// (4)创建HttpTransportsSE对象。通过AndroidHttpTransport类的构造方法可以指定WebService的WSDL文档的URL
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			// (5)使用call方法调用WebService方法
			ht.call(NAMESPACE + method_name, envelope);
			// (6)使用getResponse方法获得WebService方法的返回结果
			if (envelope.getResponse() != null) {
				System.out.println(envelope.getResponse());
				String result = String.valueOf(envelope.getResponse());
				Toast.makeText(Cxf_webservice_androidActivity.this, result,
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getNumber(String method_name) {
		SoapObject rpc = new SoapObject(NAMESPACE, method_name);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + method_name, envelope);
			if (envelope.getResponse() != null) {
				System.out.println(envelope.getResponse());
				Integer result = Integer.valueOf(String.valueOf(envelope.getResponse()));
				Toast.makeText(Cxf_webservice_androidActivity.this, result.toString(),
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getNameAndAge(String method_name,String name,int age) {
		SoapObject rpc = new SoapObject(NAMESPACE, method_name);
		rpc.addProperty("name",name);
		rpc.addProperty("age", age);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + method_name, envelope);
			if (envelope.getResponse() != null) {
				System.out.println(envelope.getResponse());
				String result = String.valueOf(envelope.getResponse());
				Toast.makeText(Cxf_webservice_androidActivity.this, result,
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getList(String method_name) {
		SoapObject rpc = new SoapObject(NAMESPACE, method_name);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + method_name, envelope);
			if (envelope.getResponse() != null) {
				System.out.println(envelope.getResponse());
				List result = (List)envelope.getResponse();
				for(int i = 0; i < result.size(); i++){
					System.out.println(result.get(i).toString());
					Toast.makeText(Cxf_webservice_androidActivity.this, result.get(i).toString(),
							Toast.LENGTH_SHORT).show();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

 记得开启网络权限

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.royal.cxf_webservice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Cxf_webservice_androidActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
        <uses-permission android:name="android.permission.INTERNET"/>

</manifest>

 记得导入KSOAP包

 

 

  • 大小: 12.8 KB
  • 大小: 17.1 KB
分享到:
评论
2 楼 jackli11241201 2014-04-17  
为什么服务端没有jar包
1 楼 xucaishen 2013-12-08  
不知道神马原因,我的在调用call语句的时候好像掉入了无底洞,进入那一句之后,再也没有任何信息了。既没有报错,也没有返回,而且也不会执行下一句了。。。

相关推荐

    基于CXF的webService本地数据交互----PC端与Android端(二)

    在本篇博文中,我们将深入探讨如何利用Apache CXF实现Web Service进行本地数据交互,特别是在PC端和Android端之间的通信。Apache CXF是一个开源框架,它允许开发人员创建和消费各种Web服务,包括SOAP和RESTful风格的...

    基于CXF的webService本地数据交互----PC端与Android端(三)

    在本篇博文中,我们将深入探讨如何利用Apache CXF库创建一个基于Web Service的数据交互解决方案,特别是关注于PC端和Android端之间的本地通信。Apache CXF是一个开源框架,它允许开发者构建和部署SOAP(简单对象访问...

    基于CXF的webService本地数据交互----PC端(四)

    在本篇博文中,我们将深入探讨如何利用Apache CXF库创建和实现基于Web Service的本地数据交互,特别是在PC端的应用。Apache CXF是一个开源框架,它允许开发者构建和部署服务,支持多种Web服务规范,如SOAP、RESTful...

    cxf-rt-rs-client-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-rs-client-3.0.1.jar; 赠送原API文档:cxf-rt-rs-client-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-client-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-rs-client-3.0.1.pom; 包含...

    cxf-rt-transports-http-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-transports-http-3.0.1.jar; 赠送原API文档:cxf-rt-transports-http-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-transports-http-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-transports-...

    cxf-rt-frontend-jaxrs-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-jaxrs-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxrs-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxrs-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxrs...

    cxf-rt-frontend-simple-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-simple-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-simple-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-simple-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-...

    cxf-rt-frontend-jaxws-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-jaxws-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxws-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxws-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxws...

    cxf-rt-databinding-jaxb-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-databinding-jaxb-3.0.1.jar; 赠送原API文档:cxf-rt-databinding-jaxb-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-databinding-jaxb-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-...

    cxf-rt-bindings-soap-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-bindings-soap-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-soap-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-soap-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-soap-...

    cxf-core-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-core-3.0.1.jar; 赠送原API文档:cxf-core-3.0.1-javadoc.jar; 赠送源代码:cxf-core-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-core-3.0.1.pom; 包含翻译后的API文档:cxf-core-3.0.1-...

    WebService CXF --- 传输文件MTOM

    WebService CXF,全称Apache CXF,是一款开源的Java框架,用于构建和开发Web服务。它支持多种协议和规范,包括SOAP、RESTful、XML以及Web服务标准如WS-*。在"WebService CXF --- 传输文件MTOM"这个主题中,我们将...

    cxf-rt-rs-extension-providers-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-rs-extension-providers-3.0.1.jar; 赠送原API文档:cxf-rt-rs-extension-providers-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-extension-providers-3.0.1-sources.jar; 赠送Maven依赖信息...

    cxf-rt-bindings-xml-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-bindings-xml-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-xml-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-xml-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-xml-3.0.1....

    cxf-rt-transports-http-jetty-3.0.2.zip

    标题 "cxf-rt-transports-http-jetty-3.0.2.zip" 指的是Apache CXF项目的一个特定版本的HTTP-Jetty传输模块。Apache CXF是一个流行的开源框架,用于构建和开发服务导向架构(SOA)和RESTful Web服务。这个版本,...

    cxf-rt-ws-addr-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-ws-addr-3.0.1.jar; 赠送原API文档:cxf-rt-ws-addr-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-ws-addr-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-ws-addr-3.0.1.pom; 包含翻译后的API...

Global site tag (gtag.js) - Google Analytics