1

我正在尝试运行 Bing 搜索 API。我使用了 odata4j 并尝试了这里提供的代码: How to use Bing search api in Java

ODataConsumer c = ODataConsumers
    .newBuilder("https://api.datamarket.azure.com/Bing/Search")
    .setClientBehaviors(OClientBehaviors.basicAuth("accountKey", "{your account key here}"))
    .build();

OQueryRequest<OEntity> oRequest = c.getEntities("Web")
    .custom("Query", "stackoverflow bing api");

Enumerable<OEntity> entities = oRequest.execute();

我在bing服务上注册后,拿到了key,放在上面代码的双引号里面。我收到以下错误:

Exception in thread "main" java.lang.RuntimeException: Expected status OK, found Bad Request. Server response:
Parameter: Query is not of type String
    at org.odata4j.jersey.consumer.ODataJerseyClient.doRequest(ODataJerseyClient.java:165)
    at org.odata4j.consumer.AbstractODataClient.getEntities(AbstractODataClient.java:69)
    at org.odata4j.consumer.ConsumerQueryEntitiesRequest.doRequest(ConsumerQueryEntitiesRequest.java:59)
    at org.odata4j.consumer.ConsumerQueryEntitiesRequest.getEntries(ConsumerQueryEntitiesRequest.java:50)
    at org.odata4j.consumer.ConsumerQueryEntitiesRequest.execute(ConsumerQueryEntitiesRequest.java:40)
    at BingAPI.main(BingAPI.java:20)
Caused by: org.odata4j.exceptions.UnsupportedMediaTypeException: Unknown content type text/plain;charset=utf-8
    at org.odata4j.format.FormatParserFactory.getParser(FormatParserFactory.java:78)
    at org.odata4j.jersey.consumer.ODataJerseyClient.doRequest(ODataJerseyClient.java:161)
    ... 5 more

我无法弄清楚问题所在。

4

2 回答 2

1

您需要做的就是像 %27stackoverflow bing api%27 那样设置您的查询

这是我的源代码:

ODataConsumer consumer = ODataConsumers
            .newBuilder("https://api.datamarket.azure.com/Bing/Search/v1/")
            .setClientBehaviors(
                    OClientBehaviors.basicAuth("accountKey",
                            "{My Account ID}"))
            .build();

    System.out.println(consumer.getServiceRootUri() + consumer.toString());

    OQueryRequest<OEntity> oQueryRequest = consumer.getEntities("Web")
            .custom("Query", "%27stackoverflow%27");

    System.out.println("oRequest : " + oQueryRequest);

    Enumerable<OEntity> entities = oQueryRequest.execute();
    System.out.println(entities.elementAt(0));
于 2015-02-27T11:10:58.100 回答
1

您可以通过使用 oQueryrequest 对象的 .custom("Type of parameter","parameter") 继续添加名称-值对来进一步尝试使用不同过滤器的不同查询。假设您想搜索印度食物,但您只想要小方块图片。

    ODataConsumer consumer = ODataConsumers
            .newBuilder("https://api.datamarket.azure.com/Bing/Search/v1/")
            .setClientBehaviors(
                    OClientBehaviors.basicAuth("accountKey",
                            "YOUR ACCOUNT KEY"))
            .build();

    System.out.println(consumer.getServiceRootUri() + consumer.toString());

    OQueryRequest<OEntity> oQueryRequest = consumer.getEntities("Image")
            .custom("Query", "%27indian food%27");    
    oQueryRequest.custom("Adult", "%27Moderate%27");
    oQueryRequest.custom("ImageFilters", "%27Size:Small+Aspect:Square%27");
    System.out.println("oRequest : " + oQueryRequest);

    Enumerable<OEntity> entities = oQueryRequest.execute();

    int count = 0;
    Iterator<OEntity> iter = entities.iterator();
    System.out.println(iter.next());
于 2016-06-16T07:15:27.823 回答