2016-11-05 3 views
1

bekommen kann ich nicht kippe, irgend etwas von bing img Suche api bekommen nur zurück, hier ist die Details dieses api, warum ich etwas zurück für bing API

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

seit Httpclient so verwende ich veraltet HttpURLConnection, Kann mir jemand sagen, was mit meinem Code nicht stimmt?

alle params und der schlüssel sind gut, ich habe auf der website getestet.

danke!

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

     } 

Antwort

0

Angenommen, Sie machen "POST" für ImageInsights.

ImageInsights

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

und die hier um den Inhalt falsch sind

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

für Search api ist "GET" nicht "POST" nennen

Suche Api

rufen

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 

Prüfung unter Probe (man kann es api Probe versuchen)

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