A
A
Aylix2019-11-08 11:29:40
Java
Aylix, 2019-11-08 11:29:40

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

1 answer(s)
A
Aylix, 2019-11-08
@Aylix

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 question

Ask a Question

731 491 924 answers to any question