如何使用SOAP Service android

时间:2012-04-30 12:07:26

标签: android ksoap2

我是Android新手。任何人都可以借助任何示例或源代码帮助我开始使用Soap服务吗? 提前致谢

2 个答案:

答案 0 :(得分:2)

如果您的网络服务是基于.Net的话,请使用此方法:

private static SoapObject callWebServiceMethod(String url,
        String namespace, String methodName,
        HashMap<String, Object> parameters, String soapAction)
        throws Exception {

    Log.i("WebService", "URL: " + url);
    Log.i("WebService", "MethodName: " + methodName);

    URL myurl = new URL(url);
    URLConnection connection = myurl.openConnection();
    connection.setConnectTimeout(20 * 1000);
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    int responseCode = httpConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK)
    {
        httpConnection.disconnect();
        SoapObject request = new SoapObject(namespace, methodName);

        if (parameters != null) {
            String[] keys = new String[0];
            keys = (String[]) parameters.keySet().toArray(keys);
            Object[] vals = (Object[]) parameters.values().toArray();

            for (int i = 0; i < parameters.size(); i++) {
                request.addProperty(keys[i], vals[i]);
                Log.i("WebService", keys[i] + ": " + vals[i]);
            }
        }

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

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(url,
                TimeOutInSeconds * 1000);


        androidHttpTransport.call(soapAction, envelope);


        SoapObject so = (SoapObject) envelope.getResponse();

        return so;

    }
    else
    {
        httpConnection.disconnect();

    }

}

用于下载Ksoap库use this link

答案 1 :(得分:1)

要在android中的SOAP Service中使用,你需要看下面的Parse Soap参考:

链接到Soap service client

链接到Step by Step Use

从这个ksoap2-android

获取在你的android项目中使用的ksoap jar

下面是使用kso​​ap库调用soap的Web服务类。