我在寻找关于如何使用Android调用标准SOAP/WSDL web服务的好信息时遇到了很多麻烦。我所能找到的只是一些非常复杂的文档和对“kSoap2”的引用,以及一些关于用SAX手动解析它的内容。好吧,这很好,但现在是2008年,所以我认为应该有一些调用标准web服务的好库。

web服务基本上就是在NetBeans中创建的。我希望有IDE支持来生成管道类。我只是需要最简单/最优雅的方法来从基于android的电话联系基于WSDL的web服务。


当前回答

请下载并添加SOAP库文件与您的项目 文件名称:ksoap2-android-assembly-3.4.0-jar-with-dependencies

清理应用程序,然后启动程序

下面是SOAP服务调用的代码

    String SOAP_ACTION = "YOUR_ACTION_NAME";
    String METHOD_NAME = "YOUR_METHOD_NAME";
    String NAMESPACE = "YOUR_NAME_SPACE";
    String URL = "YOUR_URL";
    SoapPrimitive resultString = null;

    try {
        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        addPropertyForSOAP(Request);

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        HttpTransportSE transport = new HttpTransportSE(URL);

        transport.call(SOAP_ACTION, soapEnvelope);
        resultString = (SoapPrimitive) soapEnvelope.getResponse();

        Log.i("SOAP Result", "Result Celsius: " + resultString);
    } catch (Exception ex) {
        Log.e("SOAP Result", "Error: " + ex.getMessage());
    }
    if(resultString != null) {
        return resultString.toString();
    }
    else{
        return "error";
    }

结果可能是JSONObject或JSONArray或String

为方便参考, https://trinitytuts.com/load-data-from-soap-web-service-in-android-application/

谢谢。

其他回答

请下载并添加SOAP库文件与您的项目 文件名称:ksoap2-android-assembly-3.4.0-jar-with-dependencies

清理应用程序,然后启动程序

下面是SOAP服务调用的代码

    String SOAP_ACTION = "YOUR_ACTION_NAME";
    String METHOD_NAME = "YOUR_METHOD_NAME";
    String NAMESPACE = "YOUR_NAME_SPACE";
    String URL = "YOUR_URL";
    SoapPrimitive resultString = null;

    try {
        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        addPropertyForSOAP(Request);

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        HttpTransportSE transport = new HttpTransportSE(URL);

        transport.call(SOAP_ACTION, soapEnvelope);
        resultString = (SoapPrimitive) soapEnvelope.getResponse();

        Log.i("SOAP Result", "Result Celsius: " + resultString);
    } catch (Exception ex) {
        Log.e("SOAP Result", "Error: " + ex.getMessage());
    }
    if(resultString != null) {
        return resultString.toString();
    }
    else{
        return "error";
    }

结果可能是JSONObject或JSONArray或String

为方便参考, https://trinitytuts.com/load-data-from-soap-web-service-in-android-application/

谢谢。

要从移动设备(尤其是Android手机)调用web服务,我使用了一种非常简单的方法。我没有使用任何web服务客户端API试图调用web服务。我打电话的方法如下。

Create a simple HTTP connection by using the Java standard API HttpURLConnection. Form a SOAP request. (You can make help of SOAPUI to make a SOAP request.) Set doOutPut flag as true. Set HTTP header values like content-length, Content type, and User-agent. Do not forget to set Content-length value as it is a mandatory. Write entire the SOAP request to the output stream. Call the method to make a connection and receive the response (In my case I used getResonseCode). If your received response code as It means you are succeeded to call web service. Now take an input stream on the same HTTP connection and receive the string object. This string object is a SOAP response. If the response code is other than 200 then take a ErrorInput stream on same HTTPobject and receive the error if any. Parse the received response using SAXParser (in my case) or DOMParaser or any other parsing mechanism.

我已经在Android手机上实现了这个过程,并且它正在成功运行。我能够解析响应,即使它超过700 KB。

如果可以,选择JSON。Android自带完整的组织。json包

通过SOAP方法遵循这些步骤

从WSDL文件中,

为每个请求创建SOAP请求模板。 然后替换要在代码中传递的值。 使用DefaultHttpClient实例将该数据POST到服务端点。 获取响应流,最后 使用XML Pull解析器解析响应流。

Few months ago I was working with jax-ws web service in j2ee application, There we were using CXF wsdl2java to generate WS client stub from the WSDL file and with those client stubs we consumed the web services. Few weeks ago, when I was trying to consume the web service in the same way in android platform I couldn't, because the android jar has not all the "jax-ws" supporting classes in it. That time I didn't find any such tool ( if I wasn't failed to google efficiently) to meet my requirement --

从WSDL获取客户端存根。 并使用一些参数调用服务(java业务请求 对象)。 获取响应业务对象。

因此,我开发了自己的Android SOAP客户端生成工具。你需要遵循以下步骤:

从WSDL获取WS客户端存根,将其放入您的项目中。 例如,对于某些服务“ComplexOperationService”,实例化 Service,获取端点端口并调用服务方法,并从Web服务获取响应:

eg:

ComplexOperationService service = new ComplexOperationService( );
ComplexOperation port= service.getComplexOperationPort();    
SomeComplexRequest request = --Get some complex request----;    
SomeComplexResp resp = port.operate( request  );

您不需要关心服务类/请求/响应类或任何其他类以及方法,因为您知道它们都是从WSDL生成的。 当然,您不需要知道soap动作/信封/名称空间等等。就像我们开发人员一直做的那样调用这个方法。