kapiaba Posted February 24, 2014 Report Share Posted February 24, 2014 Pessoas ajuda ahi, esta função do Rochina, funciona quase perfeita, o único incoveniente, é que não retorna a acentuação corrretamente, como corrigir isto? /* Função de busca de Endereço pelo CEP * - Desenvolvido José Carlos da Rocha para ajaxbox.com.br * - Adaptação de exemplo para utilização com FIVEWIN FOR HARBOUR * - Utilizando WebService de CEP da republicavirtual.com.br */ #include "FiveWin.ch" function main( cep ) LOCAL resultado := {} DEFAULT cep:="05171340" url := "http://republicavirtual.com.br/web_cep.php?cep="+ cep +"&formato=query_string" xmlhttp := TOleAuto():New("MSXML2.ServerXMLHTTP") xmlhttp:Open( "GET", url, .F.) xmlhttp:Send() xmlhttp_resultado := xmlhttp:responseText arr_resultado := Split( xmlhttp_resultado, "&" ) txt_resultado := "" if .not. ( len(arr_resultado) = 1 ) for i = lbound( arr_resultado ) to ubound( arr_resultado ) aadd( resultado, arr_resultado[ i ] ) txt_resultado := txt_resultado + StrTran( arr_resultado[ i ], "+", " " ) + CRLF next ? txt_resultado else ? "Falha na pesquisa do CEP." endif return .t. Function lBound() return 1 Function uBound( arr ) return len( arr ) function Split( cString, cSeparator ) LOCAL nPos LOCAL aString := {} DEFAULT cSeparator := ";" cString := ALLTRIM( cString ) + cSeparator DO WHILE .T. nPos := AT( cSeparator, cString ) IF nPos = 0 EXIT ENDIF AADD( aString, SUBSTR( cString, 1, nPos-1 ) ) cString := SUBSTR( cString, nPos+1 ) ENDDO RETURN ( aString ) function str_replace( _oque_ ) LOCAL cCAcentos := 'àâêôûãõáéíóúçüîäëïöèìòùÀÂÊÔÛÃÕÁÉÍÓÚÇÜÎÄËÏÖÈÌÒÙÆ' LOCAL cSAcentos := 'aaeouaoaeioucuiaeioeiouAAEOUAOAEIOUCUIAEIOEIOUA' for i := 1 to Len( cCAcentos ) _oque_ := StrTran( _oque_, SubStr( cCAcentos, i, 1), SubStr( cSAcentos, i, 1) ) next RETURN _oque_ FUNCTION DecTOWhat( Oque, ParaQue ) local aDecTOWhat := {} aadd( aDecTOWhat, { 32, "20", " ", "%20", "010 0000", "40" } ) aadd( aDecTOWhat, { 33, "21", "!", "%21", "010 0001", "41" } ) aadd( aDecTOWhat, { 34, "22", ["], "%22", "010 0010", "42" } ) aadd( aDecTOWhat, { 35, "23", "#", "%23", "010 0011", "43" } ) aadd( aDecTOWhat, { 36, "24", "$", "%24", "010 0100", "44" } ) aadd( aDecTOWhat, { 37, "25", "%", "%25", "010 0101", "45" } ) aadd( aDecTOWhat, { 38, "26", "&", "%26", "010 0110", "46" } ) aadd( aDecTOWhat, { 39, "27", "'", "%27", "010 0111", "47" } ) aadd( aDecTOWhat, { 40, "28", "(", "%28", "010 1000", "50" } ) aadd( aDecTOWhat, { 41, "29", ")", "%29", "010 1001", "51" } ) aadd( aDecTOWhat, { 42, "2A", "*", "%2A", "010 1010", "52" } ) aadd( aDecTOWhat, { 43, "2B", "+", "%2B", "010 1011", "53" } ) aadd( aDecTOWhat, { 44, "2C", ",", "%2C", "010 1100", "54" } ) aadd( aDecTOWhat, { 45, "2D", "-", "%2D", "010 1101", "55" } ) aadd( aDecTOWhat, { 46, "2E", ".", "%2E", "010 1110", "56" } ) aadd( aDecTOWhat, { 47, "2F", "/", "%2F", "010 1111", "57" } ) aadd( aDecTOWhat, { 48, "30", "0", "%30", "011 0000", "60" } ) aadd( aDecTOWhat, { 49, "31", "1", "%31", "011 0001", "61" } ) aadd( aDecTOWhat, { 50, "32", "2", "%32", "011 0010", "62" } ) aadd( aDecTOWhat, { 51, "33", "3", "%33", "011 0011", "63" } ) aadd( aDecTOWhat, { 52, "34", "4", "%34", "011 0100", "64" } ) aadd( aDecTOWhat, { 53, "35", "5", "%35", "011 0101", "65" } ) aadd( aDecTOWhat, { 54, "36", "6", "%36", "011 0110", "66" } ) aadd( aDecTOWhat, { 55, "37", "7", "%37", "011 0111", "67" } ) aadd( aDecTOWhat, { 56, "38", "8", "%38", "011 1000", "70" } ) aadd( aDecTOWhat, { 57, "39", "9", "%39", "011 1001", "71" } ) aadd( aDecTOWhat, { 58, "3A", ":", "%3A", "011 1010", "72" } ) aadd( aDecTOWhat, { 59, "3B", ";", "%3B", "011 1011", "73" } ) aadd( aDecTOWhat, { 60, "3C", "<", "%3C", "011 1100", "74" } ) aadd( aDecTOWhat, { 61, "3D", "=", "%3D", "011 1101", "75" } ) aadd( aDecTOWhat, { 62, "3E", ">", "%3E", "011 1110", "76" } ) aadd( aDecTOWhat, { 63, "3F", "?", "%3F", "011 1111", "77" } ) aadd( aDecTOWhat, { 64, "40", "@", "%40", "100 0000", "100" } ) aadd( aDecTOWhat, { 65, "41", "A", "%41", "100 0001", "101" } ) aadd( aDecTOWhat, { 66, "42", "B", "%42", "100 0010", "102" } ) aadd( aDecTOWhat, { 67, "43", "C", "%43", "100 0011", "103" } ) aadd( aDecTOWhat, { 68, "44", "D", "%44", "100 0100", "104" } ) aadd( aDecTOWhat, { 69, "45", "E", "%45", "100 0101", "105" } ) aadd( aDecTOWhat, { 70, "46", "F", "%46", "100 0110", "106" } ) aadd( aDecTOWhat, { 71, "47", "G", "%47", "100 0111", "107" } ) aadd( aDecTOWhat, { 72, "48", "H", "%48", "100 1000", "110" } ) aadd( aDecTOWhat, { 73, "49", "I", "%49", "100 1001", "111" } ) aadd( aDecTOWhat, { 74, "4A", "J", "%4A", "100 1010", "112" } ) aadd( aDecTOWhat, { 75, "4B", "K", "%4B", "100 1011", "113" } ) aadd( aDecTOWhat, { 76, "4C", "L", "%4C", "100 1100", "114" } ) aadd( aDecTOWhat, { 77, "4D", "M", "%4D", "100 1101", "115" } ) aadd( aDecTOWhat, { 78, "4E", "N", "%4E", "100 1110", "116" } ) aadd( aDecTOWhat, { 79, "4F", "O", "%4F", "100 1111", "117" } ) aadd( aDecTOWhat, { 80, "50", "P", "%50", "101 0000", "120" } ) aadd( aDecTOWhat, { 81, "51", "Q", "%51", "101 0001", "121" } ) aadd( aDecTOWhat, { 82, "52", "R", "%52", "101 0010", "122" } ) aadd( aDecTOWhat, { 83, "53", "S", "%53", "101 0011", "123" } ) aadd( aDecTOWhat, { 84, "54", "T", "%54", "101 0100", "124" } ) aadd( aDecTOWhat, { 85, "55", "U", "%55", "101 0101", "125" } ) aadd( aDecTOWhat, { 86, "56", "V", "%56", "101 0110", "126" } ) aadd( aDecTOWhat, { 87, "57", "W", "%57", "101 0111", "127" } ) aadd( aDecTOWhat, { 88, "58", "X", "%58", "101 1000", "130" } ) aadd( aDecTOWhat, { 89, "59", "Y", "%59", "101 1001", "131" } ) aadd( aDecTOWhat, { 90, "5A", "Z", "%5A", "101 1010", "132" } ) aadd( aDecTOWhat, { 91, "5B", "[", "%5B", "101 1011", "133" } ) aadd( aDecTOWhat, { 92, "5C", "\", "%5C", "101 1100", "134" } ) aadd( aDecTOWhat, { 93, "5D", "]", "%5D", "101 1101", "135" } ) aadd( aDecTOWhat, { 94, "5E", "^", "%5E", "101 1110", "136" } ) aadd( aDecTOWhat, { 95, "5F", "_", "%5F", "101 1111", "137" } ) aadd( aDecTOWhat, { 96, "60", "`", "%60", "110 0000", "140" } ) aadd( aDecTOWhat, { 97, "61", "a", "%61", "110 0001", "141" } ) aadd( aDecTOWhat, { 98, "62", "b", "%62", "110 0010", "142" } ) aadd( aDecTOWhat, { 99, "63", "c", "%63", "110 0011", "143" } ) aadd( aDecTOWhat, { 100, "64", "d", "%64", "110 0100", "144" } ) aadd( aDecTOWhat, { 101, "65", "e", "%65", "110 0101", "145" } ) aadd( aDecTOWhat, { 102, "66", "f", "%66", "110 0110", "146" } ) aadd( aDecTOWhat, { 103, "67", "g", "%67", "110 0111", "147" } ) aadd( aDecTOWhat, { 104, "68", "h", "%68", "110 1000", "150" } ) aadd( aDecTOWhat, { 105, "69", "i", "%69", "110 1001", "151" } ) aadd( aDecTOWhat, { 106, "6A", "j", "%6A", "110 1010", "152" } ) aadd( aDecTOWhat, { 107, "6B", "k", "%6B", "110 1011", "153" } ) aadd( aDecTOWhat, { 108, "6C", "l", "%6C", "110 1100", "154" } ) aadd( aDecTOWhat, { 109, "6D", "m", "%6D", "110 1101", "155" } ) aadd( aDecTOWhat, { 110, "6E", "n", "%6E", "110 1110", "156" } ) aadd( aDecTOWhat, { 111, "6F", "o", "%6F", "110 1111", "157" } ) aadd( aDecTOWhat, { 112, "70", "p", "%70", "111 0000", "160" } ) aadd( aDecTOWhat, { 113, "71", "q", "%71", "111 0001", "161" } ) aadd( aDecTOWhat, { 114, "72", "r", "%72", "111 0010", "162" } ) aadd( aDecTOWhat, { 115, "73", "s", "%73", "111 0011", "163" } ) aadd( aDecTOWhat, { 116, "74", "t", "%74", "111 0100", "164" } ) aadd( aDecTOWhat, { 117, "75", "u", "%75", "111 0101", "165" } ) aadd( aDecTOWhat, { 118, "76", "v", "%76", "111 0110", "166" } ) aadd( aDecTOWhat, { 119, "77", "w", "%77", "111 0111", "167" } ) aadd( aDecTOWhat, { 120, "78", "x", "%78", "111 1000", "170" } ) aadd( aDecTOWhat, { 121, "79", "y", "%79", "111 1001", "171" } ) aadd( aDecTOWhat, { 122, "7A", "z", "%7A", "111 1010", "172" } ) aadd( aDecTOWhat, { 123, "7B", "{", "%7B", "111 1011", "173" } ) aadd( aDecTOWhat, { 124, "7C", "|", "%7C", "111 1100", "174" } ) aadd( aDecTOWhat, { 125, "7D", "}", "%7D", "111 1101", "175" } ) aadd( aDecTOWhat, { 126, "7E", "~", "%7E", "111 1110", "176" } ) for i = 1 to len( aDecTOWhat ) if aDecTOWhat[ i ][ 1 ] = Oque return aDecTOWhat[ i ][ ParaQue ] endif next RETURN "" Obg. abs. Quote Link to comment Share on other sites More sharing options...
Theotokos Posted February 24, 2014 Report Share Posted February 24, 2014 Esta função que esta convertendo as letras acetuadas em sem acentuação str_replace( _oque_ ) faça um teste comentando esta função : function str_replace( _oque_ ) * LOCAL cCAcentos := 'àâêôûãõáéíóúçüîäëïöèìòùÀÂÊÔÛÃÕÁÉÍÓÚÇÜÎÄËÏÖÈÌÒÙÆ' * LOCAL cSAcentos := 'aaeouaoaeioucuiaeioeiouAAEOUAOAEIOUCUIAEIOEIOUA' * for i := 1 to Len( cCAcentos ) * _oque_ := StrTran( _oque_, SubStr( cCAcentos, i, 1), SubStr( cSAcentos, i, 1) ) * next RETURN _oque_ Quote Link to comment Share on other sites More sharing options...
Theotokos Posted February 24, 2014 Report Share Posted February 24, 2014 Esta função que esta convertendo as letras acetuadas em sem acentuação str_replace( _oque_ ) faça um teste comentando esta função : function str_replace( _oque_ ) * LOCAL cCAcentos := 'àâêôûãõáéíóúçüîäëïöèìòùÀÂÊÔÛÃÕÁÉÍÓÚÇÜÎÄËÏÖÈÌÒÙÆ' * LOCAL cSAcentos := 'aaeouaoaeioucuiaeioeiouAAEOUAOAEIOUCUIAEIOEIOUA' * for i := 1 to Len( cCAcentos ) * _oque_ := StrTran( _oque_, SubStr( cCAcentos, i, 1), SubStr( cSAcentos, i, 1) ) * next RETURN _oque_ desculpa, depois olhei direito e vi que nem esta usando esta função, ela converte acentuação em sem acentuação... pode ajudar da mesma forma... Quote Link to comment Share on other sites More sharing options...
alexavel Posted February 24, 2014 Report Share Posted February 24, 2014 Experimente usar o AnsiUpper(cTexto) kapiaba 1 Quote Link to comment Share on other sites More sharing options...
kapiaba Posted February 24, 2014 Author Report Share Posted February 24, 2014 Experimente usar o AnsiUpper(cTexto) Alex, este comando transforma em MAISCULA, mas não retorna a acentuação. ? AnsiUpper( txt_resultado ) // maisuculas ? AnsiToOem( txt_resultado ) // não funciona ? OemToAnsi( txt_resultado ) // não funciona Obg. kapiaba 1 Quote Link to comment Share on other sites More sharing options...
kapiaba Posted February 25, 2014 Author Report Share Posted February 25, 2014 // Ideia Original By Arthur Silvestre - 25/02/2014 #include "FiveWin.ch" FUNCTION Busca_CEP( cCep ) LOCAL oHttp, cXML LOCAL cRes, cResTxt, cUf, cCidade, cTipo, cEnde, cBairro DEFAULT cCep:="05171340" cCep := StrTran(AllTrim(cCep),"-","") IF Len(cCep) != 8 MsgAlert("Cep Inválido","Atenção") RETURN .F. ENDIF IF .NOT. IsInternet() return .t. endif oHttp:= TIpClientHttp():new( "http://cep.republicavirtual.com.br/web_cep.php?cep="+cCep+"&formato=xml" ) IF ! oHttp:open() MsgAlert( "Servidor de CEPS , Fora do Ar ou Congestionado , Erro na conexão: "+ oHttp:lastErrorMessage() ) RETURN .T. ENDIF cXML := oHttp:readAll() oHttp:close() IF Empty(cXML) MsgAlert("Ocorreu um Erro Inesperado , Envie outro CEP","Tente Novamente") RETURN .F. ENDIF cRes := Substr( cXML, At("<resultado>",cXML)+11, At("</resultado>",cXML)-At("<resultado>",cXML)-11 ) cResTxt := Substr( cXML, At("<resultado_txt>",cXML)+15, At("</resultado_txt>",cXML)-At("<resultado_txt>",cXML)-15 ) cUf := Substr( cXML, At("<uf>",cXML)+4, At("</uf>",cXML)-At("<uf>",cXML)-4 ) cCidade := Substr( cXML, At("<cidade>",cXML)+8, At("</cidade>",cXML)-At("<cidade>",cXML)-8 ) cTipo := Substr( cXML, At("<tipo_logradouro>",cXML)+17, At("</tipo_logradouro>",cXML)-At("<tipo_logradouro>",cXML)-17 ) cEnde := Substr( cXML, At("<logradouro>",cXML)+12, At("</logradouro>",cXML)-At("<logradouro>",cXML)-12 ) cBairro := Substr( cXML, At("<bairro>",cXML)+8, At("</bairro>",cXML)-At("<bairro>",cXML)-8 ) ? ANSIUPPER( cEnde ) RETURN .T. //-> Funcao para checar a Internet FUNCTION IsInternet() LOCAL cIp, cVret := .F. WsaStartUp() // conforme pessoal do forum, e para chamar duas vezes ?? cIp := GETHOSTBYNAME( "microsoft.com" ) cIp := GETHOSTBYNAME( "microsoft.com" ) WsaCleanUp() // Seguranca caso o provedor da microsoft esteja fora do ar... IF ( cIp <= "0.0.0.0" ) WsaStartUp() cIp := GetHostByName( "google.com" ) cIp := GetHostByName( "google.com" ) WsaCleanUp() ENDIF RETURN( cIp<>"0.0.0.0" ) //------------------------------------------------------------// Quote Link to comment Share on other sites More sharing options...
Ariston Santos Posted February 25, 2014 Report Share Posted February 25, 2014 MUITO BOA. Só faltou uma função para realizar a conversão de caracteres de endereços URL. A DecTOWhat( Oque, ParaQue ) serviria, mas está incompleta. Alguém tem uma que faça isso? Com uma função assim é possível converter "cidade=Macap%E1" para "cidade=Macapá", ou "logradouro=D%E9cima+Nona" para "logradouro=Décima+Nona", a assim por diante. Neste link tem a tabela de caracteres em endereços URL completa, que serve de exemplo do que converter: http://www.e-planning.net/pt/suporte/codificacao_caracteres_em_url.html Se alguém se prontificar a fazer, favor postar aqui. Quote Link to comment Share on other sites More sharing options...
kapiaba Posted February 25, 2014 Author Report Share Posted February 25, 2014 Ariston, veja se ajuda: http://www.pctoledo.com.br/forum/viewtopic.php?f=43&t=10331&p=74994&hilit=DecTOWhat#p74994 http://www.pctoledo.com.br/forum/viewtopic.php?f=1&t=11988&start=0&hilit=DecTOWhat http://www.pctoledo.com.br/forum/viewtopic.php?f=43&t=10331&start=0&hilit=DecTOWhat Eu acho que você consegue tranquilo... abs, Quote Link to comment Share on other sites More sharing options...
kapiaba Posted February 25, 2014 Author Report Share Posted February 25, 2014 Agora em Classe: http://www.pctoledo.com.br/forum/viewtopic.php?f=4&t=12408&p=86952#p86952. #include "FiveWin.ch" #include "hbxml.ch" #include "hbclass.ch" function main() local cep := BuscaCEP():New("05171340") ? cep:GetTipo() + " " + cep:GetLogradouro() ? cep:GetBairro() ? cep:GetCidade() ? cep:GetUF() return nil class BuscaCEP private: data cLogradouro init "" data cBairro init "" data cCidade init "" data cTipo init "" data cCep init "" data cUF init "" data cSrvResp init "" public: method new(cep) constructor method busca(cep) inline method GetLogradouro() return ::cLogradouro endmethod inline method GetBairro() return ::cBairro endmethod inline method GetCidade() return ::cCidade endmethod inline method GetTipo() return ::cTipo endmethod inline method GetUF() return ::cUF endmethod inline method GetSrvSts() return ::cSrvResp endmethod endclass method new(cep) class BuscaCEP if cep != nil ::cCep := cep else ::cCep := "99999999" endif ::busca() return self method busca(cep) class BuscaCEP local oHttp, cXML local oDoc, oIterator if cep != nil ::cCep := cep endif oHttp:= TIpClientHttp():new( "http://cep.republicavirtual.com.br/web_cep.php?cep="+::cCep+"&formato=xml" ) oHttp:open() cXML := oHttp:readAll() oHttp:close() oDoc := TXmlDocument():New(cXML, HBXML_STYLE_NOESCAPE) if oDoc:nError != HBXML_ERROR_NONE ? "Erro ao ler o XML dos correios" endif oIterator := TXmlIterator():New( oDoc:findfirst( "webservicecep" ) ) if oIterator:Next():cData == "1" ::cSrvResp := oIterator:Next():cData ::cUF := oIterator:Next():cData ::cCidade := oIterator:Next():cData ::cBairro := oIterator:Next():cData ::cTipo := oIterator:Next():cData ::cLogradouro := oIterator:Next():cData else ? "ERRO :(" endif return nil // FIM DO PROGRAMA Quote Link to comment Share on other sites More sharing options...
Theotokos Posted February 25, 2014 Report Share Posted February 25, 2014 Alguem testou usando a classe? fui testar não deu certo, quando executa a rotina diz que não existe BuscaCEP()... o que será que fiz de errado? Quote Link to comment Share on other sites More sharing options...
kapiaba Posted February 26, 2014 Author Report Share Posted February 26, 2014 Testei com FWH 13.06 e xHarbour 1.2.1 Borland BCC582 e funciona normal. Gera algum error.log? Mais uma rotina, veja se consegue passar a Five e poste para nós porfa: http://pctoledo.com.br/forum/viewtopic.php?f=43&t=13329 abs Quote Link to comment Share on other sites More sharing options...
kapiaba Posted February 26, 2014 Author Report Share Posted February 26, 2014 // By Vagner Wirts - 26/02/2014 - Todos os direitos reservados. #Include "FiveWin.Ch" Function Main() cCep := "09551030" oPg := CreateObject("Microsoft.XMLHTTP") xComando:="http://www.jpatecnologia.com.br/cep.asp?cep=" + cCep oPg:Open("GET",xComando,.f.) oPg:Send() cBuf := oPg:responseBody cLog := PegaParteTexto(cBuf,"<logradouro>") cLog1 := AllTrim(Left(cLog,At(" ",cLog))) cLog2 := AllTrim(StrTran(cLog,cLog1,"")) cBair := PegaParteTexto(cBuf,"<bairro>") cCida := PegaParteTexto(cBuf,"<Cidade>") cEsta := PegaParteTexto(cBuf,"<uf>") If Empty(cLog) MsgInfo("CEP não encontrado, favor verificar....","Busca") Else ? cLog,cLog1,cLog2,cBair,cCida,cEsta Endif Return Nil /********************************************************* * Função : PegaParteTexto() - Funcao para Pegar partes de um texto * Data : 14/03/2012 - 10:28:12 Por : Vagner Wirts * Revisado em : 14/03/2012 - 10:28:15 Por : Vagner Wirts * Parâmetros : * cTexto - Texto para a Leitura * cStri1 - String Inicial para pegar * cStri2 - String Final para Pegar * lDesca - Se descarta o parametro "cTexto" da String no retorno (Default .T.) * lArray - Se Irá Retornar um array contendo as posições (Default .F.) * Retorno : * cRetor - Texto para Retornar **********************************************************/ Function PegaParteTexto(cTexto,cStri1,cStri2,lDesca,lArray) Local nPos1 := 0,nPos2 := 0 Local cRetor := "" Default cStri2 := Stuff(cStri1,2,0,"/") Default lDesca := .T. Default lArray := .F. If Upper(cStri1) $ Upper(cTexto) nPos1 := At(Upper(cStri1),Upper(cTexto))+Iif(lDesca,Len(cStri1),-1) nPos2 := At(Upper(cStri2),Upper(cTexto))+Iif(lDesca,0,Len(cStri2)) If nPos1 == 0 ; cRetor := "" ElseIf nPos2 == 0 ; cRetor := SubStr(cTexto,nPos1) Else ; cRetor := SubStr(cTexto,nPos1,nPos2-nPos1) Endif Endif If lArray ; cRetor := {cRetor,nPos1,nPos2} ; Endif Return( cRetor ) // FIM DO PROGRAMA Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.