Skip to content

Commit 72d2d75

Browse files
authored
Updated sample
1 parent 9867aba commit 72d2d75

File tree

1 file changed

+49
-34
lines changed

1 file changed

+49
-34
lines changed
Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,67 @@
1-
/*Copyright (c) Microsoft Corporation. All rights reserved.
2-
Licensed under the MIT License.*/
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
33

44
import java.io.*;
55
import java.net.*;
66
import javax.net.ssl.HttpsURLConnection;
7+
import org.json.*;
78

8-
public class HelloWorld {
9+
/**
10+
* This sample uses the Bing Spell Check v7 API to check spelling of a sentence.
11+
*
12+
* Include library (for pretty print), add jar to a lib folder:
13+
* https://github.com/stleary/JSON-java
14+
*
15+
* Add your subscription key and endpoint to your environment variables.
16+
*
17+
* Build/run from command line:
18+
* javac BingSpellCheckv7.java -cp .;lib\*
19+
* java -cp .;lib\* BingSpellCheckv7
20+
*/
921

10-
static String host = "https://api.cognitive.microsoft.com";
11-
static String path = "/bing/v7.0/spellcheck";
22+
public class BingSpellCheckv7 {
23+
24+
// Or using the generic endpoint https://api.cognitive.microsoft.com is OK too.
25+
static String endpoint = System.getenv("BING_SPELL_CHECK_ENDPOINT") + "/bing/v7.0/spellcheck";
1226

1327
// NOTE: Replace this example key with a valid subscription key.
14-
static String key = "ENTER KEY HERE";
28+
static String subscriptionKey = System.getenv("BING_SPELL_CHECK_SUBSCRIPTION_KEY");
1529

1630
static String mkt = "en-US";
1731
static String mode = "proof";
1832
static String text = "Hollo, wrld!";
1933

20-
public static void check () throws Exception {
21-
String params = "?mkt=" + mkt + "&mode=" + mode;
22-
URL url = new URL(host + path + params);
23-
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
24-
connection.setRequestMethod("POST");
25-
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
26-
connection.setRequestProperty("Content-Length", "" + text.length() + 5);
27-
connection.setRequestProperty("Ocp-Apim-Subscription-Key", key);
28-
connection.setDoOutput(true);
29-
30-
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
31-
wr.writeBytes("text=" + text);
32-
wr.flush();
33-
wr.close();
34-
35-
BufferedReader in = new BufferedReader(
36-
new InputStreamReader(connection.getInputStream()));
37-
String line;
38-
while ((line = in.readLine()) != null) {
39-
System.out.println(line);
40-
}
41-
in.close();
42-
}
43-
4434
public static void main(String[] args) {
4535
try {
46-
check ();
47-
}
48-
catch (Exception e) {
49-
System.out.println (e);
36+
String params = "?mkt=" + mkt + "&mode=" + mode;
37+
URL url = new URL(endpoint + params);
38+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
39+
connection.setRequestMethod("POST");
40+
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
41+
connection.setRequestProperty("Content-Length", "" + text.length() + 5);
42+
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
43+
connection.setDoOutput(true);
44+
45+
// Optional
46+
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
47+
wr.writeBytes("text=" + text);
48+
wr.flush();
49+
wr.close();
50+
51+
// Get results
52+
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
53+
54+
String line;
55+
StringBuilder sb = new StringBuilder();
56+
while ((line = in.readLine()) != null) {
57+
sb.append(line);
58+
}
59+
// Pretty print
60+
System.out.println((new JSONObject(sb.toString())).toString(4));
61+
62+
in.close();
63+
} catch (Exception e) {
64+
System.out.println(e);
5065
}
5166
}
5267
}

0 commit comments

Comments
 (0)