Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 訪問Webservice接口,參數對象不能串行化問題解決(java.lang.RuntimeException: Cannot serialize)

Android 訪問Webservice接口,參數對象不能串行化問題解決(java.lang.RuntimeException: Cannot serialize)

編輯:關於Android編程

服務器端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接口了。


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved