如何从Soap Object获取响应到android中的字符串?

时间:2013-10-15 12:16:14

标签: android web-services soap

我使用以下代码从.Net Web服务(asmx)获取响应。

try{
    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet =true;
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
        envelope.setOutputSoapObject(request);
    httpTransport.debug=true;
        httpTransport.call(SOAP_ACTION, envelope);
    SoapObject result=(SoapObject)envelope.getResponse();
    String Text = result.getProperty(1).toString();
        System.out.println("Text : "  + Text);
    } 
catch (Exception e) {
    e.printStackTrace();
}

当我尝试这段代码时,我得到了以下输出。

  

anyType的{NewDataSet = anyType的{ResponseTable = {anyType的结果=成功; }; };   }

在这里,我需要“结果=成功”。

请帮帮我。

2 个答案:

答案 0 :(得分:2)

尝试:

SoapObject result = (SoapObject)envelope.bodyIn;
String Result = result.getProperty(0).toString();

答案 1 :(得分:0)

结果就像一个多维数组。您需要解析结果,直到获得最后一个元素:Result=Assigned;

第一次:( array有2个元素)

SoapObject result = (SoapObject)envelope.bodyIn;
SoapObject first = (SoapObject) result.getProperty(0);

你会得到你的评论:

  

anyType的{架构= {anyType的元素= anyType的{的complexType = {anyType的选择= anyType的{的Elemen T = {anyType的的complexType = {anyType的序列= {anyType的元素= anyType的{};   }; }; }; }; }; }; };   的DiffGram = anyType的{NewDataSet = anyType的{ResponseTable = {anyType的结果=分配;   }; }; }; }

第二次:(array[1]

SoapObject second = (SoapObject) first.getProperty(1);
你得到了:

  

anyType的{NewDataSet = anyType的{ResponseTable = {anyType的结果=分配;}; }; }

第三次:(array[1][0]

SoapObject third = (SoapObject) second.getProperty(0);
你得到了:

  

anyType的{ResponseTable = {anyType的结果=分配;};}

最后,Forth time:(array[1][0][0]

SoapObject forth = (SoapObject) third.getProperty(0);

你会得到你想要的东西:

  

anyType的{结果=指派;}

现在使用以下内容获取您的元素:( array[1][0][0][i]这次i=0

PropertyInfo pi = new PropertyInfo();
forth.getPropertyInfo(0, pi);
Log.d("MRNEO", pi.getName() + "=" + forth.getProperty(0).toString());

并且日志中的结果是:

  

结果=指派