private void BuscarCEP(object sender, EventArgs args) //para ser um event handler preciso de dois parâmetros, um é um objeto e o outro Event args { //TODO lógica //TODO validações //TODO Busca na internet //TODO apresenta os dados string cep = ttb_CEP.Text.Trim(); Entidade_CEP end = BLL_CEP.BuscaCEP(cep); try { if (end != null) { lb_Resultado.Text = string.Format("Endereço: {0}, {1}, {2} - {3}, CEP: {4}", end.logradouro, end.bairro, end.localidade, end.uf, end.cep); } else { DisplayAlert("Alerta", "Cep Inválido", "OK"); } } catch (Exception e) { DisplayAlert("Erro Crítico", e.Message, "OK"); } }
private static string EnderecoURL = "http://viacep.com.br/ws/{0}/json/";//{0} parâmetro 0 public static Entidade_CEP BuscaCEP(string CEP) { string NovoEnderecoURL = string.Format(EnderecoURL, CEP); WebClient WB = new WebClient(); string Conteudo = WB.DownloadString(NovoEnderecoURL); Entidade_CEP end = JsonConvert.DeserializeObject <Entidade_CEP>(Conteudo); //Deserializar a string json em formato Entidade CEP if (end.cep == null) { return(null); } else { return(end); } }