2013-06-22 66 views
1

我使用Bing API此示例代碼:兵API示例代碼

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

import org.apache.commons.codec.binary.Base64; 
import org.jsoup.Jsoup; 
public class bingSearch { 
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     //--------------------------------------Bing search------------------------------ 
     String searchText = "swim"; 
     searchText = searchText.replaceAll(" ", "%20"); 
     String accountKey="Your-AccountKEY"; 
     byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes()); 
     String accountKeyEnc = new String(accountKeyBytes); 
     URL url; 
     try { 
      url = new URL( 
        "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=%27" + searchText + "%27&$top=50&$format=Atom"); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Authorization", "Basic " + accountKeyEnc); 

    // conn.setRequestProperty("Accept", "application/json"); 

     BufferedReader br = new BufferedReader(new InputStreamReader(
       (conn.getInputStream()))); 
     StringBuilder sb = new StringBuilder(); 
     String output; 
     System.out.println("Output from Server .... \n"); 
     char[] buffer = new char[4096]; 
     while ((output = br.readLine()) != null) { 
      sb.append(output); 

       // text.append(link + "\n\n\n");//Will print the google search links 
      //}  
     } 

     conn.disconnect(); 

     int find = sb.indexOf("<d:Description"); 

     int total = find + 1000; 

     System.out.println("Find index: " + find); 
     System.out.println("Total index: " + total); 
     sb.getChars(find+35, total, buffer, 0); 

     String str = new String(buffer); 

     int find2 = str.indexOf("</d:Description>"); 

     int total2 = find2 + 400; 
     System.out.println("Find index: " + find); 
     System.out.println("Total index: " + total); 
     char[] buffer2 = new char[1024]; 

     str.getChars(0, find2, buffer2 , 0); 
     String str2 = new String(buffer2); 
     str2 = Jsoup.parse(str2).text();  
     System.out.println(str2); 

     } catch (MalformedURLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    }   
} 

的出來說就是:從服務器

輸出....

Find index: 1014 
Total index: 2014 
Find index: 1014 
Total index: 2014 
A computer is a general purpose device that can be programmed to carry out a finite set of arithmetic or logical operations. Since a sequence of operations can be ... 

它只顯示一個結果,但我需要不止一個結果。 使用此代碼可以做到這一點嗎?或者是這個代碼的其他替代品嗎? 感謝

回答

1

在您的來電兵您所請求的結果作爲Atom飼料,這是你在說什麼回來(長針對特定查詢38785個字符),你真的應該把它看作一個Atom feed和解析它以更合適的方式。

的原因,你只能得到你的代碼的一個結果是,雖然你似乎永遠循環在包含飼料sb字符串。如果你真的想解析進這種方式,你需要conn.disconnect()後移動到代碼迴路,並使用類似sb.indexOf("<d:Description", int fromIndex)遍歷字符串增加每次找到一個新的比賽時間的fromIndex。

但你真的應該把從兵像XML的飼料是使用一些XML庫,例如Rome解析它的響應。

+0

謝謝您的回答 –

+0

@NargesSadri如果你找到了答案有幫助隨意給予好評和/或接受它。 – jpw

+0

感謝您的回答。我是新的Java和bing實時搜索。不幸的是我對你的回答並不瞭解。其他bing api示例代碼是爲我做的嗎?我的意思是在輸出結果不止一個? –