Skip to content

Commit 9aa4407

Browse files
committed
reducing scope
1 parent d317eba commit 9aa4407

File tree

1 file changed

+84
-84
lines changed

1 file changed

+84
-84
lines changed

articles/cognitive-services/bing-local-business-search/quickstarts/local-search-java-quickstart.md

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: nitinme
88
ms.service: cognitive-services
99
ms.subservice: bing-local-business
1010
ms.topic: quickstart
11-
ms.date: 03/24/2020
11+
ms.date: 11/29/2019
1212
ms.author: aahi
1313
---
1414

@@ -32,16 +32,16 @@ The following code creates a `WebRequest`, sets the access key header, and adds
3232

3333
```java
3434
// 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);
3838
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();
4242
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);
4545
```
4646
4747
## Run the complete application
@@ -81,81 +81,81 @@ import com.google.gson.JsonParser;
8181
8282
public class LocalSearchCls {
8383
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("\nRelevant HTTP Headers:\n");
137-
for (String header : result.relevantHeaders.keySet())
138-
System.out.println(header + ": " + result.relevantHeaders.get(header));
139-
140-
System.out.println("\nJSON 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("\nRelevant HTTP Headers:\n");
137+
for (String header : result.relevantHeaders.keySet())
138+
System.out.println(header + ": " + result.relevantHeaders.get(header));
139+
140+
System.out.println("\nJSON 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+
}
159159

160160
```
161161

0 commit comments

Comments
 (0)