Answer the question
In order to leave comments, you need to log in
How to correctly transfer the modified string to telegram?
I send a message to telegrams, a regular string like "Hello world" is sent, a string like Array[i] + "hello" is not
String urlString = "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s";
String apiToken = "123example";
String chatId = "@example";
String text = arrayGames[i];
urlString = String.format(urlString, apiToken, chatId, text);
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
e.printStackTrace();
}
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);
}
Answer the question
In order to leave comments, you need to log in
the problem was that there were spaces in the array. It was not possible to encode everything at once - java.net.MalformedURLException: no protocol , so the solution is to encode the elements separately
urlString = String.format(urlString,
URLEncoder.encode(apiToken, "UTF-8"),
URLEncoder.encode(chatId, "UTF-8"),
URLEncoder.encode(text, "UTF-8"));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question