2012-08-10 113 views
3

我想從.net Web服務使用方法。KSOAP2 java.lang.RuntimeException:無法序列化

落後於Web服務的代碼在命名空間

[WebService(Namespace = "http://www.mynamespace.com/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 

這裏的盡頭有個「/」是.NET方法調用

POST /TelematicsWebService/Service.asmx HTTP/1.1 
Host: localhost 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://www.mynamespace.com/GetDevicesByBranch" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <GetDevicesByBranch xmlns="http://www.mynamespace.com/"> 
     <branchNumber>int</branchNumber> 
    </GetDevicesByBranch> 
    </soap:Body> 
</soap:Envelope> 

現在代碼調用方法如下像這樣

@Override 
    protected SoapObject doInBackground(Integer... branchNumber) { 

     final String NAMESPACE = "http://www.mynamespace.com/"; 
     final String METHOD_NAME = "GetDevicesByBranch"; 
     final String SOAP_ACTION = NAMESPACE + METHOD_NAME; 
     final String URL = "http://10.0.2.2/TelematicsWebService/Service.asmx"; 

     SoapObject response = null; 

     try { 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

      PropertyInfo inputArgs = new PropertyInfo(); 
      inputArgs.setName("branchNumber"); 
      inputArgs.setValue(branchNumber); 
      inputArgs.setType(Integer.class); 
      request.addProperty(inputArgs); 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(request); 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 
        25000); 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      response = (SoapObject) envelope.getResponse(); 
     } catch (Exception exception) { 
      response = null; 
     } 

     return response; 
    } 

接近尾聲的行androidHttpTransport.call(SOAP_ACTION, envelope);引發異常:java.lang.RuntimeException: Cannot serialize: [Ljava.lang.Integer;@412e0cf0

我找不到解決此問題的建議嗎?

回答

7

我想通了......

這是內部的的AsyncTask <>(),如果它是不是已經很明顯用Override doInBackground()方法。輸入參數是Integer...類型。

protected SoapObject doInBackground(Integer... branchNumber) 

我不知道省略號做究竟是什麼,但我知道它使參數數組。

我正在傳遞一個整數數組到請求對象而不是一個特定的數組元素。從

request.addProperty("branchNumber", branchNumber); 

更改爲

request.addProperty("branchNumber", branchNumber[0]); 

固定的問題。

謝謝@ Paul-Jan和@beginner!我感謝你的回答!

+0

完全錯過了,謝謝分享!我刪除了我的答案,因爲除了沒有幫助,事實上是錯誤的。 – 2012-08-11 08:03:16

0

而不是

PropertyInfo inputArgs = new PropertyInfo(); 
inputArgs.setName("branchNumber"); 
inputArgs.setValue(branchNumber); 
inputArgs.setType(Integer.class); 
request.addProperty(inputArgs); 

試試這個:

request.addProperty("branchNumber", branchNumber); 
+0

謝謝你的迴應。我試過這個。不幸的是,它會有同樣的結果。 – Theo 2012-08-10 21:00:24

0

對於那些誰想要添加對象的列表作爲Web服務的參數:

List<Object> vect = new Vector<Object>(); 
vect.add("Element1"); 
vect.add("Element2"); 
vect.add("Element3"); 

PropertyInfo tabProp =new PropertyInfo(); 
tabProp.setName("LIST_PARAM");   
tabProp.setValue(vect); 

request.addProperty(tabProp); 

當然有沒有需要注意的是異步任務必須實現的方法:

protected String doInBackground(List<Object>... params){ 
.... 
.... 
} 
相關問題