为什么我不能为bing API取回任何东西

时间:2016-11-05 16:27:35

标签: android api httpclient httpurlconnection bing

我无法从bing img search api中得到任何回报,这里是api的详细信息

https://dev.cognitive.microsoft.com/docs/services/56b43f0ccf5ff8098cef3808/operations/56b4433fcf5ff8098cef380c

因为HttpClient已被弃用所以我使用httpURLconnection,有人可以告诉我我的代码有什么问题吗?

所有的参数和关键都很好,我已在网站上测试过。

谢谢!

$(callback)

1 个答案:

答案 0 :(得分:0)

假设您正在对ImageInsights进行“POST”调用。

  

ImageInsights

connection .setRequestProperty("Content-Type", "multipart/form-data");

并且这里的内容有误

  params.writeBytes("q=dog&count=10&offset=0&mkt=en-us&safeSearch=Moderate");//
it takes post post body not query param String.

对于搜索,api是“GET”呼叫而不是“POST”呼叫

  

搜索Api

https://api.cognitive.microsoft.com/bing/v5.0/images/search[?q][&count][&offset][&mkt][&safeSearch]
here every this is in url query string, you have write it in outputstream

检查下面的样本(你可以尝试api样本)

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);