Skip to content

Commit cfb1d2a

Browse files
authored
Updated sample, replaced deprecated code
1 parent 30faa6c commit cfb1d2a

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

java/Search/BingAutosuggestv7.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,42 @@
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 java.util.*;
77
import javax.net.ssl.HttpsURLConnection;
8+
import com.google.gson.Gson;
9+
import com.google.gson.GsonBuilder;
10+
import com.google.gson.JsonObject;
11+
import com.google.gson.JsonParser;
812

913
/*
10-
* Gson: https://github.com/google/gson
14+
* Download the Gson library: https://github.com/google/gson
1115
* Maven info:
1216
* groupId: com.google.code.gson
1317
* artifactId: gson
14-
* version: 2.8.1
15-
*
16-
* Once you have compiled or downloaded gson-2.8.1.jar, assuming you have placed it in the
17-
* same folder as this file (Autosuggest.java), you can compile and run this program at
18-
* the command line as follows.
18+
* version: x.x.x
1919
*
20-
* javac Autosuggest.java -classpath .;gson-2.8.1.jar -encoding UTF-8
21-
* java -cp .;gson-2.8.1.jar Autosuggest
20+
* Place the Gson jar in the same folder as this file (BingAutosuggest.java),
21+
* then compile and run from the command line:
22+
* javac BingAutosuggest.java -classpath .;gson-2.8.6.jar -encoding UTF-8
23+
* java -cp .;gson-2.8.6.jar BingAutosuggest
2224
*/
23-
import com.google.gson.Gson;
24-
import com.google.gson.GsonBuilder;
25-
import com.google.gson.JsonObject;
26-
import com.google.gson.JsonParser;
27-
28-
public class Autosuggest {
2925

30-
// **********************************************
31-
// *** Update or verify the following values. ***
32-
// **********************************************
26+
public class BingAutosuggest {
3327

3428
// Add your Bing Autosuggest subscription key to your environment variables.
3529
static String subscriptionKey = System.getenv("BING_AUTOSUGGEST_SUBSCRIPTION_KEY");
3630

37-
static String host = System.getenv("BING_AUTOSUGGEST_ENDPOINT");
38-
static String path = "/bing/v7.0/Suggestions";
31+
static String endpoint = System.getenv("BING_AUTOSUGGEST_ENDPOINT") + "/bing/v7.0/Suggestions";
3932

4033
static String mkt = "en-US";
4134
static String query = "sail";
4235

4336
public static String get_suggestions () throws Exception {
4437
String encoded_query = URLEncoder.encode (query, "UTF-8");
4538
String params = "?mkt=" + mkt + "&q=" + encoded_query;
46-
URL url = new URL (host + path + params);
39+
URL url = new URL (endpoint + params);
4740

4841
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
4942
connection.setRequestMethod("GET");
@@ -63,8 +56,7 @@ public static String get_suggestions () throws Exception {
6356
}
6457

6558
public static String prettify (String json_text) {
66-
JsonParser parser = new JsonParser();
67-
JsonObject json = parser.parse(json_text).getAsJsonObject();
59+
JsonObject json = JsonParser.parseString(json_text).getAsJsonObject();
6860
Gson gson = new GsonBuilder().setPrettyPrinting().create();
6961
return gson.toJson(json);
7062
}

0 commit comments

Comments
 (0)