Author: Roopali Kaujalgi, Program Manager II, Bing API
This is the second of a series of posts that show how you can use the API with Java. This post shows how you can use Java to create a console application that sends a request to the API's SOAP interface and displays the results. The last post in the series will include a pointer to a site from which you can download all instructions and code associated with each post in the series.
This code sample demonstrates how to:
import java.rmi.RemoteException; import java.util.Iterator; import org.apache.axiom.om.OMElement; import org.apache.axis2.AxisFault; import org.apache.axis2.databinding.types.UnsignedInt; import org.apache.axis2.transport.http.HTTPConstants; import com.microsoft.schemas.livesearch._2008._03.search.LiveSearchServiceStub; import com.microsoft.schemas.livesearch._2008._03.search.LiveSearchServiceStub.*; public class WebSample { static LiveSearchServiceStub stub = null; public static void main( String [] args ) throws RemoteException { try { // Initialize the service stub = new LiveSearchServiceStub(); stub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED, false); // Build the request LiveSearchServiceStub.SearchRequest searchRequest= BuildRequest(); // Get the response SearchResponseE response = GetResponse(searchRequest); // Display the response DisplayResponse(response); } catch (AxisFault e) { DisplayErrors(e); } } private static SearchRequest BuildRequest() { SearchRequest request = new SearchRequest(); // Common request fields (required) request.setAppId("Insert your AppID here"); request.setQuery("msdn blogs"); ArrayOfSourceType param = new ArrayOfSourceType(); param.addSourceType(SourceType.Web); request.setSources(param); // Common request fields (optional) request.setVersion("2.0"); request.setMarket("en-us"); request.setAdult(AdultOption.Moderate); // Web-specific request fields (optional) WebRequest web = new WebRequest(); request.setWeb(web); web.setCount(new UnsignedInt("10")); web.setOffset(new UnsignedInt("0")); web.setFileType("DOC"); ArrayOfWebSearchOption options = new ArrayOfWebSearchOption(); options.addWebSearchOption(WebSearchOption.DisableHostCollapsing); options.addWebSearchOption(WebSearchOption.DisableQueryAlterations); web.setOptions(options); return request; } private static SearchResponseE GetResponse(SearchRequest searchRequest) throws RemoteException { SearchRequestE searchRequestE = new SearchRequestE(); searchRequestE.setParameters(searchRequest); return stub.Search(searchRequestE); } private static void DisplayResponse(SearchResponseE responseE) { SearchResponse response = responseE.getParameters(); int startIndex = response.getWeb().getOffset().intValue() + 1; int endIndex = response.getWeb().getOffset().intValue() + response.getWeb().getResults().getWebResult().length; // Display the results header. System.out.println("Live Search API Version " + response.getVersion()); System.out.println("Web results for " + response.getQuery().getSearchTerms()); System.out.println("Displaying " + (startIndex) + " to " + (endIndex) + " of " + response.getWeb().getTotal() + " results "); System.out.println(); // Display the Web results. StringBuilder builder = new StringBuilder(); WebResult[] results = response.getWeb().getResults().getWebResult(); for(int i =0 ; i < results.length; i++) { builder.setLength(0); builder.append(results[i].getTitle()); builder.append("\n"); builder.append(results[i].getDescription()); builder.append("\n"); builder.append(results[i].getUrl()); builder.append("\n"); builder.append("Last Crawled: "); builder.append(results[i].getDateTime()); builder.append("\n"); System.out.println(builder.toString()); } System.out.println(); } @SuppressWarnings("unchecked") private static void DisplayErrors(AxisFault e) { System.out.println("Live Search API errors.\n"); System.out.println("FaultString:" + e.getReason()); System.out.println("FaultDetails:\n"); OMElement element = e.getDetail(); Iterator iterator = element.getChildElements(); while(iterator.hasNext()) { OMElement childElement = (OMElement) iterator.next(); Iterator childIterator = childElement.getChildElements(); while(childIterator.hasNext()) { OMElement ele = (OMElement)childIterator.next(); System.out.println("\t" + ele.getLocalName() + " : " + ele.getText()); } } } }
nice
cool
If a user turns off Java in his/her web browser, will it cause the application to malfunction?
I love Bing Search so Much!
Can u please tell me wat to mean by ' run ' that command ?
run means to execute a command so it start to work.
Nice Post....make sense and seems useful!! Thanks
Hey I just had a quick question. I was trying to run the above sample, and hence was going through the 9 steps mentioned above under the heading "To run the sample". But after I ran step 2, I got the error log4j:WARN no appenders could be found for logger (org.apache.axis2.description.WSDL11ToAllAxisServicesBuilder).
log4j:WARN Please initialize the log4j system properly.
I have set the Axis2_Home env variable and JAVA_HOME, and have also included my JAVA_HOME/bin in my PATH env variable. Please do let me know what the problem is.
Issue Resolved Thanks!!
When I try to compile the websample.java it gives me an error because of two missing imports that are not in the library files. com.microsoft.schemas.livesearch._2008._3.search.LiveSearchServiceStub;
com.microsoft.schemas.livesearch._2008._3.search.LiveSearchServiceStub.*;
could anyone tell me where I could download these jar files from, off the internet.