如何在Android中解析复杂的soap对象

时间:2013-04-24 10:42:50

标签: android web-services soap ksoap2

Hello Friends我第一次在我的代码中使用soap web服务。 但我没有得到如何处理嵌套的soap对象。 这是我的代码

private final String URL = "http://192.168.0.20/BookingEngineService/HotelBookingService.asmx";
    private final String SOAP_ACTION = "http://tempuri.org/AndroidTestRequest";
    private final String METHOD_NAME = "AndroidTestRequest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("User", "abcd@xyz.com");
        request.addProperty("Password", "abcd@123");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.headerOut = new Element[1];
        envelope.headerOut[0] = buildAuthHeader();
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            Log.i("myAppEnvelope", envelope.toString());

            SoapObject response = (SoapObject) envelope.getResponse();

        // here i am trying to get values from response which is soap object 

            for (int i = 0; i < response.getPropertyCount(); i++) {

                SoapObject con = (SoapObject) response.getProperty(i);

                Log.e("Continent", con.toString());
            }


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

我将soap对象作为响应并且非常复杂,所以我没有得到如何解析它。 this is my response这只是我回复的一小部分。

我没有得到如何从SoapObject获取值 - &gt;属性 - &gt; elementData - &gt;值 - &GT; property - &gt; elementData等等......太复杂请帮助我。

2 个答案:

答案 0 :(得分:1)

您可以将soap对象转换为字符串。检查下面的示例代码 例如:

     final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
     final String str = response.toString();

答案 1 :(得分:0)

这是一个简单的XML解析。浏览link了解更多详情。

您可以将响应作为输入流获取并将其转换为字符串,并使用该字符串,您可以创建XML对象。有关更多信息,请参阅链接。