java - 如何使用 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)
,但它不工作。
提前致谢。
解决方案
您需要查看 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()
需要给出一些类似的输入
推荐阅读
- python - 是否可以在 guizero 中创建多个页面或刷新页面以显示新内容?
- c# - 如何在带有 AspNet Core Identity 的 .net core 5.0 中使用 keycloak?
- amazon-web-services - 如何通过 AWS CLI 命令在 AWS ECS 中创建取消注册/非活动任务定义的修订?
- django - 与 Jenkins、Docker 相关
- python - Docker 副本:无法计算来自发件人的缓存键/错误
- lex - 如何在 Windows 的命令行上运行/编译 lex 程序
- javascript - 当我导航到另一条路线时,如何不删除启用 PIP 的视频组件?使用 Aurelia.js
- typescript - 在 Formik 中使用 innerRef 访问 formik 状态,如错误
- nginx - 适用于 Amazon Linux 第 2 版 (Karoo) 和 nginx 的 certbot-auto (2021)
- svg - Vue Konva,动态创建阶段