Java and the Yahoo Search Marketing API
Using the Yahoo Search Marketing (formerly Overture) API with Java is unremarkably easy. The sample documentation provided by Yahoo! gives examples using Perl. Following these examples for making a request as a rough guide, there’re only a few simple changes to make with Java.
Posting a Request
To make the xml document, create a StringBuffer object, append the URL encoded strings using the URLencoder.encode method, be sure to manually encode spaces (the URLencoder.encode method escapes spaces as + signs, which you don’t want), and then convert the StringBuffer to a plain old String.
View a sample that requests a market state for “queryOut” returning a “numberListings” number of listings. “queryOut” is an encoded string. “numberListings” is an int. The first section of code takes a query and encodes it to produce “queryOut” so that it fits properly into the StringBuffer section.
Making the Post and Getting the Response
Posting the request requires you to open a new URL connection, post your output, read the response, and write it to an xml file:
URL url = new URL( postURL );
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setRequestMethod(“POST”);
urlcon.setRequestProperty(“Content-type”, “application/x-www-form-urlencoded”);
urlcon.setDoOutput(true);
urlcon.setDoInput(true);
PrintWriter pout = new PrintWriter( new OutputStreamWriter(urlcon.getOutputStream(), “8859_1″), true);
pout.print( urlEncodedValue );
pout.flush();
InputStream in = urlcon.getInputStream();
BufferedReader bin = new BufferedReader( new InputStreamReader( in ) );
String response;
myfile = new File(filename);
FileWriter mywriter = new FileWriter(myfile);
while((response = bin.readLine()) != null) {
mywriter.write( response );
}
mywriter.close();
“filename” should be something.xml. Now you can parse the xml file, and use the response however you’d like.

