天天看点

eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)

eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)

接上篇博客,本篇博客主要包含两个内容:

4.使用Android studio创建webservice客户端APP访问服务端。

5.开发过程中可能出现的一些错误。

闲话少叙,直奔主题。

4.使用Android studio创建webservice 客户端APP访问服务端

先介绍操作,再介绍原理。

手机端运行时,必须和电脑端在同一局域网下。

使用Android studio创建一个项目,使用这个下载链接 https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/ksoap2-android/ksoap2-android-assembly-2.4-jar-with-dependencies.jar 下载soap的jar包。

在AndroidManifest.xml中加入网络访问权限代码:

  1. <!-- 访问网络的权限 -->
  2. <uses-permission android:name="android.permission.INTERNET" />

左边列表使用project,项目名称-----app----libs,然后把上面地址下载的jar包复制进去,在jar包上右击,选择下面的add as library即可导入包。

这是我的项目目录结构图,

eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)

下面依然粘贴代码:

界面代码Activity_main.xml,只有一个输入框,一个显示textView,一个按钮,

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"
  5.     xmlns:tools="http://schemas.android.com/tools"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context="com.dyf.sharedpsclient.MainActivity">
  9.     <EditText
  10.         android:id="@+id/id_et_input"
  11.         android:layout_width="148dp"
  12.         android:layout_height="wrap_content"
  13.         android:ems="10"
  14.         android:inputType="textPersonName"
  15.         />
  16.     <TextView
  17.         android:id="@+id/id_tv_show"
  18.         android:layout_width="wrap_content"
  19.         android:text="Hello World!"
  20.         app:layout_constraintBottom_toBottomOf="parent"
  21.         app:layout_constraintLeft_toLeftOf="parent"
  22.         app:layout_constraintRight_toRightOf="parent"
  23.         app:layout_constraintTop_toTopOf="parent"/>
  24.     <Button
  25.         android:id="@+id/id_btn_get"
  26.         android:text="获取信息"
  27.         tools:layout_editor_absoluteX="147dp"
  28.         tools:layout_editor_absoluteY="321dp"/>
  29. </LinearLayout>

操作代码MainActivity.java,主要包括初始化控件,按钮的点击操作,开启新线程发送webservice请求,接收返回结果,使用Handler修改界面信息。

  1. package com.dyf.sharedpsclient;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import org.ksoap2.SoapEnvelope;
  12. import org.ksoap2.serialization.SoapObject;
  13. import org.ksoap2.serialization.SoapSerializationEnvelope;
  14. import org.ksoap2.transport.HttpTransportSE;
  15. public class MainActivity extends Activity implements View.OnClickListener
  16. {
  17.     private TextView tv = null;
  18.     private Button btn_Get = null;
  19.     private EditText et_Input = null;
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState)
  22.     {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.         tv = (TextView) findViewById(R.id.id_tv_show);
  26.         btn_Get = (Button) findViewById(R.id.id_btn_get);
  27.         et_Input = (EditText) findViewById(R.id.id_et_input);
  28.         //直接调用webservice中的getParklotName方法
  29.         btn_Get.setOnClickListener(this);
  30.     }
  31.     private String callService()
  32.         //命名空间
  33.         String nameSpace = "http://dao.diy.com";
  34.         //serviceURL
  35.         String serviceURL = "http://10.201.23.77:8080/webservice";
  36.         //调用的方法名称
  37.         String methodName = "sayHello";
  38.         //创建HttpTransportSE传输对象
  39.         HttpTransportSE transport = new HttpTransportSE(serviceURL);
  40.         //transport.debug = true;
  41.         //使用Soap1.1创建SoapSerializationEnvelope对象
  42.         SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  43.         //实例化SoapObject对象
  44.         SoapObject request = new SoapObject(nameSpace, methodName);
  45.         String soapAction = nameSpace +"/"+ methodName;
  46.         String inputText = et_Input.getText().toString();
  47.         Log.i("inputText:",inputText);
  48.         request.addProperty("arg0", inputText);
  49.         envelop.dotNet = true;
  50.         envelop.bodyOut = request;
  51.         envelop.setOutputSoapObject(request);
  52.         envelop.encodingStyle = "UTF-8";
  53.         //调用webservice
  54.         try
  55.         {
  56.             transport.call(soapAction, envelop);
  57.             Log.i("envelop.getresponse:", envelop.getResponse().toString());
  58.             if (envelop.getResponse().toString() != null)
  59.             {
  60.                 SoapObject result = (SoapObject) envelop.bodyIn;
  61.                 String word = result.getProperty(0).toString();
  62.                 return word;
  63.                 //tv.setText(word);
  64.             }
  65.         } catch (Exception e)
  66.             Log.i("调用webservice出错:",e.toString());
  67.             e.printStackTrace();
  68.             return "no";
  69.         }
  70.         return null;
  71.     //定义Handler对象
  72.     private Handler handler = new Handler(){
  73.         @Override
  74.         public void handleMessage(Message msg)
  75.             String str= null;
  76.             switch (msg.what)
  77.                 case 1:
  78.                     str = msg.obj.toString();
  79.                     tv.setText(str);
  80.     };
  81.     /**
  82.      * Called when a view has been clicked.
  83.      *
  84.      * @param v The view that was clicked.
  85.      */
  86.     public void onClick(View v)
  87.         switch (v.getId())
  88.             case R.id.id_btn_get:
  89.                 new Thread() {
  90.                     @Override
  91.                     public void run() {
  92.                         // 你要执行的方法
  93.                         Log.i("tag","new thread");
  94.                         Message message = new Message();
  95.                         message.what = 1;
  96.                         message.obj = callService();
  97.                         Log.i("message.what", String.valueOf(message.what));
  98.                         Log.i("message.obj",message.obj.toString());
  99.                         // 执行完毕后给handler发送消息
  100.                         handler.sendMessage(message);
  101.                     }
  102.                 }.start();
  103.                 break;
  104. }

