sto cercando di recuperare i dati da http://api.freebase.com/api/trans/raw/m/0h47Java codifica UTF-8 non impostato su URLConnection
Come si può vedere nel testo ci sono canta come questo: /ælˈdʒɪəriə/
.
Quando provo ad ottenere fonte dalla pagina ricevo testo con canta come ú
ecc
Finora ho provato con il seguente codice:
urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
Che cosa sto facendo di sbagliato?
Tutto il mio codice:
URL url = null;
URLConnection urlConn = null;
DataInputStream input = null;
try {
url = new URL("http://api.freebase.com/api/trans/raw/m/0h47");
} catch (MalformedURLException e) {e.printStackTrace();}
try {
urlConn = url.openConnection();
} catch (IOException e) { e.printStackTrace(); }
urlConn.setRequestProperty("Accept-Charset", "UTF-8");
urlConn.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
StringBuffer strBseznam = new StringBuffer();
if (strBseznam.length() > 0)
strBseznam.deleteCharAt(strBseznam.length() - 1);
try {
input = new DataInputStream(urlConn.getInputStream());
} catch (IOException e) { e.printStackTrace(); }
String str = "";
StringBuffer strB = new StringBuffer();
strB.setLength(0);
try {
while (null != ((str = input.readLine())))
{
strB.append(str);
}
input.close();
} catch (IOException e) { e.printStackTrace(); }
Sembra l'unica cosa che funzionerà. Grazie. – Ales
Ho lo stesso problema, per favore aiuto. m.appendTail (m); questa linea mi errore. –
@ ersyn61: Siamo spiacenti, corretto; avrebbe dovuto essere 'm.appendTail (sb);'. –