How to read multiple SoapObject from a WebService?

1

I'm trying to consume a Web Service in Java using KSoap 2 in version 3.4.0.

When I do a search with no parameters it returns the expected result, but when I search by passing a parameter addProperty it presents the error in the line that receives the WS response:

Vector<SoapObject> resposta = (Vector<SoapObject>)envelope.getResponse();

The error appears:

  

org.ksoap2.serialization.SoapObject can not be cast to java.util.Vector

    
asked by anonymous 24.02.2016 / 16:33

2 answers

0

I discovered the real problem of my code when I did the Juven_v test: when WS returned me a single result it had 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 handled the exception, that is, each time it fails to cast to the Vector I send in the catch it passes to a single SoapObject because it only came from a WS result. I do not know if it is the most practical or the best performace is what it has for the moment and is solving. Here is the code

 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.

    
27.02.2016 / 21:41
0

Actually you can not do a return type conversion from the getResponse () method to a vector. What can be done is a cast to SoapObject and then access the properties (attributes) of the created object.

SoapObject resposta = (SoapObject)envelope.getResponse();
SoapPrimitive atributo1 = (SoapPrimitive) reposta.getProperty(0); //pode ser um indice ou uma chave
String atributo_string = atributo1.toString();

If the getResponse () method is a vector of objects (or sub vectors) it looks like this:

SoapObject resposta = (SoapObject)envelope.getResponse();
SoapObject atributo1_vetor = (SoapObject) reposta.getProperty(0); //pode ser um indice ou uma chave
SoapPrimitive atributo1 = (SoapPrimitive) atributo1_vetor.getProperty(0); //pode ser um indice ou uma chave
String atributo_string = atributo1.toString();

You can consult the library documentation ksoap

    
26.02.2016 / 02:45