簡體   English   中英

使用錨標簽鏈接到 Java 中的外部 URL

[英]Using anchor tag to link to an external URL in Java

我正在嘗試從 Android 應用程序發送電報消息。 我希望該消息包含一個超鏈接,所以我使用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 鏈接視為外部 URL?

錯誤java.io.FileNotFoundException並不意味着它被視為本地路徑。

它是HTTP 404 File Not Found 它是服務器對您的 HTTP 請求的響應。

似乎首先需要提供正確的<bot_token><chat_id> 其次,您應該在用它實例化URL object 之前對該字符串進行urlencode

String encodedUrlString = URLEncoder.encode(urlString, "UTF-8");
URL url = new URL(encodedUrlString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM