如何使用 Rapid API 中的 LanguageTool API 检查 Java 中的字符串?

How do I use LanguageTool API from Rapid API to check a string in 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) 但没用。

提前致谢。

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

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

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() 需要一些类似的输入