URLConnection, getInputStream e caracter especial

1 resposta
H

Tarde pessoal do GUJ, tenho um método que chama uma página JSF onde é retornada uma mensagem, porem caracter especial que a página retorna (e que aparece normal no navegador) não é reconhecido aqui pelo Java.

//URL url = new URL("http://gate.integrada.coop.br:8080/MensagemServer/home/home.jsf");  
		URL url = new URL("http://localhost:8080/MensagemServer/home/home.jsf");
		URLConnection conn = url.openConnection();  
		
		// Get the response  
		DataInputStream rd = new DataInputStream(conn.getInputStream());  
		String line;  
		
		while ((line = rd.readLine()) != null) {  
			System.out.println((line));
		}  
		
		rd.close();

Ele retorna assim:

...depende de voc & ecirc; escolher como que...
(coloque espaços senão ele printa o ê)

O que tenho que fazer para ele reconhecer...? Alguma conversão??

Obrigado.

1 Resposta

H

Pessoal to repondendo aqui para alguem que tiver o mesmo problema… achei uma solução, não sei se é a melhor mas lá vai:

public String unescapeHTML(String schtuff) { String[][] asciiArray = { { "&lt;", "<" }, { "&gt;", ">" }, { "&amp;", "&" }, { "&quot;", "\"" }, { "&agrave;", "à" }, { "&Agrave;", "À" }, { "&acirc;", "â" }, { "&auml;", "ä" }, { "&Auml;", "Ä" }, { "&Acirc;", "Â" }, { "&aring;", "å" }, { "&Aring;", "Å" }, { "&aelig;", "æ" }, { "&AElig;", "Æ" }, { "&ccedil;", "ç" }, { "&Ccedil;", "Ç" }, { "&eacute;", "é" }, { "&Eacute;", "É" }, { "&egrave;", "è" }, { "&Egrave;", "È" }, { "&ecirc;", "ê" }, { "&Ecirc;", "Ê" }, { "&euml;", "ë" }, { "&Euml;", "Ë" }, { "&iuml;", "ï" }, { "&Iuml;", "Ï" }, { "&ocirc;", "ô" }, { "&Ocirc;", "Ô" }, { "&ouml;", "ö" }, { "&Ouml;", "Ö" }, { "&oslash;", "ø" }, { "&Oslash;", "Ø" }, { "&szlig;", "ß" }, { "&ugrave;", "ù" }, { "&Ugrave;", "Ù" }, { "&ucirc;", "û" }, { "&Ucirc;", "Û" }, { "&uuml;", "ü" }, { "&Uuml;", "Ü" }, { "&nbsp;", " " }, { "&reg;", "\u00a9" }, { "&copy;", "\u00ae" }, { "&euro;", "\u20a0" } }; int flag = -1, ampIndex = -1; if (schtuff.indexOf("&") >= 0) { if (schtuff.indexOf(";") > schtuff.indexOf("&")) { String asciiStuff = schtuff.substring(schtuff.indexOf("&"), schtuff.indexOf(";") + 1); ampIndex = schtuff.indexOf("&"); while (asciiStuff.substring(1, asciiStuff.length()) .indexOf("&") >= 0) { ampIndex = schtuff.indexOf("&", ampIndex + 1); asciiStuff = asciiStuff.substring(1, asciiStuff.length()) .trim(); } for (int j = 0; j < asciiArray.length; j++) { if (asciiArray[j][0].equals(asciiStuff)) { flag = j; break; } } if (flag >= 0) { schtuff = schtuff.substring(0, ampIndex) + asciiArray[flag][1] + schtuff.substring(schtuff.indexOf(";") + 1, schtuff.length()); return unescapeHTML(schtuff); // RECURSIVE } } return schtuff; } else { return schtuff.trim(); } }

Funcionou perfeitamente.
Abraços

Criado 17 de março de 2008
Ultima resposta 17 de mar. de 2008
Respostas 1
Participantes 1