Topic: Brazilian find postcode need help

procedure ConsultarCEP(vCEP: string; var vLogradouro, vBairro, vCidade, vUF: string);
var
  vHTTP: TIdHTTP;
  vXML: TStringStream;
  vXMLDoc: TXMLDocument;
  vCEPNode, vLogradouroNode, vBairroNode, vCidadeNode, vUFNode: IXMLNode;
begin
  vHTTP := TIdHTTP.Create(nil);
  vXML := TStringStream.Create('');
  try
    vHTTP.Get('https://viacep.com.br/ws/' + vCEP + '/xml/', vXML);
    vXML.Position := 0;
    ReadXMLFile(vXMLDoc, vXML);
    vCEPNode := vXMLDoc.ChildNodes.FindNode('xmlcep');
    if Assigned(vCEPNode) then
    begin
      vLogradouroNode := vCEPNode.ChildNodes.FindNode('logradouro');
      vBairroNode := vCEPNode.ChildNodes.FindNode('bairro');
      vCidadeNode := vCEPNode.ChildNodes.FindNode('localidade');
      vUFNode := vCEPNode.ChildNodes.FindNode('uf');
      if Assigned(vLogradouroNode) then
        vLogradouro := vLogradouroNode.Text;
      if Assigned(vBairroNode) then
        vBairro := vBairroNode.Text;
      if Assigned(vCidadeNode) then
        vCidade := vCidadeNode.Text;
      if Assigned(vUFNode) then
        vUF := vUFNode.Text;
    end;
  finally
    vHTTP.Free;
    vXML.Free;
    vXMLDoc.Free;
  end;
end;


this code is correct? im try but is some error.

Re: Brazilian find postcode need help

Some classes and functions are not supported in MVD.

Re: Brazilian find postcode need help

procedure ConsultarCEP(vCEP: string; var vLogradouro, vBairro, vCidade, vUF: string);
var s :variant;
begin
try
 s:=CreateOleObject('ScriptControl');
 s.Language := 'JavaScript';
 s := s.Eval('('+HttpGET( 'https://viacep.com.br/ws/'+vCEP+'/json/')+')');
 try
    vLogradouro := s.logradouro;
    vBairro := s.bairro;
    vCidade := s.localidade;
    vUF := s.uf;
 except
     if (s.erro) = 'true' then
     ShowMessage('Not found: "'+vCEP+'"');
 end;
 finally
   s:=0;
 end;

end;

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var Logradouro, Bairro,  Cidade, UF:string;
begin
  ConsultarCEP('01001000',Logradouro,Bairro,Cidade,UF);
  ShowMessage(Logradouro +#13#10+ Bairro +#13#10+ Cidade +#13#10+ UF);
end;   

Дальше сам...

Re: Brazilian find postcode need help

procedure Clientes_Button1_OnClick(Sender: TObject; var Cancel: boolean);
var
  Logradouro, Bairro, Cidade, UF: string;
  CEP: string;
begin
  // lê o valor da caixa de texto do CEP
  CEP := edCEP.Text;

  // verifica se o valor digitado é um CEP válido
  if (Length(CEP) <> 8) or (not TryStrToInt(CEP, CEP)) then
  begin
    ShowMessage('CEP inválido!');
    Exit; // interrompe a execução da função
  end;

  // executa a consulta com o CEP digitado
  ConsultarCEP(CEP, Logradouro, Bairro, Cidade, UF);

  // atribui os valores retornados às caixas de texto
  edLogradouro.Text := Logradouro;
  edBairro.Text := Bairro;
  edCidade.Text := Cidade;
  edUF.Text := UF;
end;

undeclared indentifier edCEP

thanks for code, but now another error.

Re: Brazilian find postcode need help

should be
FormName.ComponentName.Text

Re: Brazilian find postcode need help

undeclared indentifier TryStrToInt
sorry another error now.

procedure Clientes_Button1_OnClick(Sender: TObject; var Cancel: boolean);
var
  Logradouro, Bairro, Cidade, UF: string;
  CEP: string;
  CEPInt: integer;
begin
  // lê o valor da caixa de texto do CEP
  CEP := Clientes.edCEP.Text;

  // verifica se o valor digitado é um CEP válido
  if (Length(CEP) <> 8) or (not TryStrToInt(CEP, CEPInt)) then
  begin
    ShowMessage('CEP inválido!');
    Exit; // interrompe a execução da função
  end;

  // executa a consulta com o CEP digitado
  ConsultarCEP(IntToStr(CEPInt), Logradouro, Bairro, Cidade, UF);

  // atribui os valores retornados às caixas de texto
  Clientes.edLogradouro.Text := Logradouro;
  Clientes.edBairro.Text := Bairro;
  Clientes.edCidade.Text := Cidade;
  Clientes.edUF.Text := UF;
end;

Re: Brazilian find postcode need help

Hello amsjunior1
Put an space between Try and StrToInt or better
Try
StrToInt(xxx)

JB

8 (edited by sparrow 2023-03-23 07:44:05)

Re: Brazilian find postcode need help

Hi Jean,

No, that won't work.
TryStrToInt is a function that tries to convert a string to an integer and returns the result. True or False .
But this function is not supported in MVD.
ValidInt() can be used. Or rewrite the procedure a bit. But as I understand it, this is only a small part of what they are trying to adapt.


amsjunior1 can be advised to start reading the Delphi and MVD documentation.