Getting a response from Webservice with Ksoap

Asked

Viewed 78 times

1

I have a web service that returns this data:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <LISTACLIENTESRESPONSE xmlns="http://localhost:8093/">
         <LISTACLIENTESRESULT>
            <ACLIENTE>
               <STCLIENTE>
                  <CCODIGO>000001</CCODIGO>
                  <CRAZAO>PEDRO DA SILVA</CRAZAO>
                  <CMUNICIPIO>SAO PAULO</CMUNICIPIO>
                  <CVENDEDOR>JOAO</CVENDEDOR>
               </STCLIENTE>
               <STCLIENTE>
                  <CCODIGO>000002</CCODIGO>
                  <CRAZAO>JOSE</CRAZAO>
                  <CMUNICIPIO>SAO PAULO</CMUNICIPIO>
                  <CVENDEDOR>PAULO</CVENDEDOR>
               </STCLIENTE>
               <STCLIENTE>
                  <CCODIGO>000003</CCODIGO>
                  <CRAZAO>LETICIA</CRAZAO>
                  <CMUNICIPIO>SAO PAULO</CMUNICIPIO>
                  <CVENDEDOR>PEDRO</CVENDEDOR>
               </STCLIENTE>
            </ACLIENTE>
            <CMENSAGEM>3 Clientes Localizados</CMENSAGEM>
         </LISTACLIENTESRESULT>
      </LISTACLIENTESRESPONSE>
   </soap:Body>
</soap:Envelope>

In my Android studio project, I can connect and send the data to WS but am not understanding how to recover the data provided by WS.

envelope.setOutputSoapObject(buscarClientes);//Metodo esta correto

HttpTransportSE http = new HttpTransportSE(URL);

try {
    SoapObject soapObject = 
    (SoapObject)resposta.getProperty("ACLIENTE");
    for(int i = 0; i < soapObject.getPropertyCount(); i++) {
        soapObject.getProperty(1).toString());
        Cliente cliente = new Cliente();
        cliente.setCodigo(soapObject.getProperty("CCODIGO").toString());                
        cliente.setRazao(soapObject.getProperty("CRAZAO").toString());
        cliente.setMun(soapObject.getProperty("CMUNICIPIO").toString());
        listaClientes.add(cliente);
    }
} catch (Exception e){
    return listaClientes;
}

How do I recover this data? The return is an array of clients and a message.

I ran this code:

SoapObject resposta = (SoapObject) envelope.getResponse(); 
String clientes = envelope.getResponse().toString(); 
Log.i("CLIENTE",clientes);

O retorno no Log foi esse: anyType{ACLIENTE=anyType {STCLIENTE=anyType{CCODIGO=000819; CMUNICIPIO=SAO PAULO; CRAZAO=CLIENTE; CVENDEDOR=FIR1; };STCLIENTE=anyType{CCODIGO=001951; CMUNICIPIO=SANTOS;CRAZAO=CLIENTE 2; CVENDEDOR=FIR2; };}CMENSAGEM=2 Clientes Localizados.

Returned all Web Service data.

  • See if String clientes = envelope.getResponse().toString(); help you. This should convert the array contained in the Response into String, that then you will manipulate to fill your object Cliente. If it works, let me know and I’ll create a proper response.

  • How to manipulate this string to get separate information?

  • One question: in your original code, do you get to receive as an answer that XML you put in the question? If yes, it is only a matter of you using a parser XML, which will map each XML client to a class Cliente you can create with the properties brought in in XML. Nor would you need to convert to String.

  • The return returned as posted above. In the program Sopui the return appears as XML code with the tags between <>. But in the string the tags are between {}. But this return is composed of the following structure: On the first level I have two tags: TAG CMENSAGEM (which is a string) and TAG ACLIENTE (which is an array of clients). For TAG ACLIENTE, there is level 2 which is the STCLIENT tag (which are the array clients) and within this tag, has the Tags: CCODIGO; CMUNICIPIO, CRAZAO and CVENDEDOR (which are the information of each client. In this structure can I use the XML parse? Coo do?

1 answer

0

I managed to solve. I tried to solve it by Parse but it didn’t work, because when I pass the Ksoap object to string, it messes up all the XML code, adding brackets and removing some tags.

I used Ksoap himself to solve.

     HttpTransportSE http = new HttpTransportSE(URL);

    try {

        http.call(METODOLISTA,envelope);
        SoapObject resposta = (SoapObject) envelope.getResponse();
        String mensagem = resposta.getPrimitiveProperty("CMENSAGEM").toString();
        SoapObject respostaClientes = (SoapObject) resposta.getProperty("ACLIENTE");

        for(int x = 0; x < respostaClientes.getPropertyCount(); x++) 
       {
            SoapObject clienteOb = (SoapObject) respostaClientes.getProperty(x);
            Cliente cliente = new Cliente();
            cliente.setCodigo(clienteOb.getPrimitiveProperty("CCODIGO").toString());
            cliente.setRazao(clienteOb.getPrimitiveProperty("CRAZAO").toString());
    cliente.setMunicipio(clienteOb.getPrimitiveProperty("CMUNICIPIO").toString());
            cliente.setVendedor(clienteOb.getPrimitiveProperty("CVENDEDOR").toString());
            listaClientes.add(cliente);
       }

    } catch (Exception e)
    {
           return listaClientes;
        }

The line - Soapobject answer = (Soapobject) envelope.getresponse() returned the entire XML.

As the tag "CMENSAGEM" is in the first level, I can recover it with the command:

String message = response.getPrimitiveProperty("CMENSAGEM"). toString();

Hence the cat jump, the tag "ACLIENTE" is an array of "STCLIENTE" tags. With the command below - answerClientes - received XML from the tag "ACLIENTE"

Soapobject respostaClientes = (Soapobject) reply.getProperty("ACLIENTE");

Then just go through the object answerClient and go capturing the data Worked........

Browser other questions tagged

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