- 浏览: 161138 次
最新评论
-
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
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包
- cxf_webservice.rar (10.1 KB)
- 下载次数: 71
- ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar (145.4 KB)
- 下载次数: 47
- cxf_webservice_android.rar (186.8 KB)
- 下载次数: 65
评论
发表评论
-
各地图API坐标系统比较与转换
2015-08-07 15:34 1023/** * 各地图API坐标系统比较与转换; * W ... -
基于ZBar条形码、二维码扫描Demo
2015-07-29 15:08 2039Demo样式: -
左滑删除Demo
2015-07-29 14:48 720参考摘录:http://blog.csdn.net ... -
Fragment + ViewPager 底部菜单 demo
2015-07-17 17:33 637如附件 -
Netty Websocket 例子
2015-01-08 14:36 1813Netty框架(Netty-3.5.7.Final)来实现W ... -
基于CXF的webService本地数据交互----PC端(四)
2013-04-26 17:20 1597不想新建项目了(可以参考前面一、二、三),就在现有的 ... -
LDAP简单的增删查改
2013-03-04 14:08 31121. 认证信息: package com.royal ... -
pushlet 推送框架简单使用Demo
2013-01-30 14:48 3253人懒了,有点不想写了,网上有好例子,帮忙宣传一下。 ... -
Java调用 SMS Cat发短信
2012-11-02 16:38 2308居然不能发表SMS Cat的中文名?! 这个是 SMS ... -
jersey demo
2012-08-28 16:33 2194同样是Maven构建的项目,不解释。 项目 ... -
验证码例子(jcaptcha插件的使用)
2012-07-20 11:45 2607和上一篇验证码的例子(kaptcha插件的使用)类 ... -
验证码例子(kaptcha插件的使用)
2012-07-19 17:35 2836使用kaptcha插件实现验证码小例子(顺便加了spri ... -
springMVC security Demo
2012-07-03 10:33 2496springMVC 结合权限控制。 项目目录结构 ... -
android googleMap key 的申请
2012-06-28 17:25 1207关于应用于android上 ... -
android phoneGap 静态页面中简单的数据传递
2012-06-27 17:14 2488最终效果: 主要采用方式: wi ... -
android PhoneGap JQuery Mobile Demo
2012-06-26 13:49 2952最终效果: 项目目录结构 ... -
Apache Mina 入门Demo
2012-06-11 14:45 8162什么是Mina,用Mina可以干什么? 官网 ... -
android PhoneGap 自定义插件
2012-06-08 17:16 2280以"发送短信"功能 自定义插件 ... -
android PhoneGap 入门
2012-06-08 09:57 986最终效果: 项目结构图: 需要用到Ph ... -
springMVC Demo
2012-06-07 16:32 1609项目目录结构图: 项目前需要用到的jar包 ...
相关推荐
在本篇博文中,我们将深入探讨如何利用Apache CXF实现Web Service进行本地数据交互,特别是在PC端和Android端之间的通信。Apache CXF是一个开源框架,它允许开发人员创建和消费各种Web服务,包括SOAP和RESTful风格的...
在本篇博文中,我们将深入探讨如何利用Apache CXF库创建一个基于Web Service的数据交互解决方案,特别是关注于PC端和Android端之间的本地通信。Apache CXF是一个开源框架,它允许开发者构建和部署SOAP(简单对象访问...
在本篇博文中,我们将深入探讨如何利用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-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-...
赠送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-...
WebService CXF,全称Apache CXF,是一款开源的Java框架,用于构建和开发Web服务。它支持多种协议和规范,包括SOAP、RESTful、XML以及Web服务标准如WS-*。在"WebService CXF --- 传输文件MTOM"这个主题中,我们将...
赠送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....
标题 "cxf-rt-transports-http-jetty-3.0.2.zip" 指的是Apache CXF项目的一个特定版本的HTTP-Jetty传输模块。Apache CXF是一个流行的开源框架,用于构建和开发服务导向架构(SOA)和RESTful Web服务。这个版本,...
赠送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...