首页 > 解决方案 > 如何使用 Rapid API 中的 LanguageTool API 检查 Java 中的字符串?

问题描述

这是为我创建的代码 Rapid API。

HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://dnaber-languagetool.p.rapidapi.com/v2/check"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-rapidapi-host", "dnaber-languagetool.p.rapidapi.com")
        .header("x-rapidapi-key", "[redacted to prevent abuse]")
        .method("POST", HttpRequest.BodyPublishers.ofString("text=This%20is%20a%20error.&language=en-US"))
        .build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

这是它的输出:

{"software":{"name":"LanguageTool","version":"5.5-SNAPSHOT","buildDate":"2021-09-17 18:41:41 +0000","apiVersion":1,"premium":false,"premiumHint":"You might be missing errors only the Premium version can find. Contact us at support<at>languagetoolplus.com.","status":""},"warnings":{"incompleteResults":false},"language":{"name":"English (US)","code":"en-US","detectedLanguage":{"name":"English (US)","code":"en-US","confidence":0.528}},"matches":[{"message":"Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.","shortMessage":"Wrong article","replacements":[{"value":"an"}],"offset":8,"length":1,"context":{"text":"This is a error.","offset":8,"length":1},"sentence":"This is a error.","type":{"typeName":"Other"},"rule":{"id":"EN_A_VS_AN","description":"Use of 'a' vs. 'an'","issueType":"misspelling","category":{"id":"MISC","name":"Miscellaneous"}},"ignoreForIncompleteSentence":false,"contextForSureMatch":1}]}

它似乎正在工作,但我该如何让它检查我的字符串而不是他们给我的字符串?

就像我有String myString = "Hello World. Hello World. Hello World."并且我想检查这个一样,我会将字符串放在他们提供给我的代码中的什么位置?

我试过做.method(myString),但它不工作。

提前致谢。

标签: javastringlanguagetool

解决方案


您需要查看 API 文档,但似乎您需要更新请求正文

我不熟悉这个http客户端类,但无论是在方法

String x = "This is a error.";
... 
.method("POST", HttpRequest.BodyPublishers.ofString("text=" + URLEncoder.encode(x, StandardCharsets.UTF_8.toString()) +"&language=en-US")
.build();

或者HttpResponse.BodyHandlers.ofString()需要给出一些类似的输入


推荐阅读