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

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

阅读更多

本篇基于(二)的基础上续写

 

主要是JSON的数据交互。

 

1.项目结构图。

 

2.POM.XML   maven中导进需要的包  新增代码(注意这里没有给全,只是给出这篇需要的)

<!-- =============JSon====================== -->
<!-- =========这个我包下不下来  手动导入吧============== -->
<!-- 		<dependency> -->
<!-- 			<groupId>net.sf.json-lib</groupId> -->
<!-- 			<artifactId>json-lib</artifactId> -->
<!-- 			<version>2.3</version> -->
<!-- 		</dependency> -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>20040616</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>20030203.000129</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>20030211.134440</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
			<version>1.0.6</version>
		</dependency>
		<dependency>
			<groupId>xom</groupId>
			<artifactId>xom</artifactId>
			<version>1.2.5</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>20020423</version>
		</dependency>
		<dependency>
			<groupId>net.sf.morph</groupId>
			<artifactId>morph</artifactId>
			<version>1.1.1</version>
		</dependency>
 

3.IHelloWorld   新增代码

package com.royal.service;

import java.util.List;

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

import com.royal.json.Concentrator;

@WebService
public interface IHelloWorld {

        public Concentrator getConcentrator(@WebParam(name = "UID") String UID,
			@WebParam(name = "name") String name);
	// JSON 
	public String getJsonResult(@WebParam(name = "object") Object object);
	
	public String getJsonKeyValue(@WebParam(name = "object") Object object, @WebParam(name = "key") String key);
}

 

4.HelloWorldImpl   新增代码

package com.royal.serviceImpl;

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

import javax.jws.WebService;

import com.royal.json.Concentrator;
import com.royal.json.JSonDataFormatUtils;
import com.royal.service.IHelloWorld;

