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

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


当前回答

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动作/信封/名称空间等等。就像我们开发人员一直做的那样调用这个方法。

其他回答

我相信您可以用Axis创建一个小型SOAP客户机。轴安装说明。

Android没有提供任何类型的SOAP库。您可以自己编写,也可以使用kSOAP 2之类的东西。正如您所注意到的,其他人已经能够在他们自己的项目中编译和使用kSOAP2,但我还没有这样做。

到目前为止,谷歌对向Android添加SOAP库几乎没有兴趣。我怀疑他们更愿意支持当前Web服务的趋势,即基于rest的服务,并使用JSON作为数据封装格式。或者,使用XMPP进行消息传递。但这只是猜测。

目前,基于xml的web服务在Android上还不是一项简单的任务。由于不了解NetBeans,我不能谈论那里可用的工具,但我同意应该有一个更好的库。XmlPullParser有可能使您不用使用SAX,但我对此了解不多。

要从android调用SOAP web服务,请尝试使用此客户端

不要忘记在java构建路径中添加ksoap2-android.jar

public class WsClient {
    private static final String SOAP_ACTION = "somme";
    private static final String OPERATION_NAME = "somme";
    private static final String WSDL_TARGET_NAMESPACE = "http://example.ws";
    private static final String SOAP_ADDRESS = "http://192.168.1.2:8080/axis2/services/Calculatrice?wsdl";

    public String caclculerSomme() {

        String res = null;
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);
        request.addProperty("a", "5");
        request.addProperty("b", "2");

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

        try {
            httpTransport.call(SOAP_ACTION, envelope);
            String result = envelope.getResponse().toString();
            res = result;
            System.out.println("############# resull is :" + result);
        } catch (Exception exception) {
            System.out.println("########### ERRER" + exception.getMessage());
        }

        return res;
    }
}

要从移动设备(尤其是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。

这是一个在android中使用SOAP web服务的工作示例。

**注意:***不要忘记在你的项目中添加ksoap2.jar,并在AndroidManifest文件中添加INTERNET权限*

public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String METHOD_NAME = "FahrenheitToCelsius";
public final String PROPERTY_NAME = "Fahrenheit";
public final String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
public final String SOAP_ADDRESS = "http://www.w3schools.com/webservices/tempconvert.asmx";


private class TestAsynk extends AsyncTask<String, Void, String> {

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
        Toast.makeText(getApplicationContext(),
                String.format("%.2f", Float.parseFloat(result)),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    protected String doInBackground(String... params) {
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                METHOD_NAME);
        request.addProperty(PROPERTY_NAME, params[0]);

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

        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(
                SOAP_ADDRESS);
        Object response = null;
        try {

            androidHttpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            Log.e("Object response", response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.toString();
    }
}