简体   繁体   English

java中的Web服务客户端

[英]web service client in java

I want to generate a client program using the service 我想使用该服务生成一个客户端程序

I am unable to display the results, how can I do so? 我无法显示结果,我该怎么办?

import java.rmi.RemoteException; 


public class searchtry {
public static void main(String[] args) throws RemoteException { 

  SearchRequest request=new SearchRequest(); 
  SearchRequestType1 type1=new SearchRequestType1(); 
  query.setAppId("*********************************"); //Windows Live gave this id for using that service  
  query.setSources(new SourceType[]{SourceType.Web}); 
  query.setQuery("Java"); 
  aratip.setParameters(request); 
  SearchResponseType0 answer= client.search(type1); 
  System.out.println(answer.toString()); 
} 

For starters, calling 对于初学者来说,打电话

answer.toString();

May or may not result in anything (usually won't). 可能会或可能不会产生任何结果(通常不会)。 You might just get a string that represents the instance, not the string you're expecting. 您可能只获得一个表示实例的字符串,而不是您期望的字符串。 You need to find a method on SearchResponseType0 that will give you the string representation of the response. 您需要在SearchResponseType0上找到一个方法,该方法将为您提供响应的字符串表示形式。 Perhaps a method like getContent() or getResponse() or something like that but without understanding more about the web service it's difficult to give you more help. 也许像getContent()getResponse()这样的方法或类似的东西,但如果不了解更多有关Web服务的信息,很难给你更多的帮助。 Bottom line, you're using the wrong method to attempt to get the string content of the result. 最重要的是,您使用了错误的方法来尝试获取结果的字符串内容。

It looks like your are using the bing-search-java-sdk . 看起来你正在使用bing-search-java-sdk They have a very nice example on their homepage you might want to look at: 他们在主页上有一个非常好的例子,你可能想看一下:

BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
BingSearchClient client = factory.createBingSearchClient();

SearchRequestBuilder builder = client.newSearchRequestBuilder();
builder.withAppId(applicationId);
builder.withQuery("msdn blogs");
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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM