俄语导致 IOException

Russian language causing IOException

我正在使用 API https://dictionaryapi.dev/ 为 Google 字典制作机器人。它应该显示不同语言的单词定义。该机器人适用于英语和西班牙语。但是每次我输入俄语时都会出现 IOException。可能是什么原因造成的?

这是我的机器人 class。 Bot.java:

public void onUpdateReceived(Update update) {
        //Model model = new Model();
        WordModel wordModel = new WordModel();
        Message message = update.getMessage();
        if (message != null && message.hasText())
        {
            switch (message.getText()) {
                case "/english":
                    DictionaryEntry.setLanguage("en");
                    sendMsg(message, DictionaryEntry.getLanguage());
                    break;
                case "/russian":
                    DictionaryEntry.setLanguage("ru");
                    sendMsg(message, DictionaryEntry.getLanguage());
                    break;
                case "/spanish":
                    DictionaryEntry.setLanguage("es");
                    sendMsg(message, DictionaryEntry.getLanguage());
                    break;
                default:
                    if (!message.getText().contains("/")) {
                        try {
                            sendMsgs(message, DictionaryEntry.getWords(message.getText(), wordModel));
                        } catch (IOException e) {
                            sendMsg(message, "Не найдено");
                            sendMsg(message, DictionaryEntry.getUrl().toString());
                        }
                    }
                    break;
            }
        }
    }

public void sendMsg(Message message, String text) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setChatId(message.getChatId());
        sendMessage.setReplyToMessageId(message.getMessageId());
        sendMessage.setText(text);
        try {
            //setButtons(sendMessage);
            execute(sendMessage);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

public void sendMsgs(Message message, List<String> words) {
        for (int i = 0; i < words.size(); i++) {
            SendMessage sendMessage = new SendMessage();
            sendMessage.setChatId(message.getChatId());
            sendMessage.setReplyToMessageId(message.getMessageId());
            sendMessage.setText(words.get(i));
            try {
                //setButtons(sendMessage);
                execute(sendMessage);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

这是我的 DictionaryEntry class,我在其中处理从 URL 获得的 JSON 字符串。 DictionaryEntry.java:

private static String language = "ru";

    public static String getLanguage() {
        return language;
    }

    public static void setLanguage(String language) {
        DictionaryEntry.language = language;
    }

    public static URL getUrl() {
        return url;
    }

    private static URL url;

public static List<String> getWords(String message, WordModel wordModel) throws IOException {
        url = new URL("https://api.dictionaryapi.dev/api/v2/entries/"
                + DictionaryEntry.getLanguage() + "/" + message);

        Scanner in = new Scanner((InputStream) url.getContent());
        String result = "";

        while (in.hasNext())
        {
            result += in.nextLine();
        }

        String result2 = result.replaceAll("\"", "\\"");

        List<WordModel> models = new ArrayList<>();
        List<String> results = new ArrayList<>();
        int count = 0;

        try {
            JSONArray mainArray = new JSONArray(result2);

            for (int i = 0; i < mainArray.length(); i++) {
                JSONObject wordEntry = mainArray.getJSONObject(i);
                String wordName = wordEntry.getString("word");
                wordModel.setWord(wordName);

                JSONArray meaningsArray = wordEntry.getJSONArray("meanings");
                for (int j = 0; j < meaningsArray.length(); j++) {
                    JSONObject meaningEntry = meaningsArray.getJSONObject(j);
                    JSONArray definitionsArray = meaningEntry.getJSONArray("definitions");
                    for (int k = 0; k < definitionsArray.length(); k++) {
                        //count++;
                        JSONObject definitionEntry = definitionsArray.getJSONObject(k);
                        String definition = definitionEntry.getString("definition");
                        wordModel.setDefinition(definition);

                        if (definitionEntry.has("example")) {
                            count++;
                            String example = definitionEntry.getString("example");
                            wordModel.setExample(example);
                        }
                        else {
                            wordModel.setExample("No examples found");
                        }

                        models.add(wordModel);
                        results.add("Word: " + wordModel.getWord() + "\n\n" +
                                "Definition: " + wordModel.getDefinition() + "\n\n" +
                                "Example: " + wordModel.getExample());
                    }
                }
            }

            /*result = "Word: " + wordModel.getWord() + "\n\n" +
                     "Definition: " + wordModel.getDefinition() + "\n\n" +
                     "Example: " + wordModel.getExample();*/
        } catch (JSONException e) {
            count = 50;
        }

        results.add(url.toString());

        return results;
    }

这是堆栈跟踪:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.dictionaryapi.dev/api/v2/entries/ru/мышь
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1932)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1528)
    at java.base/java.net.URLConnection.getContent(URLConnection.java:749)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:404)
    at java.base/java.net.URL.getContent(URL.java:1181)
    at DictionaryEntry.getWords(DictionaryEntry.java:73)
    at Bot.onUpdateReceived(Bot.java:91)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:321)

检查您的环境文件是否为 utf-8u 格式,因为可能不支持西里尔字母

+ URLEncoder.encode(message, "UTF-8")

您正在发送 HTTP 请求。 URL 应该适合使用的编码。

使用权利还有更多方面charset/encoding,但这可能已经足够了。该站点可能使用 UTF-8,因为它可以处理整个 Unicode 范围。

400 是错误请求。


url = new URL("https://api.dictionaryapi.dev/api/v2/entries/"
            + DictionaryEntry.getLanguage() + "/"
            + URLEncoder.encode(message, "UTF-8"));

    Scanner in = new Scanner((InputStream) url.getContent(),
        StandardCharsets.UTF_8);
    StringBuilder sb = new StringBuilder();
    while (in.hasNextLine())
    {
        sb.append(in.nextLine()).append('\n');
    }
    String result = sb.toString();