我已经制作了一个小函数来将标题作为网页,在本例中为IMDB。 但是当标题包含特殊字符时,例如åäö编码会搞定。
例如,我用“http://www.imdb.com/title/tt0290002/”调用该函数
im expecting the title "Familjen är värre (2004) - IMDb"
but i get: "Familjen är värre (2004) - IMDb"
I dont know how to get the 'ä' from 'ä'
我尝试使用imdb使用的编码:“ISO-8859-1”,但我没有什么区别。
private String getTitleForLink(String link) {
Pattern p = Pattern.compile("<title>(.*?)</title>");
try {
HttpURLConnection httpcon = (HttpURLConnection) new URL(link).openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
InputStreamReader isr = new InputStreamReader(
httpcon.getInputStream(), );
BufferedReader br = new BufferedReader(isr);
while (br.ready()) {
Matcher m = p.matcher(br.readLine());
if ((m.find() == true)) {
String title = m.group(1);
br.close();
return title.trim();
}
}
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}