gpt4 book ai didi

java - 必应 API 示例代码

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:02 24 4
gpt4 key购买 nike

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


}
}

输出是:

服务器的输出....

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 ...

它只显示一个结果,但我需要多个结果。用这段代码可以做到吗?或者这个代码有其他替代品吗?谢谢

最佳答案

在调用 Bing 时,您请求的结果为 Atom feed,这就是您返回的内容(该特定查询的长度为 38785 个字符),您确实应该将其视为 Atom feed 并以更合适的方式解析它。

您在代码中只得到一个结果的原因是您似乎从未循环过 sb包含提要的字符串。如果您确实想以这种方式解析提要,您需要移至 conn.disconnect() 之后的代码到循环并使用类似 sb.indexOf("<d:Description", int fromIndex) 的内容遍历字符串,每次找到新的匹配项时都会增加 fromIndex。

但是您确实应该将 Bing 的响应视为 xml-feed,并使用某些 xml-library 对其进行解析,例如 Rome .

关于java - 必应 API 示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17250897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com