Android 访问Webservice接口,参数对象不能串行化问题解决(java.lang.RuntimeException: Cannot serialize)

服务器端Webservice接口参数使用的是自定义结构体,非通用的一些字符串类。

Android 客户端使用ksoap2  jar包,使用方法如下:

    	//调用的方法
        String methodName = "sceneUserValidate";
        //创建httpTransportSE传输对象
        HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);//service url
        ht.debug = true;
        //使用soap1.1协议创建Envelop对象
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //实例化SoapObject对象
        SoapObject request = new SoapObject(SERVICE_NS, methodName);
        /**
         * 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
         * */
        
        SceneUserValidateEvt sevt = new SceneUserValidateEvt();
        sevt.setCallNumber("13913008213");
        sevt.setInaccessInfo(new InaccessInfo("0020001","0020001","12","12","v1.0"));
        request.addProperty("SceneUserValidateEvt", sevt);
        
        //将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
        envelope.bodyOut = request;        
        envelope.setOutputSoapObject(request);
        Log.d("haha","start = ");
        try{
            //调用webService
            ht.call(SERVICE_NS+methodName, envelope);
            Log.d("haha","dump = "+ht.requestDump);
            //txt1.setText("看看"+envelope.getResponse());
            if(envelope.getResponse() != null){
                SoapObject result = (SoapObject) envelope.bodyIn;
                String name = result.getProperty(0).toString();
                Log.d("haha","返回值 = "+name);
            }else{

                Log.d("haha","返回值 =! ");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

运行出现如下错误:

04-12 21:31:12.046: W/System.err(4202): java.lang.RuntimeException: Cannot serialize: com.jh.webservice_visit.SceneUserValidateEvt@41b1ec80
04-12 21:31:12.046: W/System.err(4202): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:664)
04-12 21:31:12.046: W/System.err(4202): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:649)
04-12 21:31:12.046: W/System.err(4202): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:604)
04-12 21:31:12.046: W/System.err(4202): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:582)
04-12 21:31:12.046: W/System.err(4202): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:658)
04-12 21:31:12.046: W/System.err(4202): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:564)
04-12 21:31:12.056: W/System.err(4202): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:205)
04-12 21:31:12.056: W/System.err(4202): at org.ksoap2.transport.Transport.createRequestData(Transport.java:111)
04-12 21:31:12.056: W/System.err(4202): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:121)
04-12 21:31:12.056: W/System.err(4202): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
04-12 21:31:12.056: W/System.err(4202): at com.jh.webservice_visit.MainActivity.startWebservice(MainActivity.java:72)
04-12 21:31:12.056: W/System.err(4202): at com.jh.webservice_visit.MainActivity.access$0(MainActivity.java:40)
04-12 21:31:12.056: W/System.err(4202): at com.jh.webservice_visit.MainActivity$2.run(MainActivity.java:113)

04-12 21:31:12.056: W/System.err(4202): at java.lang.Thread.run(Thread.java:856)


第一反应将SceneUserValidateEvt结构体implements Serializable,可是运行后还是如上提示错误。


goolge串行化失败的大多数场景与我的不符合,网上比较多的是一些Integer.class 串行失败的场景,不过后来想了想翻了一下ksoap的包

有一个这个接口类org.ksoap2.serialization.KvmSerializable;


尝试了一下将SceneUserValidateEvt结构体implements KvmSerializable,并实现了以下4个接口,运行成功OK。

public abstract interface org.ksoap2.serialization.KvmSerializable {
  
  // Method descriptor #4 (I)Ljava/lang/Object;
  public abstract java.lang.Object getProperty(int arg0);
  
  // Method descriptor #6 ()I
  public abstract int getPropertyCount();
  
  // Method descriptor #8 (ILjava/lang/Object;)V
  public abstract void setProperty(int arg0, java.lang.Object arg1);
  
  // Method descriptor #10 (ILjava/util/Hashtable;Lorg/ksoap2/serialization/PropertyInfo;)V
  public abstract void getPropertyInfo(int arg0, java.util.Hashtable arg1, org.ksoap2.serialization.PropertyInfo arg2);
}

Android客户端能正常访问到Webservice接口了。


你可能感兴趣的:(android,webservice)