如何在来自 YouTube 数据 API 的数据中正确打印 "foreign"/特殊字符?
How do I get "foreign"/special characters to print properly in data from YouTube Data API?
我使用以下代码获取视频的第一条评论:
public static void main(String[] args) throws IOException
{
String apiKey = "[redacted]";
YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtube-view-count-test").build();
System.out.println(getFirstComment(youtube,"-BAvLYgq5m0", apiKey));
}
public static String getFirstComment(YouTube youtube, String videoId, String apiKey) throws IOException
{
String commentText, author, date;
YouTube.CommentThreads.List list2 = youtube.commentThreads().list(Arrays.asList("snippet"));
list2.setVideoId(videoId);
list2.setKey(apiKey);
List<CommentThread> c = list2.execute().getItems();
Comment c2 = c.get(0).getSnippet().getTopLevelComment();
return getFormattedCommentString(c2);
}
但是有一个问题:我认为评论(显然还有用户名)是日语,当我打印到 System.out
时,我得到的只是一堆问号。 getFormattedCommentString
只是将评论信息(用户名、文本、日期)放在一个 String
中打印出来,并没有做任何奇怪的事情;我在调用此方法之前进行了测试,评论文本已经“损坏”了。那么,如果文本不是简单的 ASCII 或正在使用的任何字符集,有人知道我需要做什么来检索评论的实际文本吗?
嗯,原来问题出在我的 IDE,Eclipse 中...将工作区编码设置为 UTF-8,效果非常好。
我使用以下代码获取视频的第一条评论:
public static void main(String[] args) throws IOException
{
String apiKey = "[redacted]";
YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtube-view-count-test").build();
System.out.println(getFirstComment(youtube,"-BAvLYgq5m0", apiKey));
}
public static String getFirstComment(YouTube youtube, String videoId, String apiKey) throws IOException
{
String commentText, author, date;
YouTube.CommentThreads.List list2 = youtube.commentThreads().list(Arrays.asList("snippet"));
list2.setVideoId(videoId);
list2.setKey(apiKey);
List<CommentThread> c = list2.execute().getItems();
Comment c2 = c.get(0).getSnippet().getTopLevelComment();
return getFormattedCommentString(c2);
}
但是有一个问题:我认为评论(显然还有用户名)是日语,当我打印到 System.out
时,我得到的只是一堆问号。 getFormattedCommentString
只是将评论信息(用户名、文本、日期)放在一个 String
中打印出来,并没有做任何奇怪的事情;我在调用此方法之前进行了测试,评论文本已经“损坏”了。那么,如果文本不是简单的 ASCII 或正在使用的任何字符集,有人知道我需要做什么来检索评论的实际文本吗?
嗯,原来问题出在我的 IDE,Eclipse 中...将工作区编码设置为 UTF-8,效果非常好。