How to read multiple Soapobject from a Webservice?

Asked

Viewed 319 times

1

I am trying to consume a Web Service in Java using Ksoap 2 in version 3.4.0.

When I do a search without parameters it returns the expected result but when I do a search passing a parameter addProperty it displays the error on the line receiving the WS response:

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

Error appears:

org.ksoap2.serialization.Soapobject cannot be cast to java.util.Vector

  • I don’t know this technology, but the error seems very clear: it seems that you have an object SoapObject and not a Vector<SoapObject> . You’ve tried it this way: SoapObject resposta = (SoapObject)envelope.getResponse(); ?

  • 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.

  • 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.

2 answers

0

In fact, you cannot convert the return type of the getresponse() method to a vector. What can be done is 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 were an object vector (or sub-vectors) it would look 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

  • Thanks for answering Juven_v was very helpful.

0


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.