1
Good morning, you guys!
I have a zip code search engine that is no longer working. When I use the URL passing my zip code it returns the query. Thanks for the help!
Here is a example link to test the query directly in the browser. Here is the result:
<webservicecep>
<resultado>1</resultado>
<resultado_txt>sucesso - cep completo</resultado_txt>
<uf>SP</uf>
<cidade>Marília</cidade>
<bairro>Vila Romana</bairro>
<tipo_logradouro>Rua</tipo_logradouro>
<logradouro>Antônio Pinheiro Faro</logradouro>
</webservicecep>
public void encontraCEP() {
CepWebService cepWebService = new CepWebService(cliente.getCep());
if (cepWebService.getResultado() == 1) {
cliente.setLogradouro(cepWebService.getLogradouro());
cliente.setUf(cepWebService.getEstado());
cliente.setCidade(cepWebService.getCidade());
cliente.setBairro(cepWebService.getBairro());
// cliente.setTipoEndereco(cepWebService.getTipoLogradouro());
} else {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Cep não encontraro ou erro no servidor",
"Digite os dados manualmente"));
}
}
public class CepWebService {
private String estado = "";
private String cidade = "";
private String bairro = "";
private String tipoLogradouro = "";
private String logradouro = "";
private int resultado = 0;
@SuppressWarnings("rawtypes")
public CepWebService(String cep) {
try {
URL url = new URL(
"http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep
+ "&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"))
setEstado(element.getText());
if (element.getQualifiedName().equals("cidade"))
setCidade(element.getText());
if (element.getQualifiedName().equals("bairro"))
setBairro(element.getText());
if (element.getQualifiedName().equals("tipo_logradouro"))
setTipoLogradouro(element.getText());
if (element.getQualifiedName().equals("logradouro"))
setLogradouro(element.getText());
if (element.getQualifiedName().equals("resultado"))
setResultado(Integer.parseInt(element.getText()));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Document getDocumento(URL url) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(url);
return document;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getTipoLogradouro() {
return tipoLogradouro;
}
public void setTipoLogradouro(String tipoLogradouro) {
this.tipoLogradouro = tipoLogradouro;
}
public String getLogradouro() {
return logradouro;
}
public void setLogradouro(String logradouro) {
this.logradouro = logradouro;
}
public int getResultado() {
return resultado;
}
public void setResultado(int resultado) {
this.resultado = resultado;
}
}
I did the following test. I passed the direct zip code to test and is returning debug result.
public void encontraCEP() {
CepWebService cepWebService = new CepWebService("17522350");
if (cepWebService.getResultado() == 1) {
cliente.setLogradouro(cepWebService.getLogradouro());
cliente.setUf(cepWebService.getEstado());
cliente.setCidade(cepWebService.getCidade());
cliente.setBairro(cepWebService.getBairro());
//cliente.setTipoEndereco(cepWebService.getTipoLogradouro());
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Cep não encontraro ou erro no servidor",
"Digite os dados manualmente"));
}
}
Man CommandButton
is ordering null
for the method:
<p:outputLabel for="cep" value="CEP" />
<h:panelGroup>
<p:inputMask id="cep" mask="99999-999" size="9"
value="#{cadastroClienteBean.cliente.cep}" />
<p:commandButton value="Buscar Endereço" style=" color: #D20005" >
<f:ajax listener="#{cadastroClienteBean.encontraCEP}" render="@form"
event="click" />
</p:commandButton>
</h:panelGroup>
The return of the service is correct. It returns the xml content of the zip code address. What problem are you facing? PS: I suggest that to serialize xml you use an API that does this for you, like Xstream (http://x-stream.github.io/).
– Giuliana Bezerra
@Giulianabezerra He is falling straight into the Else, returning to msg that zip zip was not found.
– Sidnei Ferreira
Ah, I get it. Then do the following, start by seeing if the root variable is getting the values loaded from xml, i.e., if the execution stream is entering the for. I’ve never used this Saxreader for serialization, so I don’t know the format it returns.
– Giuliana Bezerra
Are you sure that client.getCep() is not null? is the only way to fall into Else, the code is correct, debug and please check.
– Dilnei Cunha
@Dilneicunha The class that makes the query is working, is returning the zip code. I’m doing something wrong with my method or my page. I have thus 'Cepwebservice cepWebService = new Cepwebservice("17522350");' and returned cep in debug.
– Sidnei Ferreira
I tested here in a standalone class your code and it works well, you have to go line by line in the debug to identify why it falls into Else, or if you don’t have another hidden Exception.
– Dilnei Cunha
@Dilneicunha edited the question and put the debug result. Thanks
– Sidnei Ferreira
If returned brother then it does not fall into Else, has some other error or problem ?
– Dilnei Cunha
So, I was thinking I was falling into Isis, not weighing that it could be the getCep that was null. Now I need to see why getCep is null,
– Sidnei Ferreira
declares type="Submit" on command button and test please.
– Dilnei Cunha
@Dilneicunha It worked out! Thank you so much for your help.
– Sidnei Ferreira