- 浏览: 160138 次
最新评论
-
bihongliang:
并且服务端一直报错:javax.bluetooth.Bluet ...
android 和 PC端 进行蓝牙通信 demo -
bihongliang:
你好,博主,我是windows10 64 位系统,加载了 64 ...
android 和 PC端 进行蓝牙通信 demo -
白云飘飘2016:
fcylf 写道win7 64位,pc端测试,报错:Nativ ...
android 和 PC端 进行蓝牙通信 demo -
fcylf:
win7 64位,pc端测试,报错:Native Librar ...
android 和 PC端 进行蓝牙通信 demo -
abc天残:
http://www.iteye.com/images/smi ...
android 从服务器下载更新新版本软件 demo
本篇基于(二)的基础上续写
主要是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)
不成功
不知道是不是不能这样传输对象
希望知道的大牛如果解决了可以告诉我 谢谢!
附件是结合(二)一起的全部代码
- json-lib-2.3-jdk15.jar (148.3 KB)
- 下载次数: 15
- ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar (145.4 KB)
- 下载次数: 16
- cxf_webservice.rar (29.8 KB)
- 下载次数: 31
- cxf_webservice_android.rar (191.7 KB)
- 下载次数: 22
发表评论
-
各地图API坐标系统比较与转换
2015-08-07 15:34 1014/** * 各地图API坐标系统比较与转换; * W ... -
基于ZBar条形码、二维码扫描Demo
2015-07-29 15:08 2030Demo样式: -
左滑删除Demo
2015-07-29 14:48 714参考摘录:http://blog.csdn.net ... -
Fragment + ViewPager 底部菜单 demo
2015-07-17 17:33 630如附件 -
Netty Websocket 例子
2015-01-08 14:36 1803Netty框架(Netty-3.5.7.Final)来实现W ... -
基于CXF的webService本地数据交互----PC端(四)
2013-04-26 17:20 1591不想新建项目了(可以参考前面一、二、三),就在现有的 ... -
LDAP简单的增删查改
2013-03-04 14:08 31041. 认证信息: package com.royal ... -
pushlet 推送框架简单使用Demo
2013-01-30 14:48 3226人懒了,有点不想写了,网上有好例子,帮忙宣传一下。 ... -
Java调用 SMS Cat发短信
2012-11-02 16:38 2275居然不能发表SMS Cat的中文名?! 这个是 SMS ... -
jersey demo
2012-08-28 16:33 2169同样是Maven构建的项目,不解释。 项目 ... -
验证码例子(jcaptcha插件的使用)
2012-07-20 11:45 2593和上一篇验证码的例子(kaptcha插件的使用)类 ... -
验证码例子(kaptcha插件的使用)
2012-07-19 17:35 2820使用kaptcha插件实现验证码小例子(顺便加了spri ... -
springMVC security Demo
2012-07-03 10:33 2469springMVC 结合权限控制。 项目目录结构 ... -
android googleMap key 的申请
2012-06-28 17:25 1195关于应用于android上 ... -
android phoneGap 静态页面中简单的数据传递
2012-06-27 17:14 2484最终效果: 主要采用方式: wi ... -
android PhoneGap JQuery Mobile Demo
2012-06-26 13:49 2949最终效果: 项目目录结构 ... -
Apache Mina 入门Demo
2012-06-11 14:45 8154什么是Mina,用Mina可以干什么? 官网 ... -
android PhoneGap 自定义插件
2012-06-08 17:16 2255以"发送短信"功能 自定义插件 ... -
android PhoneGap 入门
2012-06-08 09:57 962最终效果: 项目结构图: 需要用到Ph ... -
springMVC Demo
2012-06-07 16:32 1585项目目录结构图: 项目前需要用到的jar包 ...
相关推荐
在本篇博文中,我们将深入探讨如何利用Apache CXF实现Web Service进行本地数据交互,特别是在PC端和Android端之间的通信。Apache CXF是一个开源框架,它允许开发人员创建和消费各种Web服务,包括SOAP和RESTful风格的...
本文将深入探讨基于Apache CXF实现的Web Service在本地数据交互中的应用,尤其是如何在PC端与Android端之间进行通信。Apache CXF是一个开源框架,专门用于构建和消费Web服务,支持多种协议和标准,如SOAP、RESTful等...
在本篇博文中,我们将深入探讨如何利用Apache CXF库创建和实现基于Web Service的本地数据交互,特别是在PC端的应用。Apache CXF是一个开源框架,它允许开发者构建和部署服务,支持多种Web服务规范,如SOAP、RESTful...
赠送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; 包含...
赠送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-...
赠送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...
赠送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-...
赠送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...
赠送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-...
赠送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-...
赠送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依赖信息...
赠送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,全称Apache CXF,是一款开源的Java框架,用于构建和开发Web服务。它支持多种协议和规范,包括SOAP、RESTful、XML以及Web服务标准如WS-*。在"WebService CXF --- 传输文件MTOM"这个主题中,我们将...
赠送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...
赠送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-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载