运行之后的结果:左边是软件一进入的界面,右边是输入框输入之后,点击获取信息按钮,然后显示服务端返回的内容。

eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)

下面介绍一下这个程序中的代码。

按照程序流程一步步进行,首先是103行的点击按钮的click事件,定义一个Message对象,接收返回结果使用,115行的message.obj = callService();使用callService方法,得到服务端返回结果,然后由handler发送消息,82行判断之后,修改主界面信息。其中callService方法是一个主要过程,下面重点说明一下。

首先39行声明命名空间nameSpace,值就是服务器端提供服务接口所在包倒写,最后不加 "/" ,否则下面运行时eclipse服务端会报 下面这个错误,(主要看第二行,楼主就在这个地方坑了两天)

  1. 警告: Interceptor for {http://dao.diy.com}SayHelloService#{http://dao.diy.com}sayHello has thrown exception, unwinding now
  2. org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://dao.diy.com/}sayHello found. Expected {http://dao.diy.com}sayHello.
  3.    at org.apache.cxf.wsdl.interceptors.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:107)
  4.    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
  5.    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
  6.    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:267)
  7.    at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:234)
  8.    at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:76)
  9.    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1129)
  10.    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1065)
  11.    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
  12.    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
  13.    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
  14.    at org.eclipse.jetty.server.Server.handle(Server.java:499)
  15.    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
  16.    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:258)
  17.    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
  18.    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
  19.    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
  20.    at java.lang.Thread.run(Thread.java:745)

然后41行定义服务访问的serviceURL,值就是服务端发布时的发布地址,结尾不要加 "/" ,不加 "?wsdl"。

43行定义调用的方法名称,注意名称和大小写别写错。

52行的soapAction ,注意是nameSpace+"/"+methodName,别忘了这个 "/" ,如果没有,服务端控制台那里报错,会打出这个soapaction,自己一看就明白了。

55行的request.addProperty(key ,value),这句是添加参数的,因为上面的sayHello方法只有一个参数,所以这里写了一次,如果有多个参数,则这里写对应个数的参数即可。

63行发起请求。

65行判断返回值不为空的情况,在67行可能会出现一个问题,使用SoapObject类时,Android studio控制台可能会报

java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive cannot be cast to org.ksoap2.serialization.SoapObject , 类转换错误,出现这个错误的话,把SoapObject改为Object就可以了,网上有人说是版本的问题,楼主也不是很清楚,但是如果出现这个错误,改为Object就可以了。

另外67行还有一个坑,有的使用envelop.getResponse() 的时候,也可能会报错,改为使用 envelop.bodyIn即可。

上面就是Android studio主要代码中的一些说明了。

下面是项目中,可能会遇到的一些错误。

5.开发过程中可能会出现的一些错误

(1)java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive,类转换错误。

参考: https://www.cnblogs.com/gushandujian/p/3495191.html ,

http://blog.csdn.net/whybiang/article/details/6533051 ,解释很详细,感谢作者。

在服务器端返回值是String类型的数值的时候使用SoapObject soapObject = (SoapObject) envelope.getResponse();和 SoapObject result = (SoapObject)envelope.bodyIn;这两种方法来接受值都会报出 java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive这样的错误。 我们可以使用 Object object = (Object)envelope.getResponse();就可以解决这种错误。

(2)org.apache.cxf.interceptor.Fault: The given SOAPAction http://wsservice.abc.com/icService does not match an operation. 或者是 org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://wsservice.abc.com}icService found. Expected {http://wsservice.abc.com/}icService.

参考: http://www.yyjjssnn.cn/articles/706.html ,解释很详细,感谢作者。

这两种错误都是因为服务端编写时,没有在服务和方法上加入或者编写注解不对导致的,请详细查看注解是否正确,以及网页中的wsdl 描述的soapaction是否正确,详细可以查看我的上一篇博客,查看webservice服务端代码如何编写。

新增demo源码下载地址:链接:https://pan.baidu.com/s/1sm1w3ud  密码:0n40,链接失效的话,请评论区留言个人百度账号,给你分享一下。

由于编者水平有限,文章中如有不妥之处或者有什么疑问,可直接在下面评论指出,不胜感激。