解析android中的KSoap2响应

时间:2010-10-16 21:24:40

标签: android web-services parsing response ksoap2

  

可能重复:
  Parsing ksoap2 response

所以我设法在android中使用KSoap2调用web服务,但我找不到解析响应的方法......

So here's what I receive from the webservice
anyType{
WORCCategoriaSubcategoriaRecord=anyType{ssENCategoria=anyType{Id=1; Nome=Problema na rua; }; ssENSubcategoria=anyType{Id=1; Nome=Falta de acesso; Imagem=anyType{}; CategoriaId=1; }; }; 

WORCCategoriaSubcategoriaRecord=anyType{ssENCategoria=anyType{Id=1; Nome=Problema na rua; }; ssENSubcategoria=anyType{Id=2; Nome=Falta de Passadeira; Imagem=anyType{}; CategoriaId=1; }; }; 
}

这是我用来调用webservice的代码......

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        //request.addProperty("Celsius", "32");

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        soapEnvelope.dotNet = true;
        AndroidHttpTransport aht = new AndroidHttpTransport(URL);
        try{
            aht.call(SOAP_ACTION, soapEnvelope);
            //SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
            SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
            int elementCount = resultsRequestSOAP.getPropertyCount();

            if(elementCount>0){
                SoapObject element;
                for(int i = 0;i<elementCount;i++){
                    element = (SoapObject)resultsRequestSOAP.getProperty(i);
                }
            }

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

有没有办法“轻松”解析它而不必“手动”通过每个属性?像XML解析器......

2 个答案:

答案 0 :(得分:3)

看看这里,我相信这段代码已经过优化,可以处理复杂类型并使用KSOAP返回复杂类型的数组:

Web Service That Returns An Array of Objects With KSOAP

答案 1 :(得分:1)

您可以设置自动编组。查看ksoap2-android项目的链接部分,获取一些教程的链接。