使用锚标记 link 到 Java 中的外部 URL
Using anchor tag to link to an external URL in Java
我正在尝试从 Android 应用发送 Telegram 消息。我希望该消息包含一个 hyperlink,所以我使用了 parse_mode=html
参数,但锚标记有问题。似乎 java 将我的 URL 视为本地路径。
这是代码:
String location = "http://www.google.com";
urlString = String.format("https://api.telegram.org/bot<bot_token>/sendMessage?chat_id=<chat_id>&parse_mode=html&text=<a href=%s>Location</a>", location);
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
StringBuilder sb = new StringBuilder();
InputStream is = new BufferedInputStream(conn.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
这是错误:
java.io.FileNotFoundException:
https://api.telegram.org/bot<bot_token>/sendMessage?chat_id=<chat_id>&parse_mode=html&text=<a href=http://google.com>Location</a>
我应该如何编写此消息,以便 href link 将被视为外部 URL?
错误java.io.FileNotFoundException
并不意味着它被视为本地路径。
是HTTP 404 File Not Found。它是服务器对您的 HTTP 请求的响应。
似乎首先提供适当的<bot_token>
和<chat_id>
是必要的。其次,在使用它实例化 URL
对象之前,您应该 urlencode 该字符串。
String encodedUrlString = URLEncoder.encode(urlString, "UTF-8");
URL url = new URL(encodedUrlString);
我正在尝试从 Android 应用发送 Telegram 消息。我希望该消息包含一个 hyperlink,所以我使用了 parse_mode=html
参数,但锚标记有问题。似乎 java 将我的 URL 视为本地路径。
这是代码:
String location = "http://www.google.com";
urlString = String.format("https://api.telegram.org/bot<bot_token>/sendMessage?chat_id=<chat_id>&parse_mode=html&text=<a href=%s>Location</a>", location);
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
StringBuilder sb = new StringBuilder();
InputStream is = new BufferedInputStream(conn.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
这是错误:
java.io.FileNotFoundException:
https://api.telegram.org/bot<bot_token>/sendMessage?chat_id=<chat_id>&parse_mode=html&text=<a href=http://google.com>Location</a>
我应该如何编写此消息,以便 href link 将被视为外部 URL?
错误java.io.FileNotFoundException
并不意味着它被视为本地路径。
是HTTP 404 File Not Found。它是服务器对您的 HTTP 请求的响应。
似乎首先提供适当的<bot_token>
和<chat_id>
是必要的。其次,在使用它实例化 URL
对象之前,您应该 urlencode 该字符串。
String encodedUrlString = URLEncoder.encode(urlString, "UTF-8");
URL url = new URL(encodedUrlString);