Webservice Post Office - Tracking

Asked

Viewed 2,850 times

0

I am trying to consume the web service of the Post Office - Tracking. On the net Benas imported the WSDL in new->client to Webservice. The problem is that Webservice is returning the empty list, I’m not getting it.

public static void main(String[] args) {
    Sroxml result = buscaEventos("ECT", "SRO", "L", "T", "101", "OC013972795BR");
    System.out.println("qtd "+result.getQtd());
    System.out.println("Objeto "+result.getObjeto());
    System.out.println(result.getObjeto().get(0).getNumero());
    System.out.println(result.getObjeto().get(0).getEvento().get(0).getDescricao());
}

private static Sroxml buscaEventos(java.lang.String usuario, java.lang.String senha, java.lang.String tipo, java.lang.String resultado, java.lang.String lingua, java.lang.String objetos) {
    br.com.correios.webservice.resource.Rastro service = new br.com.correios.webservice.resource.Rastro();
    br.com.correios.webservice.resource.Service port = service.getServicePort();
    return port.buscaEventos(usuario, senha, tipo, resultado, lingua, objetos);
}

}

And you’re making this mistake:

run: Qtd 1

Object []

Exception in thread "main" java.lang.Indexoutofboundsexception: Index: 0, Size: 0 at java.util.Arraylist.rangeCheck(Arraylist.java:653) at java.util.Arraylist.get(Arraylist.java:429) at javaapplication3.Javaapplication3.main(Javaapplication3.java:11) C: Users Lorena Documents Netbeansprojects Javaapplication3 nbproject build-impl.xml:1041: The following error occurred while executing this line: C: Users Lorena Documents Netbeansprojects Javaapplication3 nbproject build-impl.xml:806: Java returned: 1 BUILD FAILURE (total time: 5 seconds)

  • 1

    I am implementing the same thing, if you want to put my code as I search the information.

  • Douglas, I would really appreciate it if you would post.

1 answer

1


I am implementing the same case and you can use this example that works perfectly: Just note that the code arrives as parameter and in the end I treat the xml. You can feel free to make your modifications:

public static String consultarCodigo(String codigo) throws IOException, SOAPException{

            String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:res=\"http://resource.webservice.correios.com.br/\">"
                    + "   <soapenv:Header/>" + "   <soapenv:Body>" + "      <res:buscaEventos>"
                    + "         <usuario>ECT</usuario>" + "         <senha>SRO</senha>" + "         <tipo>L</tipo>"
                    + "         <resultado>T</resultado>" + "         <lingua>101</lingua>"
                    + "         <objetos>"+codigo+"</objetos>" + "      </res:buscaEventos>" + "   </soapenv:Body>"
                    + "</soapenv:Envelope>";
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            // URL do Webservice
            String url = "http://webservice.correios.com.br:80/service/rastro";
            MimeHeaders headers = new MimeHeaders();
            headers.addHeader("Content-Type", "text/xml");

            MessageFactory messageFactory = MessageFactory.newInstance();

            SOAPMessage msg = messageFactory.createMessage(headers, (new ByteArrayInputStream(xml.getBytes())));

            SOAPMessage soapResponse = soapConnection.call(msg, url);
            Document xmlRespostaARequisicao = soapResponse.getSOAPBody().getOwnerDocument();
            return passarXMLParaString(xmlRespostaARequisicao, 4);
        }

        public static String passarXMLParaString(Document xml, int espacosIdentacao) {
            try {
                // set up a transformer
                TransformerFactory transfac = TransformerFactory.newInstance();
                transfac.setAttribute("indent-number", new Integer(espacosIdentacao));
                Transformer trans = transfac.newTransformer();
                trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                trans.setOutputProperty(OutputKeys.INDENT, "yes");

                // create string from xml tree
                StringWriter sw = new StringWriter();
                StreamResult result = new StreamResult(sw);
                DOMSource source = new DOMSource(xml);
                trans.transform(source, result);
                String xmlString = sw.toString();
                return xmlString.substring(xmlString.indexOf("<objeto>"), xmlString.indexOf("</objeto>")+10);
            } catch (TransformerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.exit(0);
            }
            return null;
  • Douglas, how do you access the ratings? for example to save in the database, I want to save only the last object status, as I assign this event to a variable?

  • <object> <number>OC013972795BR</number> <acronym>OC</acronym> <name>ORDER E-SEDEX (ETIQ LOGICA)</name> <category>E-SEDEX</category> <event> <type>BDE</type> <status>01</status> <date>22/03/2017</date> <time>14:26</hour> <Description>Object delivered to recipient</Description>

  • I’m using a technology called JAXB, it kind of takes the values between tags and populates an object attribute.

  • I’m gonna test......

  • Douglas, to access the values of the return String Xml, I found very good this method also: http://answall.com/questions/92363/rescues

Browser other questions tagged

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