.net 使用Android调用SOAP Web服务时连接被拒绝

.net 使用Android调用SOAP Web服务时连接被拒绝,.net,android,web-services,soap,ksoap2,.net,Android,Web Services,Soap,Ksoap2,我在.net中开发了一个Web服务,并试图在Android中调用它。 要调用Web服务,我正在使用ksoap2 WDSL的URL为: 这是WSDL <?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="

我在.net中开发了一个Web服务,并试图在Android中调用它。 要调用Web服务,我正在使用ksoap2

WDSL的URL为:

这是WSDL

<?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://Cidadaos_Cidade/Open/" xmlns:s0="http://www.outsystems.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://Cidadaos_Cidade/Open/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://www.outsystems.com">
      <s:element name="GetCategoriasFull">
        <s:complexType />
      </s:element>
      <s:element name="GetCategoriasFullResponse">
        <s:complexType>
          <s:sequence>

            <s:element minOccurs="0" maxOccurs="1" name="Out1" type="s0:ArrayOfWORCCategoriaSubcategoriaRecord" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ArrayOfWORCCategoriaSubcategoriaRecord">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="unbounded" name="WORCCategoriaSubcategoriaRecord" nillable="true" type="s0:WORCCategoriaSubcategoriaRecord" />
        </s:sequence>
      </s:complexType>

      <s:complexType name="WORCCategoriaSubcategoriaRecord">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="ssENCategoria" type="s0:Categoria" />
          <s:element minOccurs="0" maxOccurs="1" name="ssENSubcategoria" type="s0:Subcategoria" />
        </s:sequence>
      </s:complexType>
      <s:complexType name="Categoria">
        <s:sequence>
          <s:element minOccurs="1" maxOccurs="1" name="Id" type="s:int" />

          <s:element minOccurs="0" maxOccurs="1" name="Nome" type="s:string" />
        </s:sequence>
      </s:complexType>
      <s:complexType name="Subcategoria">
        <s:sequence>
          <s:element minOccurs="1" maxOccurs="1" name="Id" type="s:int" />
          <s:element minOccurs="0" maxOccurs="1" name="Nome" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="Imagem" type="s:string" />
          <s:element minOccurs="1" maxOccurs="1" name="CategoriaId" type="s:int" />

        </s:sequence>
      </s:complexType>
      <s:element name="GetCategorias">
        <s:complexType />
      </s:element>
      <s:element name="GetCategoriasResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="Out1" type="s0:ArrayOfCategoria" />

          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ArrayOfCategoria">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="unbounded" name="Categoria" nillable="true" type="s0:Categoria" />
        </s:sequence>
      </s:complexType>
    </s:schema>

  </wsdl:types>
  <wsdl:message name="GetCategoriasFullSoapIn">
    <wsdl:part name="parameters" element="s0:GetCategoriasFull" />
  </wsdl:message>
  <wsdl:message name="GetCategoriasFullSoapOut">
    <wsdl:part name="parameters" element="s0:GetCategoriasFullResponse" />
  </wsdl:message>
  <wsdl:message name="GetCategoriasSoapIn">
    <wsdl:part name="parameters" element="s0:GetCategorias" />

  </wsdl:message>
  <wsdl:message name="GetCategoriasSoapOut">
    <wsdl:part name="parameters" element="s0:GetCategoriasResponse" />
  </wsdl:message>
  <wsdl:portType name="OpenSoap">
    <wsdl:operation name="GetCategoriasFull">
      <wsdl:input message="tns:GetCategoriasFullSoapIn" />
      <wsdl:output message="tns:GetCategoriasFullSoapOut" />
    </wsdl:operation>

    <wsdl:operation name="GetCategorias">
      <wsdl:input message="tns:GetCategoriasSoapIn" />
      <wsdl:output message="tns:GetCategoriasSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="OpenSoap" type="tns:OpenSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetCategoriasFull">
      <soap:operation soapAction="http://Cidadaos_Cidade/Open/GetCategoriasFull" style="document" />

      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetCategorias">
      <soap:operation soapAction="http://Cidadaos_Cidade/Open/GetCategorias" 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="OpenSoap12" type="tns:OpenSoap">

    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetCategoriasFull">
      <soap12:operation soapAction="http://Cidadaos_Cidade/Open/GetCategoriasFull" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>

    </wsdl:operation>
    <wsdl:operation name="GetCategorias">
      <soap12:operation soapAction="http://Cidadaos_Cidade/Open/GetCategorias" 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="Open">
    <wsdl:port name="OpenSoap" binding="tns:OpenSoap">
      <soap:address location="http://localhost/Cidadaos_Cidade/Open.asmx" />
    </wsdl:port>
    <wsdl:port name="OpenSoap12" binding="tns:OpenSoap12">
      <soap12:address location="http://localhost/Cidadaos_Cidade/Open.asmx" />
    </wsdl:port>

  </wsdl:service>
</wsdl:definitions>
有人能帮我吗?我已经尝试过使用C#.net项目调用它,效果非常好。
很抱歉把所有的XML和代码都放在这里,但我很绝望…

我对android开发不太了解,但localhost是一个指向android设备的环回地址。您需要用运行Web服务的服务器的名称或ip地址替换localhost

我对android的开发不太了解,但localhost是android设备的环回地址。您需要用运行Web服务的服务器的名称或ip地址替换localhost

在android中,当您为web服务调用引用localhost时,它总是会给您一个错误,因为本地主机是android的回环。您必须为此使用别名地址。选中此项可添加别名地址

在android中,当您为web服务调用引用localhost时,它总是会给您一个错误,因为本地主机是android的回环。您必须为此使用别名地址。选中此项可添加别名地址

实际上,当我们在本地主机上运行服务时,服务运行正常,但当我们尝试从android访问本地主机时,会出现错误,原因是“本地主机”是android仿真器设备的回送地址。所以你要做的就是给出别名地址。使用以下IP地址呼叫您的web服务

android中的web服务url如下所示
http://10.0.2.2:PortNumber/WebService.php

实际上,当我们在本地主机上运行服务时,服务运行正常,但当我们尝试从android访问本地主机时,会出现错误,原因是“本地主机”是android仿真器设备的回送地址。所以你要做的就是给出别名地址。使用以下IP地址呼叫您的web服务

android中的web服务url如下所示
http://10.0.2.2:PortNumber/WebService.php

我花了一个小时才意识到,如果使用emulator,就必须使用10.0.2.2!不是本地主机…我花了一个小时才意识到,如果使用emulator,就必须使用10.0.2.2!不是本地主机。。。
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);


        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //soapEnvelope.dotNet = true; //tried with and without this flag
        soapEnvelope.setOutputSoapObject(request);

        AndroidHttpTransport aht = new AndroidHttpTransport(URL);
        try{
            //crashes in the following line with the exception:
            //java.net.ConnectException: localhost/127.0.0.1:80 - Connection refused
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;

        }catch(Exception ex){
            ex.printStackTrace();
        }