使用kso​​ap2-android-2.6.2.jar的Android .Net webservice,一个String参数变为nul

时间:2015-01-18 17:08:04

标签: java android .net web-services android-ksoap2

我在测试Android App时遇到问题,试图传递一个字符串参数,我希望返回参数的值,这是我的WebService代码:

[WebService(Namespace = "http://xxx.xxx.x.xxx:8080/root")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public String SayHello(String name)
        {
                return name;

        }
    }
}

此WebService托管在我的本地PC IIS上 这是我的Android肥皂呼叫:

public class CallSoap {

    // Lever one
    public final String SOAP_SAY_HELLO = "http://xxx.xxx.x.xxx:8080/root/SayHello";

    // Lever two operation
    public final String SAY_HELLO_OPERATION = "SayHello";
    // Lever four Soap target namespace
    public final String WSDL_TARGET_NAMESPACE = "http://xxx.xxx.x.xxx:8080/root";

    // Lever four Soap address
    public final String SOAP_ADDRESS = "http://xxx.xxx.x.xxx:8080/Service1.asmx";

    // Call say hello Method
    public String getHello(String nameVal) {
        String resp = new String();
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                SAY_HELLO_OPERATION);


        request.addProperty("name", "Test");

        SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelop.dotNet = false;
        envelop.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        Log.d("HTTP  REQUEST", "HTTP REQUEST:\n" + httpTransport.requestDump);
        Log.d("HTTP  RESPONSE", "HTTP RESPONSE:\n" + httpTransport.responseDump);
        Object response1 = new Object();

        try {
            httpTransport
                    .setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            httpTransport.call(SOAP_SAY_HELLO, envelop);
            if (envelop.bodyIn instanceof SoapObject) { // SoapObject = SUCCESS
                SoapObject soapObject = (SoapObject) envelop.bodyIn;
                response1 = envelop.getResponse();
            } else if (envelop.bodyIn instanceof SoapFault) { // SoapFault =
                                                                // FAILURE
                SoapFault soapFault = (SoapFault) envelop.bodyIn;
                response1 = soapFault.getClass();
                throw new Exception(soapFault.getMessage());
            }

            return resp;
        } catch (Exception e) {
            e.printStackTrace();

        }
        return resp;

    }

}

我得到空值作为返回值,如果我检查肥皂信封,我可以在数据包中看到我的参数。我没有想法发送的参数发生了什么。

请任何能帮我辨别错误的人,提前致谢

1 个答案:

答案 0 :(得分:0)

我找错了;当我将webservice托管到本地IIS时,我不得不添加包含托管webservice文件的文件夹的安全性,在我的代码中,我必须更改envelope.dotNet=true而不是envelope.dotNet=false