android - why i cant get anything back for bing API -
i cant thing bing img search api, here details of api
since httpclient deprecated use httpurlconnection, can tell me wrong code ?
all params , key good, have tested on website.
thanks!
public void run() { httpurlconnection connection = null; try { url url = new url("https://api.cognitive.microsoft.com/bing/v5.0/images/search"); connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("post"); connection.setdoinput(true); connection.setdooutput(true); connection.setconnecttimeout(8000); connection.setreadtimeout(8000); connection.setusecaches(false); connection.setrequestproperty("ocp-apim-subscription-key", "562eaaada4b644f2bea31a454f26d905"); outputstream out = connection.getoutputstream(); dataoutputstream params =new dataoutputstream(out); params.writebytes("q=dog&count=10&offset=0&mkt=en-us&safesearch=moderate"); out.close(); connection.connect(); inputstream in = connection.getinputstream(); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); stringbuilder response = new stringbuilder(); string line; while ((line = reader.readline()) != null) { response.append(line); } message message = new message(); message.what = show_response; message.obj = response.tostring(); handler.sendmessage(message); } catch (exception e) { // todo: handle exception } { if (connection != null) { connection.disconnect(); } } }
assuming doing "post" call imageinsights.
imageinsights
connection .setrequestproperty("content-type", "multipart/form-data");
and here contents wrong
params.writebytes("q=dog&count=10&offset=0&mkt=en-us&safesearch=moderate");// takes post post body not query param string.
for search api "get" call not "post" call
search api
https://api.cognitive.microsoft.com/bing/v5.0/images/search[?q][&count][&offset][&mkt][&safesearch] here every in url query string, have write in outputstream
check below sample(you can try there api sample )
url url = new url("https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=cats&count=10&offset=0&mkt=en-us&safesearch=moderate"); connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("get"); connection.setdoinput(true); connection.setdooutput(true); connection.setconnecttimeout(8000); connection.setreadtimeout(8000); connection.setusecaches(false);
Comments
Post a Comment