I want to use Bing Autosuggest API in an android app. I used the documented code from here :- https://learn.microsoft.com/en-us/azure/cognitive-services/Bing-Autosuggest/quickstarts/java The code is working fine when run/compiled the same code in a simple java compiler :-https://www.onlinegdb.com/online_java_compiler
My app is able to open the url https://api.cognitive.microsoft.com/bing/v7.0/Suggestions/?subscription-Key=my_key&q=india in a web-view(which means there is no internet problem in the app). Android need to call this in a separate thread, thats why I used Async, here.
For the below code, First Url is working fine(200 as response code), but Second one gives Response code as 404.
And both can be open in a browser with their exact url link
url = new URL("https://api.bing.com/osjson.aspx?query=abcd");
url = new URL("https://api.cognitive.microsoft.com/bing/v7.0/Suggestions/?mkt=en-IN&subscription-Key=key&q=india");
HttpsURLConnection connection = null;
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
int HttpsREsult = connection.getResponseCode();
As mentioned in the documentation, I have written the code as follow:-
In Android the code is written as below:-
public class bing_ac_main extends AsyncTask<String,Void,String> {
protected String subscriptionKey = "my key";
protected String host = "https://api.cognitive.microsoft.com";
protected String path = "/bing/v7.0/Suggestions";
protected String mkt = "en-IN";
protected String query = "sail";
@Override
protected String doInBackground(String... strings) {
String encoded_query = null;
try {
encoded_query = URLEncoder.encode (query, "UTF-8");
String params = "?mkt=" + mkt + "&q=" + encoded_query;
URL url = new URL (host + path + params);
HttpsURLConnection connection = null;
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setDoOutput(true);
int HttpsREsult = connection.getResponseCode();
String Httpsstring= connection.getResponseMessage();
.......
In the online compiler I have written the same code in a function as:-
public static String getResponse(){
String subscriptionKey = "my key";
String host = "https://api.cognitive.microsoft.com";
String path = "/bing/v7.0/Suggestions";
String mkt = "en-IN";
String query = "ind";
String encoded_query = null;
try {
encoded_query = URLEncoder.encode (query, "UTF-8");
String params = "?mkt=" + mkt + "&q=" + encoded_query;
URL url = new URL (host + path + params);
HttpsURLConnection connection = null;
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setDoOutput(true);
int HttpsREsult = connection.getResponseCode();
.....
}
I expected the HttpsResult should be 200, but the actual output is 404 in android. I am getting HttpsResult as 200 in the online compiler for the same code, which is correct