解析Web服务结果

时间:2012-05-27 20:12:06

标签: java web-services soap wsdl android-ksoap2

我正在使用下面的代码片段从wsdl url获取用户详细信息,它只返回第一个用户的信息而不是整个列表,我如何更正它以便打印整个用户详细信息?我需要它在Android上运行所以我使用的是ksoap-2-android库。

请记住,我完全是网络服务和wsdl的新手。我试过理解阅读但没有取得很多成就。

package parsewsdl;

import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class ParseWSDL {

    private static final String NAMESPACE = "http://domain.intern.bits.com";
    private static final String METHOD_NAME = "getUserList";
    private static final String URL =
        "http://www.bulusalim.com:12000/BitsMobileInternHomeWork/services/UserWebService?wsdl";
    private static final String SOAP_ACTION = "getUserListResponse";

public static void main(String[] argv) {
    SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    env.setOutputSoapObject(soapObject);

    HttpTransportSE con = new HttpTransportSE(URL);
    try {
        con.call(SOAP_ACTION, env);
        SoapObject result = (SoapObject) env.bodyIn;
        SoapObject dets = (SoapObject) result.getProperty("getUserListReturn");
        System.out.println(dets.getProperty("email").toString());

    } catch (IOException | XmlPullParserException e) {};
}

更新: System.out.println(result.toString()); 给出this

1 个答案:

答案 0 :(得分:-1)

我不知道这是否是最充分或最恰当的方式,但我找到了这样的解决方案:

...
SoapObject result = (SoapObject) env.bodyIn;
final int size = result.getPropertyCount();
for (int i = 0; i < size; i++) {
    SoapObject tempObj = (SoapObject) result.getProperty(i);
    // accessing operations with the name name and surname
    tempObj.getPropertyAsString("name");
    tempObj.getPropertyAsString("surname");
...