Java Bing api示例代码

Java Bing api示例代码,java,Java,我使用bing 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.Ba

我使用bing 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();
        }


    }        
}
它只显示一个结果,但我需要不止一个结果。 用这个代码可以吗?或者是该代码的其他替代品?
谢谢你打电话给Bing,你请求的结果是一个提要,这就是你得到的结果(对于这个特定的查询是38785个字符),你真的应该把它当作一个Atom提要,并以更合适的方式解析它


但是,在代码中只得到一个结果的原因是,您似乎从未在包含提要的
sb
字符串上循环。如果你真的想以这种方式解析提要,你需要在
conn.disconnect()
之后进入一个循环,并在调用Bing时使用类似于
sb.indexOf(“)的内容,你正在请求作为提要的结果,这就是你得到的结果(对于特定的查询,38785个字符长)你真的应该把它当作一个Atom提要,用一种更合适的方式来解析它


但是,在代码中只得到一个结果的原因是,您似乎从未在包含提要的
sb
字符串上循环。如果您确实希望以这种方式解析提要,则需要将
conn.disconnect()
后的代码移动到循环,并使用类似
sb.indexOf(“@NargesSadri如果您觉得答案有帮助,请随时投票和/或接受。感谢您的回答。我是java和bing live search新手。不幸的是,我对您的答案不太了解。其他bing api示例代码是否为我提供了帮助?我的意思是,在输出中,结果可能不止一个?@NargesSadri您从查询并分配给
输出
变量实际上包含多个结果,但它的格式是需要解析的XML文档。如果您在URL中将
格式=原子
更改为
格式=json
,则可以以该格式获得结果,这可能更容易解析,但在任何情况下都需要解析重新生成的结果sult访问单个结果。我建议搜索类似解析json java的内容。我无法真正帮助您进行解析,因为我自己也不太了解它。@NargesSadri如果您发现答案有帮助,请随意投票和/或接受它。感谢您的回答。我是java和bing live search的新手。不幸的是,我不太了解h了解你的答案。是不是有其他bing api示例代码为我做了这件事?我的意思是在输出中结果可能不止一个?@NargesSadri从查询中返回并分配给
输出
变量的结果实际上包含不止一个结果,但它的格式是需要解析的XML文档。你可以得到如果您在URL中将
format=Atom
更改为
format=json
,则会生成格式,这可能更容易解析,但在任何情况下,您都需要解析结果才能访问单个结果。我建议搜索类似解析json java的内容。我无法真正帮助您进行解析,因为我自己对它了解不多.
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 ...