3

我必须制作一个能够将 Bing 搜索 API(SOAP 服务)与 java 一起使用的应用程序。它必须对单词进行特定搜索。这是我的代码:

    import com.google.code.bing.search.client.BingSearchClient;
    import com.google.code.bing.search.client.BingSearchServiceClientFactory;
    import com.google.code.bing.search.client.BingSearchClient.SearchRequestBuilder;
    import com.google.code.bing.search.schema.AdultOption;
    import com.google.code.bing.search.schema.SearchOption;
    import com.google.code.bing.search.schema.SearchRequest;
    import com.google.code.bing.search.schema.SearchResponse;
    import com.google.code.bing.search.schema.SourceType;
    import com.google.code.bing.search.schema.web.WebResult;
    import com.google.code.bing.search.schema.web.WebSearchOption;
public class MyApp {    
   String apikey = "****************";
   String searchword="google";
     public static void main(String[] args){
BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
    BingSearchClient client = factory.createBingSearchClient();
    SearchRequestBuilder builder = client.newSearchRequestBuilder();
    builder.withAppId(apikey);
    builder.withQuery(searchword);
    builder.withSourceType(SourceType.WEB);
    builder.withVersion("2.0");
    builder.withMarket("en-us");
    builder.withAdultOption(AdultOption.MODERATE);
    builder.withSearchOption(SearchOption.ENABLE_HIGHLIGHTING);
    builder.withWebRequestCount(10L);
    builder.withWebRequestOffset(0L);
    builder.withWebRequestSearchOption(WebSearchOption.DISABLE_HOST_COLLAPSING);        
    builder.withWebRequestSearchOption(WebSearchOption.DISABLE_QUERY_ALTERATIONS);
    SearchResponse response = client.search(builder.getResult());

    for (WebResult result : response.getWeb().getResults()) {
            System.out.println(result.getTitle());
            System.out.println(result.getDescription());
            System.out.println(result.getUrl());
            System.out.println(result.getDateTime());
    }
}
    }

我找到了这个http://code.google.com/p/bing-search-java-sdk/网站。我从 Azure MarketPlace 获得我的 appkey。我收到一个错误:java.lang.NullPointerException 在将显示响应的 for 循环行。这意味着响应为空。我不明白我错过了什么。

4

1 回答 1

2

bing 目前正在更改他们的许可证系统。此 API 是使用“旧”版本 2 许可证创建的。MS 在迁移到 Azzure 市场时做了一些更改: https ://datamarket.azure.com/dataset/5BA839F1-12CE-4CCE-BF57-A49D98D29A44 迁移指南:http: //go.microsoft.com/fwlink/ ?LinkID =248077

我不认为您已经使用的这个 Java-API 包装器涵盖了这一点。

于 2012-06-20T09:34:44.370 回答