Consume web service

0

I come as an idea of deploying on a system that I am putting together a web service, where the user informs the zip code, and the address fields if they complete. However I do not think anything about how to do this in desktop applications.

What I would like is that when the zip field was filled in the following gray fields would autocomplete themselves.

    
asked by anonymous 14.09.2017 / 05:13

2 answers

0

Follow code used to consume web service Note: I did not use the via zip, I found a great example that worked perfectly with little code, however it is necessary to add an external library called "dom4j-1.6.1"

import java.net.URL;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class SearchForZipCode 
{
    // Properties
    private String state;
    private String city;
    private String neighborhood;
    private String street;

    private int result = 0;
    private String resultText;

    // Constructor
    @SuppressWarnings("rawtypes")
    public SearchForZipCode(String zipCode)
    {
        try 
        {
            URL url = new URL("http://cep.republicavirtual.com.br/web_cep.php?cep=" + zipCode + "&formato=xml");

            Document document = getDocumento(url);

            Element root = document.getRootElement();

            for (Iterator i = root.elementIterator(); i.hasNext();) 
            {
                Element element = (Element) i.next();

                if(element.getQualifiedName().equals("uf")) setState(element.getText());

                if(element.getQualifiedName().equals("cidade")) setCity(element.getText());

                if(element.getQualifiedName().equals("bairro")) setNeighborhood(element.getText());

                if(element.getQualifiedName().equals("logradouro")) setStreet(element.getText());

                if(element.getQualifiedName().equals("resultado")) setResult(Integer.parseInt(element.getText()));

                if(element.getQualifiedName().equals("resultado_txt")) setResultText(element.getText());    
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public Document getDocumento(URL url) throws DocumentException 
    {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);

        return document;
    } 


    public String getState() 
    {
        return state;
    }
    public void setState(String state) 
    {
        this.state = state;
    }

    public String getCity() 
    {
        return city;
    }
    public void setCity(String city) 
    {
        this.city = city;
    }

    public String getNeighborhood() 
    {
        return neighborhood;
    }
    public void setNeighborhood(String neighborhood) 
    {
        this.neighborhood = neighborhood;
    }

    public String getStreet() 
    {
        return street;
    }
    public void setStreet(String street) 
    {
        this.street = street;
    }

    public int getResult() 
    {
        return result;
    }
    public void setResult(int result) 
    {
        this.result = result;
    }

    public String getResultText() 
    {
        return resultText;
    }
    public void setResultText(String resultText) 
    {
        this.resultText = resultText;
    }
}

After building this class, just instantiate it and play the information in the respective fields using the "setText".

    
15.09.2017 / 01:15
2

The webservice ViaCEP is what you are looking for.

Example call passing the cep into the parameter:

link

Answer:

{
  "cep": "88111-500",
  "logradouro": "Rua Otto Júlio Malina",
  "complemento": "",
  "bairro": "Ipiranga",
  "localidade": "São José",
  "uf": "SC",
  "unidade": "",
  "ibge": "4216602",
  "gia": ""
}
    
14.09.2017 / 13:56