1
1
---
2
- title : Quickstart - Send a query to the API using Java - Bing Local Business Search
2
+ title : Quickstart - Send a query to the API using Java using Bing Local Business Search
3
3
titleSuffix : Azure Cognitive Services
4
4
description : Use this quickstart to begin sending requests to the Bing Local Business Search API, which is an Azure Cognitive Service.
5
5
services : cognitive-services
@@ -8,7 +8,7 @@ manager: nitinme
8
8
ms.service : cognitive-services
9
9
ms.subservice : bing-local-business
10
10
ms.topic : quickstart
11
- ms.date : 11/29/2019
11
+ ms.date : 03/24/2020
12
12
ms.author : aahi
13
13
---
14
14
@@ -32,16 +32,16 @@ The following code creates a `WebRequest`, sets the access key header, and adds
32
32
33
33
``` java
34
34
// construct URL of search request (endpoint + query string)
35
- URL url = new URL (host + path + " ?q=" + URLEncoder . encode(searchQuery, " UTF-8" ) + & mkt= en- us" );
36
- HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
37
- connection.setRequestProperty(" Ocp - Apim - Subscription - Key " , subscriptionKey);
35
+ URL url = new URL (host + path + " ?q=" + URLEncoder . encode(searchQuery, " UTF-8" ) + & mkt= en- us" );
36
+ HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
37
+ connection.setRequestProperty(" Ocp - Apim - Subscription - Key " , subscriptionKey);
38
38
39
- // receive JSON body
40
- InputStream stream = connection.getInputStream();
41
- String response = new Scanner(stream).useDelimiter(" \\A " ).next();
39
+ // receive JSON body
40
+ InputStream stream = connection.getInputStream();
41
+ String response = new Scanner(stream).useDelimiter(" \\A " ).next();
42
42
43
- // construct result object for return
44
- SearchResults results = new SearchResults(new HashMap<String, String>(), response);
43
+ // construct result object for return
44
+ SearchResults results = new SearchResults(new HashMap<String, String>(), response);
45
45
```
46
46
47
47
## Run the complete application
@@ -81,81 +81,81 @@ import com.google.gson.JsonParser;
81
81
82
82
public class LocalSearchCls {
83
83
84
- // ***********************************************
85
- // *** Update or verify the following values. ***
86
- // **********************************************
87
-
88
- // Replace the subscriptionKey string value with your valid subscription key.
89
- static String subscriptionKey = " YOUR - ACCESS - KEY " ;
90
-
91
- static String host = " https: // api.cognitive.microsoft.com/bing";
92
- static String path = " /v7.0/localbusinesses/search" ;
93
-
94
- static String searchTerm = " Hotel in Bellevue" ;
95
-
96
- public static SearchResults SearchLocal (String searchQuery) throws Exception {
97
- // construct URL of search request (endpoint + query string)
98
- URL url = new URL (host + path + " ?q=" + URLEncoder . encode(searchQuery, " UTF-8" ) + " &mkt=en-us" );
99
- HttpsURLConnection connection = (HttpsURLConnection )url. openConnection();
100
- connection. setRequestProperty(" Ocp-Apim-Subscription-Key" , subscriptionKey);
101
-
102
- // receive JSON body
103
- InputStream stream = connection. getInputStream();
104
- String response = new Scanner (stream). useDelimiter(" \\ A" ). next();
105
-
106
- // construct result object for return
107
- SearchResults results = new SearchResults (new HashMap<String , String > (), response);
108
-
109
- // extract Bing-related HTTP headers
110
- Map<String , List<String > > headers = connection. getHeaderFields();
111
- for (String header : headers. keySet()) {
112
- if (header == null ) continue ; // may have null key
113
- if (header. startsWith(" BingAPIs-" ) || header. startsWith(" X-MSEdge-" )) {
114
- results. relevantHeaders. put(header, headers. get(header). get(0 ));
115
- }
116
- }
117
-
118
- stream. close();
119
- return results;
120
- }
121
-
122
- // pretty-printer for JSON; uses GSON parser to parse and re-serialize
123
- public static String prettify(String json_text) {
124
- JsonParser parser = new JsonParser ();
125
- JsonObject json = parser. parse(json_text). getAsJsonObject();
126
- Gson gson = new GsonBuilder (). setPrettyPrinting(). create();
127
- return gson. toJson(json);
128
- }
129
-
130
- public static void main (String [] args) {
131
- try {
132
- System . out. println(" Searching the Web for: " + searchTerm);
133
-
134
- SearchResults result = SearchLocal(searchTerm);
135
-
136
- System . out. println(" \n Relevant HTTP Headers:\n " );
137
- for (String header : result. relevantHeaders. keySet())
138
- System . out. println(header + " : " + result. relevantHeaders. get(header));
139
-
140
- System . out. println(" \n JSON Response:\n " );
141
- System . out. println(prettify(result. jsonResponse));
142
- }
143
- catch (Exception e) {
144
- e. printStackTrace(System . out);
145
- System . exit(1 );
146
- }
147
- }
148
- }
149
-
150
- // Container class for search results encapsulates relevant headers and JSON data
151
- class SearchResults {
152
- HashMap<String , String > relevantHeaders;
153
- String jsonResponse;
154
- SearchResults (HashMap<String , String > headers , String json ) {
155
- relevantHeaders = headers;
156
- jsonResponse = json;
157
- }
158
- }
84
+ // ***********************************************
85
+ // *** Update or verify the following values. ***
86
+ // **********************************************
87
+
88
+ // Replace the subscriptionKey string value with your valid subscription key.
89
+ static String subscriptionKey = " YOUR - ACCESS - KEY " ;
90
+
91
+ static String host = " https: // api.cognitive.microsoft.com/bing";
92
+ static String path = " /v7.0/localbusinesses/search" ;
93
+
94
+ static String searchTerm = " Hotel in Bellevue" ;
95
+
96
+ public static SearchResults SearchLocal (String searchQuery) throws Exception {
97
+ // construct URL of search request (endpoint + query string)
98
+ URL url = new URL (host + path + " ?q=" + URLEncoder . encode(searchQuery, " UTF-8" ) + " &mkt=en-us" );
99
+ HttpsURLConnection connection = (HttpsURLConnection )url. openConnection();
100
+ connection. setRequestProperty(" Ocp-Apim-Subscription-Key" , subscriptionKey);
101
+
102
+ // receive JSON body
103
+ InputStream stream = connection. getInputStream();
104
+ String response = new Scanner (stream). useDelimiter(" \\ A" ). next();
105
+
106
+ // construct result object for return
107
+ SearchResults results = new SearchResults (new HashMap<String , String > (), response);
108
+
109
+ // extract Bing-related HTTP headers
110
+ Map<String , List<String > > headers = connection. getHeaderFields();
111
+ for (String header : headers. keySet()) {
112
+ if (header == null ) continue ; // may have null key
113
+ if (header. startsWith(" BingAPIs-" ) || header. startsWith(" X-MSEdge-" )) {
114
+ results. relevantHeaders. put(header, headers. get(header). get(0 ));
115
+ }
116
+ }
117
+
118
+ stream. close();
119
+ return results;
120
+ }
121
+
122
+ // pretty-printer for JSON; uses GSON parser to parse and re-serialize
123
+ public static String prettify(String json_text) {
124
+ JsonParser parser = new JsonParser ();
125
+ JsonObject json = parser. parse(json_text). getAsJsonObject();
126
+ Gson gson = new GsonBuilder (). setPrettyPrinting(). create();
127
+ return gson. toJson(json);
128
+ }
129
+
130
+ public static void main (String [] args) {
131
+ try {
132
+ System . out. println(" Searching the Web for: " + searchTerm);
133
+
134
+ SearchResults result = SearchLocal(searchTerm);
135
+
136
+ System . out. println(" \n Relevant HTTP Headers:\n " );
137
+ for (String header : result. relevantHeaders. keySet())
138
+ System . out. println(header + " : " + result. relevantHeaders. get(header));
139
+
140
+ System . out. println(" \n JSON Response:\n " );
141
+ System . out. println(prettify(result. jsonResponse));
142
+ }
143
+ catch (Exception e) {
144
+ e. printStackTrace(System . out);
145
+ System . exit(1 );
146
+ }
147
+ }
148
+ }
149
+
150
+ // Container class for search results encapsulates relevant headers and JSON data
151
+ class SearchResults {
152
+ HashMap<String , String > relevantHeaders;
153
+ String jsonResponse;
154
+ SearchResults (HashMap<String , String > headers , String json ) {
155
+ relevantHeaders = headers;
156
+ jsonResponse = json;
157
+ }
158
+ }
159
159
160
160
```
161
161
0 commit comments