我想在 Android 应用程序中实现必应拼写检查 API。 为了检查它是否起作用,我从 Microsoft 教程中复制了代码示例并将其改编为我的应用程序。 看起来请求运行良好,根据响应,我的文本是正确的。但是文字不正确... 有人知道这个问题或合适的解决方案吗?
如果我的代码有错误:
protected void requestWebService2(String key) throws IOException {
final String host = "https://api.cognitive.microsoft.com";
final String path = "/bing/v7.0/spellcheck";
final String mkt = "en-US";
final String mode = "proof";
final String text = "Hollo, wrld!";
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(host+path);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", key);
connection.setDoOutput(true);
String encoded_text = URLEncoder.encode (text, "UTF-8");
String params = "?"+mkt+"&"+mode+"&"+"text="+encoded_text;
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(params);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
Log.i("...", line);
}
in.close();
}catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
以及来自控制台的相应条目:
11-04 16:33:17.310 2662-3904/myapps.webaccess I/...: {"_type": "SpellCheck", "flaggedTokens": []}
提前致谢!!!
最佳答案
所提供的代码示例中似乎存在一个小错误。您需要将 params 字符串修改为:
String params = "?mkt=" + mkt + "&" + "mode=" + mode + "&" + "text=" + encoded_text;
上面的代码中缺少 mkt
和 mode
。
关于android - 使用必应拼写检查 API 的空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47112948/