Junior Topanotti Junior Topanotti - 3 months ago 26
Android Question

Get property childs in SOAP object in android studio

I was getting this error when i try to get Propertys from an envelope:

java.lang.RuntimeException: illegal property: Codigo


But i have this property and it is correctly, see below request from SoapUI:

<CalcPrecoPrazoResponse xmlns="http://tempuri.org/">
<CalcPrecoPrazoResult>
<Servicos>
<cServico>
<Codigo>40010</Codigo>
<Valor>17,20</Valor>
<PrazoEntrega>1</PrazoEntrega>
<ValorMaoPropria>0,00</ValorMaoPropria>
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
<ValorValorDeclarado>0,00</ValorValorDeclarado>
<EntregaDomiciliar>S</EntregaDomiciliar>
<EntregaSabado>S</EntregaSabado>
<Erro>0</Erro>
<MsgErro/>
<ValorSemAdicionais>17,20</ValorSemAdicionais>
<obsFim/>
</cServico>
</Servicos>
</CalcPrecoPrazoResult>
</CalcPrecoPrazoResponse>


This is my try to get these values

try {
http.call(SOAP_ACTION, envelope);
SoapObject resposta = (SoapObject) envelope.getResponse();
frt = new Frete();

frt.setCodigo(Integer.parseInt(resposta.getProperty("Codigo").toString()));
frt.setValor(resposta.getProperty("Valor").toString());
frt.setPrazoEntrega(resposta.getProperty("PrazoEntrega").toString());
frt.setValorMaoPropria(resposta.getProperty("ValorMaoPropria").toString());
frt.setValorAvisoRecebimento(resposta.getProperty("ValorAvisoRecebimento").toString());
frt.setEntregaDomiciliar(resposta.getProperty("EntregaDomiciliar").toString());
frt.setEntregaSabado(resposta.getProperty("EntregaSabado").toString());
frt.setErro(resposta.getProperty("Erro").toString());
frt.setMsgErro(resposta.getProperty("MsgErro").toString());
frt.setValorSemAdicionais(resposta.getProperty("ValorSemAdicionais").toString())
frt.setObsFim(resposta.getProperty("ObsFim").toString());


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


And this is the response on android studio when i calling the webservice with an String[] return

anyType{cServico=anyType{Codigo=40010; Valor=1720; PrazoEntrega=1; ValorMaoPropria=000; ValorAvisoRecebimento=000;
ValorValorDeclarado=000; EntregaDomiciliar=S; EntregaSabado=S;
Erro=0; MsgErro=anyType{}; ValorSemAdicionais=1720; obsFim=anyType{}; }; }


I have tryed this solution but this not worked for me...
Android java.lang.RuntimeException: illegal property: while getting the string from SOAP Object(KSOAP2)

PS:I'm using KSoap2 lib

Answer

Codigo,Valor and other properties are children of cServico and not direct property of anyType. So access these via object of cServico like follows:

SoapObject resposta = (SoapObject) envelope.bodyIn; 
SoapObject body = (SoapObject) resposta.getProperty(0); 
SoapObject subResposta = (SoapObject) body.getProperty("Servicos"); 
SoapObject subRespostaDois = (SoapObject) subResposta.getProperty("cServico") ; 

Only then:

frt = new Frete();

frt.setCodigo(Integer.parseInt(subRespostaDois.getProperty("Codigo").toString()));
frt.setValor(subRespostaDois.getProperty("Valor").toString());