如何使用kso​​ap2使用Web服务

时间:2017-09-26 03:00:52

标签: android ksoap

我正在尝试使用Web服务2天,但我失败了。这是我的代码:

String SOAP_ACTION = "urn:WsTestIntf-IWsTest#Test101";
                String METHOD_NAME = "Test101";
                String NAMESPACE = "urn:WsTestIntf-IWsTest";
                String URL = "http://wsig.127.cc:8465/WsTemp.dll/wsdl/IWsTest";

                String strTemp;

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                try
                {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

                    strTemp = resultsRequestSOAP.toString();


                } catch (Exception e) {
                    strTemp = "Error : " + e.getMessage();
                }

                Toast.makeText(getBaseContext(), strTemp, Toast.LENGTH_SHORT).show();

它出了什么问题?我已将INTERNET权限放入我的清单中。 Web服务是用Delphi编写的。

1 个答案:

答案 0 :(得分:0)

我用ksoap2度过了很长时间,但是有一个正常工作的客户端代码..希望它有所帮助......

注意: 它会重试3次,如果你愿意,你可以删除它。

import org.apache.http.client.ClientProtocolException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;

public class HttpClient {

    private static final String SOAP_ACTION = "http://tempuri.org/doPost";
    private static final String METHOD_NAME = "doPost";
    private static final String NAMESPACE = "http://tempuri.org/";
     private static final String URL = "url"

    private static int timeout = 10000;
    private static final int MAXTRY = 3;

    public String getXmlFromUrl(String xmlData) {
        String xml = null;

        int trycount = 0;
        do {
            try {
                // defaultHttpClient
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                request.addProperty("data", xmlData);

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

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,
                        timeout);

                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }

                SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
                if (result != null)
                    xml = result.toString();

            } catch (SocketTimeoutException e) {
                Log.e("Network", "timeout");
                trycount++;
                xml = "Request Timeout";
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                trycount = MAXTRY;
            } catch (ClientProtocolException e) {
                e.printStackTrace();
                trycount = MAXTRY;
            } catch (IOException e) {
                e.printStackTrace();
                xml = "Connection Error";
                trycount = MAXTRY;
            } finally {
                if (xml == null)
                    xml = ""; // to avoid exception in while condition
            }
        } while (trycount < MAXTRY && xml.equalsIgnoreCase("Request Timeout"));

        return xml == "" ? "Something Went Wrong Please Try Again!" : xml;
    }
}