Consume web service

Asked

Viewed 259 times

0

I come as an idea to deploy in a system that I am setting up a web service, where the user informs the zip code, and address fields if completed. However I find nothing about how to do this in desktop applications.inserir a descrição da imagem aqui

What I would like is that when the cep field was filled in the following fields in gray they would complete themselves.

2 answers

2

The webservice Viacep is what you seek.

Example called by passing the zip code in the parameter:

https://viacep.com.br/ws/88111500/json/

Answer:

{
  "cep": "88111-500",
  "logradouro": "Rua Otto Júlio Malina",
  "complemento": "",
  "bairro": "Ipiranga",
  "localidade": "São José",
  "uf": "SC",
  "unidade": "",
  "ibge": "4216602",
  "gia": ""
}
  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • @Marconi, I don’t know how I could collaborate with essential parts or code here, since the question just asks a Webservice capable of consuming the data. Any suggestions to improve my response in this regard?

  • Cleber, put relevant parts of the code that can help you find a solution to your problem. Link-only responses are usually deleted.

  • In these cases use a comment.

  • 1

    Right! I have access to Viacep blocked at the moment. At lunch time I will access and complement the answer!

  • Sure I’m studying the code in gitlab, once I succeed put here as the code got

Show 1 more comment

0


Follow code used to consume web service NOTE: I didn’t use the via cep, I found a great example that worked perfectly with little code, but 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, simply instantiate it and throw the information in the respective fields using "setText".

Browser other questions tagged

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