我只是不能得到任何东西从Bing img搜索API,这里是这个API的细节.
https://dev.cognitive.microsoft.com/docs/services/56b43f0ccf5ff8098cef3808/operations/56b4433fcf5ff8098cef380c
由于HttpClient已被弃用,所以我使用httpURLconnection
,有人能告诉我我的代码有什么问题吗?
所有的参数和关键是好的,我在网站上测试.
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
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
字符串
1条答案
按热度按时间koaltpgm1#
假设您正在执行“POST”调用ImageInsights。
ImageInsights
字符串
这里的内容是错误的
型
对于搜索API,是“GET”调用而不是“POST”调用
搜索API
型
检查下面的示例(你可以尝试那里的API示例)
型