2009-01-16 35 views
1

因此,我在NetBeans 6.5生成的Java ME客戶端的Web服務代碼到c#(vs2005)Web服務上發生了一些編譯錯誤。我已經對我的示例進行了大量修改,並且仍然顯示了問題,並且無法返回一組東西幾乎是一種破壞行爲。從.net Web服務將數組返回到Java ME Web服務會導致存根編譯錯誤?

C#web服務(SimpleWebService.asmx)

<%@ WebService Language="C#" Class="SimpleWebService" %> 

using System; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 

[WebService(Namespace = "http://sphereinabox.com/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class SimpleWebService : System.Web.Services.WebService { 

    [WebMethod] 
    public CustomType[] GetSomething() { 
     return new CustomType[] {new CustomType("hi"), new CustomType("bye")}; 
    } 
    public class CustomType { 
     public string Name; 
     public CustomType(string _name) { 
      Name = _name; 
     } 
     public CustomType() { 
     } 
    } 
} 

WSDL(由VS2005自動生成):

<?xml version="1.0" encoding="utf-8"?> 
<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://sphereinabox.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://sphereinabox.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 
    <wsdl:types> 
    <s:schema elementFormDefault="qualified" targetNamespace="http://sphereinabox.com/"> 
     <s:element name="GetSomething"> 
     <s:complexType /> 
     </s:element> 
     <s:element name="GetSomethingResponse"> 
     <s:complexType> 
      <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="GetSomethingResult" type="tns:ArrayOfCustomType" /> 
      </s:sequence> 
     </s:complexType> 
     </s:element> 
     <s:complexType name="ArrayOfCustomType"> 
     <s:sequence> 
      <s:element minOccurs="0" maxOccurs="unbounded" name="CustomType" nillable="true" type="tns:CustomType" /> 
     </s:sequence> 
     </s:complexType> 
     <s:complexType name="CustomType"> 
     <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" /> 
     </s:sequence> 
     </s:complexType> 
    </s:schema> 
    </wsdl:types> 
    <wsdl:message name="GetSomethingSoapIn"> 
    <wsdl:part name="parameters" element="tns:GetSomething" /> 
    </wsdl:message> 
    <wsdl:message name="GetSomethingSoapOut"> 
    <wsdl:part name="parameters" element="tns:GetSomethingResponse" /> 
    </wsdl:message> 
    <wsdl:portType name="SimpleWebServiceSoap"> 
    <wsdl:operation name="GetSomething"> 
     <wsdl:input message="tns:GetSomethingSoapIn" /> 
     <wsdl:output message="tns:GetSomethingSoapOut" /> 
    </wsdl:operation> 
    </wsdl:portType> 
    <wsdl:binding name="SimpleWebServiceSoap" type="tns:SimpleWebServiceSoap"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="GetSomething"> 
     <soap:operation soapAction="http://sphereinabox.com/GetSomething" style="document" /> 
     <wsdl:input> 
     <soap:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:binding name="SimpleWebServiceSoap12" type="tns:SimpleWebServiceSoap"> 
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="GetSomething"> 
     <soap12:operation soapAction="http://sphereinabox.com/GetSomething" style="document" /> 
     <wsdl:input> 
     <soap12:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap12:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:service name="SimpleWebService"> 
    <wsdl:port name="SimpleWebServiceSoap" binding="tns:SimpleWebServiceSoap"> 
     <soap:address location="http://localhost/SimpleWebService/SimpleWebService.asmx" /> 
    </wsdl:port> 
    <wsdl:port name="SimpleWebServiceSoap12" binding="tns:SimpleWebServiceSoap12"> 
     <soap12:address location="http://localhost/SimpleWebService/SimpleWebService.asmx" /> 
    </wsdl:port> 
    </wsdl:service> 
</wsdl:definitions> 

生成(netbeans的)代碼編譯失敗,這創建經歷的「添加 - >新的JavaME到Web服務客戶端」嚮導。 (SimpleWebService_Stub.java)

public ArrayOfCustomType GetSomething() throws java.rmi.RemoteException { 
     Object inputObject[] = new Object[] { 
     }; 

     Operation op = Operation.newInstance(_qname_operation_GetSomething, _type_GetSomething, _type_GetSomethingResponse); 
     _prepOperation(op); 
     op.setProperty(Operation.SOAPACTION_URI_PROPERTY, "http://sphereinabox.com/GetSomething"); 
     Object resultObj; 
     try { 
      resultObj = op.invoke(inputObject); 
     } catch(JAXRPCException e) { 
      Throwable cause = e.getLinkedCause(); 
      if(cause instanceof java.rmi.RemoteException) { 
       throw (java.rmi.RemoteException) cause; 
      } 
      throw e; 
     } 

//////// Error on next line, symbol ArrayOfCustomType_fromObject not defined 
     return ArrayOfCustomType_fromObject((Object[])((Object[]) resultObj)[0]); 
    } 

原來用(在我的生產問題「CustomType」有一個以上的場)這個人爲的例子我也從產生相同的這個有趣的代碼得到錯誤(SimpleWebService_Stub.java )生成的代碼。錯誤是沒有定義字符串(它是Java中的String,除此之外我認爲這應該是談論CustomType)。

private static string string_fromObject(Object obj[]) { 
    if(obj == null) return null; 
    string result = new string(); 
    return result; 
} 

回答

2

顯然你不應該使用內置的東西一樣的NetBeans的存根生成看中。票反對,你應該使用Sun(或Sprint的品牌...)存根生成器,你可以通過:

  1. 右鍵點擊你的項目
  2. 選擇屬性
  3. 選擇平臺在新的對話框
  4. 點擊左側上管理模擬器在rigt側
  5. 選擇所需的仿真器(Sun Java(TM)Wirless Tookit 2.5(用於CLDC)位於此新對話框的左側
  6. 單擊工具&擴展選項卡。
  7. 單擊打開公用程序按鈕。
  8. 在這個新的對話框
  9. 哭了涉及大量的步驟和對話框選擇樁模塊生成實用程序。
  10. 點擊啓動
  11. 指定您的WSDL URL http://localhost/SimpleWebService/SimpleWebService.asmx?WSDL
  12. 指定您的紙路徑** C:\代碼\ SimpleMobile的\ src \ **
  13. 指定您的輸出包simplewebservice或任何你
  14. 確定按鈕。
  15. 如果要使用netbeans進行編譯,請忽略無法編譯的錯誤。 錯誤:com.sun.tools.javac.Main在類路徑中不可用。 錯誤:comilation失敗,應該報告錯誤
  16. 在Web服務發生更改時重複上述所有操作,並且存根需要重新生成。
  17. 寫一個批處理文件來做到這一點,以保持你的理智。

呃。好吧,無論如何,上面我發現喬納森Knudsen的書「踢與MIDP和MSA的對接:創造偉大的移動應用程序」

1

以前的答案是偉大的,我有classcastexception,當我使用netbeans生成存根類,Java我的平臺SDK 3生成的課程就像一個魅力。

Java ME平臺SDK 3和NetBeans 6.8沒有顯示開公用事業按鈕(不能點擊它),但是你可以打開Java ME平臺SDK 3生成stub類

創建一個新項目, 右鍵點擊項目名稱: add - 其他 選擇其他移動web服務客戶端,指定WSDL URL 和woila,你有工作生成的類。

0

在web服務中添加屬性[Serializable]用於類CustomType。 您將在存根類中獲得ArrayOfCustomType_fromObject()。