为什么我无法从 Bing API 获得任何回报

问题描述 投票:0回答:1

我无法从 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();
                }
            }
            
        }
android httpurlconnection bing
1个回答
0
投票

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

图像洞察

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);
© www.soinside.com 2019 - 2024. All rights reserved.