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.
-- Roopali Kaujalgi
Requirements
Demonstrates
This code sample demonstrates how to:
- Send a request to the Live Search SOAP interface and the Web SourceType
- Display the Live Search response as results
To run the sample
- Set the AXIS2_HOME environment variable to the directory where you have installed Axis2.
- From any directory, run %AXIS2_HOME%\bin\wsdl2java -uri http://api.search.live.net/search.wsdl?AppId=<Insert your AppID here> to create the web service proxy classes. For the rest of this procedure, we refer to the directory in which you run this procedure as directory X.
- The classes will be generated with a folder structure as X\src\com\microsoft\schemas\livesearch\_2008\_03\search.
- Create a text file and save it as WebSample.java in X\src.
- Copy the code under "Example" below, and save it in WebSample.java.
- Insert your AppId into the WebSample class in the BuildRequest method.
- Make sure your CLASSPATH environment variable contains all jar files from AXIS2_HOME\lib directory.
- Compile Websample and the proxy classes.
- Run WebSample, using the command java WebSample.
Example
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());
}
}
}
}