我开发了一个小的 Java Swing 应用程序,在 Ubuntu Gnome 14.04 中使用 Netbeans 使用他们的开发人员 API 来查找魔兽世界中的角色统计信息。除了调用在默认浏览器中打开指向指定角色配置文件的链接的方法的按钮之外,一切都按我的预期工作。在 Ubuntu 上,我在 URL 中使用的字符串正确呈现,但在 Windows 上却没有。如果我使用为 JVM 指定 UTF-8 编码的批处理文件运行应用程序(在 Windows 中),则不会出现此问题。当直接从 .jar 文件运行它时,尽管我尝试将所有字符串编码为 UTF-8,但在 Windows 上的 URL 中,诸如“â”之类的字符会注册为“â”。如何正确格式化 URL?我假设我缺少一些东西。如果您需要查看更多代码,请告诉我。先感谢您。
ArmoryScanner_UI.java
private void openArmoryLink() {
ArmoryScanner_Backend armory = new ArmoryScanner_Backend();
String name;
String realm;
String locale;
try {
name = new String(jTextField_Name.getText().getBytes("UTF-8"));
realm = jComboBox_Realm.getSelectedItem().toString();
locale = jComboBox_Locale.getSelectedItem().toString();
if (!name.trim().isEmpty()) {
name = formatName(name);
realm = formatRealm(realm);
locale = formatLocale(locale);
armory.setPlayerInfo(name, realm, locale);
if (armory.isCharacterFound()) {
armory.setArmoryLink();
} else {
showErrorMessage("Character not found.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} else {
showErrorMessage("Please enter a character name.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
}
}
private String formatName(String name) {
String result;
try {
result = new String(name.getBytes("UTF-8"), "UTF-8");
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
result = "";
}
return result;
}
ArmoryScanner_Backend.java
public void setArmoryLink () {
try {
String baseURL = "https://us.battle.net/wow/en/character/";
String fullURL = (baseURL + realm + "/" + name + "/simple");
System.out.println("Full URL: " + fullURL);
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(new URL(fullURL).toURI());
} else {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + fullURL);
} catch (IOException e) {
System.out.println("I/O exception (non-Windows system)");
}
}
} catch (UnsupportedOperationException e) {
System.out.println("Unsupported OS");
} catch (MalformedURLException e) {
System.out.println("Bad URL");
} catch (IOException e) {
System.out.println("I/O exception.");
} catch (URISyntaxException e) {
System.out.println("Bad URI syntax");
}
}
此方法工作正常:
private void submit() {
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
ArmoryScanner_Backend armory = new ArmoryScanner_Backend();
String name;
String realm;
String locale;
try {
name = new String(jTextField_Name.getText().getBytes("UTF-8"));
realm = jComboBox_Realm.getSelectedItem().toString();
locale = jComboBox_Locale.getSelectedItem().toString();
if (!name.trim().isEmpty()) {
name = formatName(name);
realm = formatRealm(realm);
locale = formatLocale(locale);
armory.setPlayerInfo(name, realm, locale);
if (armory.isCharacterFound()) {
setStatistics(armory);
setProgression(armory);
} else {
showErrorMessage("Character not found.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} else {
showErrorMessage("Please enter a character name.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
}
this.setCursor(Cursor.getDefaultCursor());
}