这是快速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()
一些类似的输入