我使用ksoap2套件要呼叫.Net Webservice﹐由网络上找来的范例﹐如果呼叫的是没有参数的webservice是可以﹐但如果是有参数的webservice却是参数一直无法正确的传递。我的.net webservice如下    //没有参数的
    [WebMethod]
    public string HelloWorld() {
        string str = "Android Call me";
        return str;
    }
    //有参数的
    [WebMethod]
    public string ShowData(string strValue) {
        WriteLog("to Call and str= "+strValue);
        if (strValue == null) {
            strValue = "strValue is null";
        } else if (strValue == "") {
            strValue = "str is space";
        }
        
        return strValue;
    }
在android中的code如下  private static final String SOAP_ACTION="http://tempuri.org/HelloWorld";
  private static final String METHOD_NAME="HelloWorld";
  private static final String SOAP_ACTION2="http://tempuri.org/ShowData";
  private static final String METHOD_NAME2="ShowData";
  private static final String NAMESPACE="http://tempuri.org";
  private static final String URL="http://198.100.100.101/SampleService/Service.asmx";
  //呼叫没有参数的 web service
  private String CallNoneParamService(){
    String result="";
    try{
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
      SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
      envelope.dotNet=true;
      envelope.setOutputSoapObject(request);
      AndroidHttpTransport aht=new AndroidHttpTransport(URL);
      aht.call(SOAP_ACTION, envelope);
      SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
      result = response.toString();
    }catch(Exception er){
      result=er.toString();
    }
    return result;
  }
    
    //呼叫有参数的Web Service
    private String CallParamService(){
      String result="";
      try{
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
        request.addProperty("strValue", "TEST");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport aht=new AndroidHttpTransport(URL);
        aht.call(SOAP_ACTION2, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        result = response.toString();
      }catch(Exception er){
        result=er.toString();
      }
      return result;
    }
我由ShowData这个Web Service Method所下的记录﹐确实可以知道这个Service有被呼叫﹐而回传值是"strValue is null"﹐可见呼叫时传递的参数有问题﹐不知道是什么原因。希望能有人可以解惑。

解决方案 »

  1.   

    请问大家在呼叫.Net Web service都没有这样的问题吗?
    不论我怎么试, 没有错误讯息, webservice也有被呼叫,但参数就是传不进去, 有没有人有什么经验可以分享一下吗?
      

  2.   

    http://hi.baidu.com/lbp0408/blog/item/7971ae10d229b30c203f2e12.html
      

  3.   

    http://blog.csdn.net/kylixlu/archive/2010/03/12/5372846.aspx看下这个博客
    期望对你有帮助
      

  4.   

    由梦枫給的博客資料果然有用, 的確讓我找到了為什麼帶參數會失敗的原因.
    目前傳遞string已無問題, 對於較複雜的型態可以再進一步來測試了。
      

  5.   

    private static final String NAMESPACE="http://tempuri.org";楼主的问题应该是出在这里,NAMESPACE和webservice默认的相比少个"/".
    我也是菜鸟,对soap原理不甚了解,我是这么解决这个问题的,有高人解释下为什么吗?我想NAMESPACE虽然看起来是个Url,但毕竟是个命名空间,应该是必须相同的。那么为什么只会影响参数的传递呢?
      

  6.   

    楼主,对于单独返回一个字符串的问题你已经解决了。。有没有解决返回复杂对象的?现在遇到一个返回复杂对象的,不知道怎么取得对象中各属性的值。HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetFullPositionNameListResponse xmlns="http://abc.cc">
          <GetFullPositionNameListResult>
            <UserPosition>
              <PositionName>string</PositionName>
              <PositionID>string</PositionID>
            </UserPosition>
            <UserPosition>
              <PositionName>string</PositionName>
              <PositionID>string</PositionID>
            </UserPosition>
          </GetFullPositionNameListResult>
        </GetFullPositionNameListResponse>
      </soap:Body>
    </soap:Envelope>比如返回这样的结构。如何取得到值呢。
      

  7.   

    envelope.dotNet = false;就可以获取到参数了
      

  8.   

    我试过,传String,int ,参数都没问题, 对于返回的对象解析也不难, 但我现在的需求是要传递对象类型的参数,期待高手出现 指点一二 
      

  9.   

    楼上朋友们的问题 ,  依我看应该是参数写错了, 网上说所传的参数名可以不对应 ,我绝不认同! 
    大家可以先用浏览器访问一下你的webService , 那里会有所需参数的名称,记住,一个字母都不能错!!!
      

  10.   

     在webservice类的上面有这样一句[WebService(Namespace = "http://tempuri.org/")]
    改成[WebService(Namespace = "http://tempuri.org")] 去掉最后/就好了