WebService Post Office - Tracking

0

I'm trying to consume the Post Office Web Service - Tracking. In net Benas I imported the WSDL into new-> client for Webservice. The problem is that the WebService is returning the empty list, I can not understand.

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 giving this error:

  

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 CONSTRUCTION FAIL (total time: 5 seconds)

    
asked by anonymous 23.03.2017 / 14:19

1 answer

0

I'm implementing the same case and you can use this example that works perfectly: Just notice that the code arrives as a parameter and in the end I treat xml . You may feel free to make your changes:

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;
    
23.03.2017 / 14:34