Skip to content

Commit 4078c1c

Browse files
authored
Updated sample, replaced deprecated code
1 parent d5485a7 commit 4078c1c

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

java/Search/BingVideoSearchv7.java

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,73 @@
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.net.*;
55
import java.util.*;
66
import java.io.*;
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

9-
/*
13+
/**
14+
* This sample uses the Bing Video Search API with a text query, which returns videos from a web search.
15+
*
1016
* Gson: https://github.com/google/gson
1117
* Maven info:
1218
* groupId: com.google.code.gson
1319
* artifactId: gson
14-
* version: 2.8.1
20+
* version: x.x.x
1521
*
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 (BingVideoSearch.java), you can compile and run this program at
18-
* the command line as follows.
22+
* Add your Bing Video Search key and endpoint to your environment variables.
1923
*
20-
* javac BingVideoSearch.java -classpath .;gson-2.8.1.jar -encoding UTF-8
21-
* java -cp .;gson-2.8.1.jar BingVideoSearch
24+
* Compile and run from the command line:
25+
* javac BingVideoSearch.java -cp .;gson-2.8.6.jar -encoding UTF-8
26+
* java -cp .;gson-2.8.6.jar BingVideoSearch
2227
*/
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-
2828
public class BingVideoSearch {
2929

30-
// ***********************************************
31-
// *** Update or verify the following values. ***
32-
// **********************************************
33-
3430
// Add your Bing Search V7 subscription key to your environment variables.
3531
static String subscriptionKey = System.getenv("BING_SEARCH_V7_SUBSCRIPTION_KEY");
3632

3733
// Add your Bing Search V7 endpoint to your environment variables.
38-
static String host = System.getenv("BING_SEARCH_V7_ENDPOINT");
39-
static String path = "/bing/v7.0/videos/search";
34+
static String endpoint = System.getenv("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/videos/search";
4035

4136
static String searchTerm = "kittens";
4237

38+
public static void main(String[] args) {
39+
try {
40+
System.out.println("Searching the Web for: " + searchTerm);
41+
42+
SearchResults result = SearchVideos(searchTerm);
43+
44+
System.out.println("\nRelevant HTTP Headers:\n");
45+
for (String header : result.relevantHeaders.keySet())
46+
System.out.println(header + ": " + result.relevantHeaders.get(header));
47+
48+
System.out.println("\nJSON Response:\n");
49+
System.out.println(prettify(result.jsonResponse));
50+
} catch (Exception e) {
51+
e.printStackTrace(System.out);
52+
System.exit(1);
53+
}
54+
}
55+
4356
public static SearchResults SearchVideos (String searchQuery) throws Exception {
44-
// construct URL of search request (endpoint + query string)
45-
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchQuery, "UTF-8"));
57+
// Construct URL of search request (endpoint + query string)
58+
URL url = new URL(endpoint + "?q=" + URLEncoder.encode(searchQuery, "UTF-8"));
4659
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
4760
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
4861

49-
// receive JSON body
62+
// Receive JSON body
5063
InputStream stream = connection.getInputStream();
51-
String response = new Scanner(stream).useDelimiter("\\A").next();
64+
Scanner scanner = new Scanner(stream);
65+
String response = scanner.useDelimiter("\\A").next();
5266

53-
// construct result object for return
67+
// Construct result object for return
5468
SearchResults results = new SearchResults(new HashMap<String, String>(), response);
5569

56-
// extract Bing-related HTTP headers
70+
// Extract Bing-related HTTP headers
5771
Map<String, List<String>> headers = connection.getHeaderFields();
5872
for (String header : headers.keySet()) {
5973
if (header == null) continue; // may have null key
@@ -63,35 +77,17 @@ public static SearchResults SearchVideos (String searchQuery) throws Exception {
6377
}
6478

6579
stream.close();
80+
scanner.close();
81+
6682
return results;
6783
}
6884

69-
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
85+
// Pretty-printer for JSON; uses GSON parser to parse and re-serialize
7086
public static String prettify(String json_text) {
71-
JsonParser parser = new JsonParser();
72-
JsonObject json = parser.parse(json_text).getAsJsonObject();
87+
JsonObject json = JsonParser.parseString(json_text).getAsJsonObject();
7388
Gson gson = new GsonBuilder().setPrettyPrinting().create();
7489
return gson.toJson(json);
7590
}
76-
77-
public static void main (String[] args) {
78-
try {
79-
System.out.println("Searching the Web for: " + searchTerm);
80-
81-
SearchResults result = SearchVideos(searchTerm);
82-
83-
System.out.println("\nRelevant HTTP Headers:\n");
84-
for (String header : result.relevantHeaders.keySet())
85-
System.out.println(header + ": " + result.relevantHeaders.get(header));
86-
87-
System.out.println("\nJSON Response:\n");
88-
System.out.println(prettify(result.jsonResponse));
89-
}
90-
catch (Exception e) {
91-
e.printStackTrace(System.out);
92-
System.exit(1);
93-
}
94-
}
9591
}
9692

9793
// Container class for search results encapsulates relevant headers and JSON data

0 commit comments

Comments
 (0)