Autor: luislacombe | Publicado: 2014-03-09T18:38:00-07:00

Obter endereço a partir de um cep com ADVPL

Abaixo abaixo estou disponibilizando um pequeno código que recebe um cep como parâmetro e acessa um web service, obtendo como retorno o endereço correspondente. Espero que seja útil.

#INCLUDE 'PROTHEUS.CH'
#INCLUDE 'RWMAKE.CH'
#INCLUDE 'FONT.CH'
#INCLUDE 'COLORS.CH'


User Function VerCep()

Private cBairro &nbsp; &nbsp;:= Space(100)
Private cCidade &nbsp; &nbsp;:= Space(100)
Private cLog &nbsp; &nbsp; &nbsp; := Space(100)
Private cTipLog &nbsp; &nbsp;:= Space(20)
Private cUF &nbsp; &nbsp; &nbsp; &nbsp;:= Space(20) &nbsp; &nbsp; &nbsp;
Private cCep &nbsp; &nbsp; &nbsp; := Space(12)


SetPrvt("oDlg1","oSay1","oSay2","oSay3","oSay4","oSay5","oSay6","oGet1","oSBtn1")


oDlg1 &nbsp; &nbsp; &nbsp;:= MSDialog():New( 088,232,316,624,"oDlg1",,,.F.,,,,,,.T.,,,.T. )
oSay1 &nbsp; &nbsp; &nbsp;:= TSay():New( 012,012,{||"Digite o CEP:"},oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,036,008)
oSay2 &nbsp; &nbsp; &nbsp;:= TSay():New( 044,016,,oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,032,008)
oSay3 &nbsp; &nbsp; &nbsp;:= TSay():New( 044,056,,oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,128,012)
oSay4 &nbsp; &nbsp; &nbsp;:= TSay():New( 064,016,,oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,168,012)
oSay5 &nbsp; &nbsp; &nbsp;:= TSay():New( 084,012,,oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,032,012)
oSay6 &nbsp; &nbsp; &nbsp;:= TSay():New( 084,052,,oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,132,012)
oGet1 &nbsp; &nbsp; &nbsp;:= TGet():New( 012,052,{|u| If(PCount()&gt;0,cCep:=u,cCep)},oDlg1,060,008,'',,CLR_BLACK,CLR_WHITE,,,,.T.,"",,,.F.,.F.,,.F.,.F.,"","cCep",,)
oSBtn1 &nbsp; &nbsp; := SButton():New( 012,160,1,,oDlg1,,"", ) &nbsp; &nbsp;

oSBtn1:bLClicked := {|| LoadCEP() }

oDlg1:Activate(,,,.T.)

Return&nbsp;

Static Function LoadCEP()
Local cError &nbsp; := ""
Local cWarning := ""
Local aRetorno := {}&nbsp;
Local cRETORNO &nbsp;//obtem o xml

cRETORNO := HttpGet( 'http://republicavirtual.com.br/web_cep.php?cep='+cCep+'&amp;formato=xml' )

if cRETORNO == nil
&nbsp; &nbsp;MsgStop( Alert( "Servidor de CEP indisponível." ) )
else
&nbsp; &nbsp;oScript := XmlParser( cRETORNO, "_", @cError, @cWarning )
&nbsp; &nbsp;if oScript &lt;&gt; nil
&nbsp; &nbsp; &nbsp; if XmlChildEx( oSCRIPT, "_WEBSERVICECEP" ) &lt;&gt; nil &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Val(oSCRIPT:_WEBSERVICECEP:_RESULTADO:TEXT) &gt; 0
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cUF &nbsp; &nbsp; := oSCRIPT:_WEBSERVICECEP:_UF:TEXT&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cCidade := oSCRIPT:_WEBSERVICECEP:_CIDADE:TEXT&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cBairro := oSCRIPT:_WEBSERVICECEP:_BAIRRO:TEXT&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cTipLog := oSCRIPT:_WEBSERVICECEP:_TIPO_LOGRADOURO:TEXT&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cLog &nbsp; &nbsp;:= oSCRIPT:_WEBSERVICECEP:_LOGRADOURO:TEXT&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;endif &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; endif
&nbsp; &nbsp;endif
endif&nbsp;

oSay2 &nbsp; &nbsp; &nbsp;:= TSay():New( 044,016,{||"UF: " + cUF},oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,032,008)
oSay3 &nbsp; &nbsp; &nbsp;:= TSay():New( 044,056,{||"Cidade: " + cCidade},oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,128,012)
oSay4 &nbsp; &nbsp; &nbsp;:= TSay():New( 064,016,{||"Bairro: " + cBairro},oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,168,012)
oSay5 &nbsp; &nbsp; &nbsp;:= TSay():New( 084,012,{|| cTipLog},oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,032,012)
oSay6 &nbsp; &nbsp; &nbsp;:= TSay():New( 084,052,{|| cLog},oDlg1,,,.F.,.F.,.F.,.T.,CLR_BLACK,CLR_WHITE,132,012) &nbsp; &nbsp; &nbsp;

Return


----------------------------------------------------------------------------------------------------
A aparência do programa é simples, agora é só incrementar do jeito que você quiser.