@WebService(endpointInterface = "com.royal.service.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {

	public Concentrator getConcentrator(String UID,String name){
		return new Concentrator(UID,name);
	}
	
	//  JSON
	public String getJsonResult(Object object){
		String result = null;
		try {
			result = JSonDataFormatUtils.resultJSonFormat(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result; 
	}
	
	public String getJsonKeyValue(Object object,String key){
		String result = null;
		try {
			result = JSonDataFormatUtils.resultJSonStringKeyValue(object, key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
 

5.Concentrator   简单的POJO类

package com.royal.json;

public class Concentrator{

	private String UID;

	private String name;

	public Concentrator() {

	}

	public Concentrator(String UID, String name) {
		this.UID = UID;
		this.name = name;
	}

	public String getUID() {
		return UID;
	}

	public void setUID(String uID) {
		UID = uID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

6.JSonDataFormatUtils  JSON数据转换工具类---有很多数据类型间的转换,可上网搜索。

package com.royal.json;

import net.sf.json.JSONObject;

/**
 * 
 * @author LONMID
 * 
 *         所依赖的包: json-lib-2.3-jdk15.jar, commons-collections.jar, commons-
 *         lang.jar, commons-logging.jar, commons-beanutils.jar,
 *         ezmorph-1.0.6.jar, xom-1.1.jar
 * 
 */
public class JSonDataFormatUtils {

	/**
	 * 根据对象获得JSON
	 * 
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public static String resultJSonFormat(Object object) throws Exception {
		String result = "";

		JSONObject jsonResult = JSONObject.fromObject(object);
		result = jsonResult.toString();

		return result;
	}

	/**
	 * 根据对象获得名称的值    比如---name:萧_瑟, 获得 萧_瑟
	 * @param object
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String resultJSonStringKeyValue(Object object, String key)
			throws Exception {
		String result = "";

		JSONObject jsonResult = JSONObject.fromObject(object);
		result = jsonResult.getString(key);

		return result;
	}

}

 

7.beanRefServer.xml  和  web.xml 就不贴了  和(二)的一样

 

8.还最重要的一个地方  由于我是maven的项目,所有在项目部署启动前你得勾上json.jar

右键项目---属性---Tomcat---devloader Classpath

这个小细节阻碍了我2天,由于没勾上,在进行测试的时候经常会报这样的错误:

 

[javax.xml.bind.JAXBException: class xxxxxxxx nor any of its super class is known to this context.]

或者是

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: net/sf/json/JSONObject
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)
    at $Proxy30.getJsonResult(Unknown Source)

之类的错误

 

后来经过debug,才发现少了JSONObject这个类,可是这个类明明是json.jar包里面带有的啊,为什么会找不到呢,然后想想spring的注入配置问题,试着在DevLoader Classpath勾上json.jar,神奇的就可以了,网上找了很多,各有各的理解,不知道其他人是不是这种情况,反正我的是,所以我觉得在做这些调研小例子的时候还是先不要用maven项目,用传统的web项目就好了。

 

9.服务部署完成了,浏览器访问是不是可以成功

输入地址:http://localhost:8090/cxf_webservice/services/HelloWorldService?wsdl

 

10.CXFClient   客户端测试   增加代码

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.json.Concentrator;
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();

		//传递对象交互
		Concentrator c = client.getConcentrator("DD154151", "江南路");
		
		System.out.println("\nconcentrator-----" + c.getUID() + "," + c.getName());
		
		//传递json交互  
		Concentrator concentrator = new Concentrator();
		concentrator.setUID("CC0215613");
		concentrator.setName("逸辉路集中器");
		String result = client.getJsonResult(concentrator);
		System.out.print(result);
		String UID = client.getJsonKeyValue(concentrator,"UID");
		System.out.println("UID:" + UID);//CC0215613
		String name = client.getJsonKeyValue(concentrator,"name");
		System.out.println("name:" + name);//逸辉路集中器
		
	}

}

 

11.PC端测试结果

 

12.android端   Cxf_webservice_androidActivity  新增代码

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;

import com.royal.model.Concentrator;

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 = "getConcentrator";
		String UID = "DD154151";
		name = "江南路";
		getConcentrator(method_name, UID, name);
		
		//传递json交互  
		Concentrator concentrator = new Concentrator();
		concentrator.setUID("CC0215613");
		concentrator.setName("逸辉路集中器");
		method_name = "getJsonResult";
		getJsonResult(method_name, concentrator);
	}

	private void getConcentrator(String method_name, String UID, String name) {
		SoapObject rpc = new SoapObject(NAMESPACE, method_name);
		rpc.addProperty("UID", UID);
		rpc.addProperty("name", 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());
				SoapObject result = (SoapObject) envelope.getResponse();
				System.out.println("UID:" + result.getProperty("UID") + ",name:"
						+ result.getProperty("name"));
				
				Toast.makeText(Cxf_webservice_androidActivity.this,
						result.getProperty("UID").toString(),
						Toast.LENGTH_SHORT).show();
				Toast.makeText(Cxf_webservice_androidActivity.this,
						result.getProperty("name").toString(),
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getJsonResult(String method_name,Object object) {
		SoapObject rpc = new SoapObject(NAMESPACE, method_name); 
		rpc.addProperty("object", object); 
//		PropertyInfo pi = new PropertyInfo();
//		pi.setName("object");
//		pi.setValue(object);
//		pi.setType(object.getClass());
//		rpc.addProperty(pi);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		envelope.addMapping(NAMESPACE, "Concentrator", object.getClass());
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			System.out.println("0");
			ht.call(NAMESPACE + method_name, envelope);
			System.out.println("1");
			if (envelope.getResponse() != null) {
				System.out.println("这里" + envelope.getResponse()); 
//				SoapObject result = (SoapObject) envelope.getResponse();
//				System.out.println("UID:" + result.getProperty("UID") + ",name:"
//						+ result.getProperty("name"));
//				
//				Toast.makeText(Cxf_webservice_androidActivity.this,
//						result.getProperty("UID").toString(),
//						Toast.LENGTH_SHORT).show();
			}else{
				System.out.println("null");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

客户端  别忘了导入KSOAP2 jar包

 

测试结果:

 

getConcentrator(String method_name, String UID, String name)

方法测试成功。

 

getJsonResult(String method_name,Object object) 

不成功

 

不知道是不是不能这样传输对象

 

希望知道的大牛如果解决了可以告诉我  谢谢!

 

附件是结合(二)一起的全部代码

 

 

 

 

  • 大小: 23.5 KB
  • 大小: 107.1 KB
  • 大小: 36.1 KB
  • 大小: 21.7 KB
分享到:
评论

相关推荐

    基于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是一个开源框架,专门用于构建和消费Web服务,支持多种协议和标准,如SOAP、RESTful等...

    基于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-...

    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....

    WebService CXF --- 传输文件MTOM

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

    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...

    cxf-rt-frontend-jaxws-3.0.16.jar 下载

    cxf-rt-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载

Global site tag (gtag.js) - Google Analytics