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".
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
@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 Griff
Cleber, put relevant parts of the code that can help you find a solution to your problem. Link-only responses are usually deleted.
– Marconi
In these cases use a comment.
– ramaral
Right! I have access to Viacep blocked at the moment. At lunch time I will access and complement the answer!
– Cleber Griff
Sure I’m studying the code in gitlab, once I succeed put here as the code got
– Rodrigo Prado