I discovered the real problem of my code when I did the Juven_v test: when WS returned me a single result presented the error of java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to java.util.Vector
because I was trying to pass a single Soapobject to a Vector.
What I did then, I treated the exception, IE, each when he did not get cast for Vector I command in the catch it pass to a single Soapobject because only came a result of WS. I don’t know if it’s the most practical way or the best way to perform more is what you have so far and solving it.
Follow the code below
public ArrayList<CidadesAt> buscarcidades(String uf){
ArrayList<CidadesAt> resultado = new ArrayList<CidadesAt>();
SoapObject buscacidade = new SoapObject(NAMESPACE,buscarcidsat);
buscacidade.addProperty("uf", uf);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.setOutputSoapObject(buscacidade);
envelope.implicitTypes =true;
HttpTransportSE http = new HttpTransportSE(Proxy.NO_PROXY,URL,timeout);
try {
http.call("urn:" + buscarcidsat, envelope);
Vector<SoapObject> resposta = (Vector<SoapObject>) envelope.getResponse();//se vier um vetor segue daqui
if(resposta != null) {
for (SoapObject so : resposta) {
CidadesAt cidades = new CidadesAt();
cidades.setCidade(so.getProperty(0).toString());
resultado.add(cidades);
}
}
}catch( java.lang.ClassCastException e) {//trata aqui caso tenha Exception de cast quer dizer que foi so um registro
e.printStackTrace();
try {
SoapObject resposta = (SoapObject) envelope.getResponse();
if (resposta != null) {
CidadesAt cidades = new CidadesAt();
cidades.setCidade(resposta.getProperty(0).toString());
resultado.add(cidades);
}
return resultado;
} catch (Exception e2) {
e2.printStackTrace();
return null;
}
}catch (Exception e3) {//outro tipo de Exception
e3.printStackTrace();
return null;
}
return resultado;
}
That’s it Thank you all and hug.
I don’t know this technology, but the error seems very clear: it seems that you have an object
SoapObject
and not aVector<SoapObject>
. You’ve tried it this way:SoapObject resposta = (SoapObject)envelope.getResponse();
?– Pablo Almeida
Pablo thanks for the reply, I did as you told me and it works but only brings the first record, I thought a Vector to bring the whole array. For example when I pass a UF to the WS returns me all the cities of that UF understood? I do not know if there is another way to do this.
– RodrigoPassador
I got it. I took the liberty of editing your title to clarify what your question is. I don’t know Ksoap 2, but I suggest you research how to read multiples
SoapObject
, since that seems to be your problem.– Pablo Almeida