1

因此,我在 netbeans 6.5 生成的 Java ME 客户端到 ac# (vs2005) Web 服务的 Web 服务代码上遇到了一些编译错误。我已经显着修改了我的示例,它仍然显示了问题,并且无法返回一组东西几乎是一个交易破坏者。

时间:2019-05-10 标签:c#webservice(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中的字符串,此外我认为这应该是关于CustomType的)。

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

3 回答 3

2

显然,您不应该使用花哨的内置东西,例如 netbeans 的存根生成器。不,您应该使用 Sun(或 Sprint 品牌...)存根生成器,您可以通过以下方式获得:

  1. 右键单击您的项目
  2. 选择属性
  3. 在新对话框的左侧选择平台
  4. 单击rigt 侧的Manage Emulators
  5. 在这个新对话框的左侧选择您选择的模拟器(Sun Java(TM) Wirless Tookit 2.5 for CLDC )
  6. 单击工具和扩展选项卡。
  7. 单击打开实用程序按钮。
  8. 在这个新对话框中选择存根生成器实用程序
  9. 为所涉及的大量步骤和对话而哭泣。
  10. 单击启动
  11. 指定您的 WSDL 网址http://localhost/SimpleWebService/SimpleWebService.asmx?WSDL
  12. 指定您的输出路径 ** C:\code\SimpleMobile\src\ **
  13. 指定你的输出包simplewebservice或任何你
  14. 确定按钮。
  15. 如果要使用 netbeans 编译,请忽略有关无法编译的错误。error : com.sun.tools.javac.Main is not available in the classpath.. error: 编译失败,应该报错
  16. 每当 Web 服务发生更改并且需要重新生成存根时,请重复上述所有操作。
  17. 编写一个批处理文件来执行此操作以保存您的理智。

啊。无论如何,我在 Jonathan Knudsen 的书“踢屁股与 MIDP 和 MSA:创建出色的移动应用程序”中找到了上述内容

于 2009-01-16T19:12:40.313 回答
1

前面的答案很好,当我使用 netbeans 生成的存根类时出现了 classcastexception,java me 平台 SDK 3 生成的类就像一个魅力。

Java ME 平台 SDK 3 和 netbeans 6.8 没有显示打开实用程序按钮(不能点击它),但您可以打开 Java ME 平台 SDK 3 生成存根类

创建一个新项目,右键单击项目名称:添加 - 其他选择其他,移动 Web 服务客户端,指定 WSDL URL 和 woila,您就有了工作生成的类。

于 2010-03-21T21:07:21.630 回答
0

在 Web 服务中为类 CustomType 添加属性 [Serializable]。您将在存根类中获得 ArrayOfCustomType_fromObject()。

于 2010-03-31T11:51:30.307 回答