Sample Code in Java
A short chunk of Java sample code below demonstrates how to fetch listings from the Oodle API.
In the sample code, we do the following:
- Assemble the parameters in a HashMap
- Create the XmlRpcClient object
- Execute the XmlRpcClient call with that parameter set
- Print the hierarchical response data structure using some recursion
- xmlrpc-client
- xmlrpc-common
- ws-commons-util
- commons-httpclient-3.0
- commons-logging
- commons-codec-1.3
Sample Code
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
public class OodleApi
{
/**
* Creates the hash of parameters for the Oodle API's "get" method.
* Keys are all strings. Values may be strings, arrays, booleans...
* http://developer.oodle.com/get has full list of parameters.
* @return Parameter hash ready to be sent by XmlRpcClient
*/
public static Object[] setupMethodParams()
{
HashMap map = new HashMap();
map.put("partner_id", "TEST"); // Your API key goes here!
map.put("region", "chicago"); // 'region' is a string (required)
map.put("category", "vehicle"); // 'category' is a string (optional)
map.put("from", 1); // from: listing #1 (optional)
map.put("to", 5); // to: listing #5 (optional)
// "dimensions" is an array of strings (optional)
String[] dimensions = { "make_honda", "model_civic" };
map.put("dimensions", dimensions);
return new Object[] { map };
}
/**
* Send the XML-RPC request to the Oodle API.
* @param methodParams Hash of method parameters from setupMethodParams()
* @return The body of the Oodle API's XML-RPC response
*/
public static Map getResponse(Object[] methodParams) throws Exception
{
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://api.oodle.com:80/api"));
XmlRpcClient client = new XmlRpcClient();
client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
client.setConfig(config);
return (Map) client.execute("get", methodParams);
}
private static void print(Object object, String indent)
{
if (object instanceof String)
{
System.out.println("'" + object + "'");
}
else if (object instanceof Number)
{
System.out.println(object);
}
else if (object instanceof Boolean)
{
System.out.println(object.toString().toUpperCase() + " (boolean)");
}
else if (object instanceof Map)
{
print((Map) object, indent);
}
else if (object instanceof Object[])
{
print((Object[]) object, indent);
}
}
private static void print(Object[] list, String indent)
{
System.out.println();
System.out.println(indent + "[");
String newIndent = "\t" + indent;
for(Object obj : list)
{
print(obj, newIndent);
}
System.out.println(indent + "]");
}
private static void print(Map map, String indent)
{
System.out.println();
System.out.println(indent + "{");
String newIndent = "\t" + indent;
for(Object key: map.keySet())
{
System.out.print(newIndent + key + " => ");
print(map.get(key), newIndent);
}
System.out.println(indent + "}");
}
/**
* Setup Oodle API "get" method params, send request, print response.
*/
public static void main(String[] args)
{
Object[] methodParams = setupMethodParams();
try
{
Map response = getResponse(methodParams);
print(response, "");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Sample Output
{
total => 2187
current =>
{
dimension =>
[
{
value => 'Honda'
name => 'Make'
id => 'make_honda'
}
{
value => 'Civic'
name => 'Model'
id => 'model_civic'
}
]
category =>
[
{
url => 'vehicle'
name => 'Cars & Vehicles'
id => 'vehicle'
abbrev => 'Cars'
}
]
}
items =>
[
{
create_time => 1201709083
title => '2008 Honda Civic Grey'
trim => 'trim_lx:LX'
privateparty => '0'
paid => TRUE (boolean)
condition => 'New'
id => '745248433'
longitude => '-88.0775986'
sbody => 'Driver Air Bag, Passenger Air Bag,Passenger Air Bag On/Off Switch,Side Air Bag,Side Head Air Bag,A/C,Alarm,AM/FM Stereo,CD Player,4-Wheel ABS,Front Disc/Rear Drum Brakes,Cruise Control,Rear Defrost,Child Safety Locks,Front Wheel Drive,Front Floor Mats,Daytime Running Lights,Keyless Entry,Power Door Locks,Power Driver Mirror,Pass-Through Rear Seat,Cloth Seats,Bucket Seats,Power Steering,Adjustable Steering Wheel,Tires - Front Performance,Tires - Rear Performance,Compact Spare Tire,Power Windows,Intermittent Wipers,MP3 Player,Power Passenger Mirror,Variable Speed Intermittent Wipers,Auxiliary Pwr Outlet,Vehicle Anti-Theft System,Driver Vanity Mirror,Passenger Vanity Mirror,Rear Seat Heat Ducts,Front Reading Lamps,Emergency Trunk Release,Remote Trunk Release,Tire Pressure Monitoring System,Wheel Covers,Steel Wheels,Auxiliary Audio Input'
image_height => '84'
price => '18395.0'
locationcity => 'Lisle'
thumb => 'http://i.oodleimg.com/item/745248433_1s.jpg?1201712361'
url => 'http://www.oodle.com/rx/745248433-1365p1025-TEST/www.automart.com/vehicledetail/adid-77299274/?CMP=KNC-Oodle&WT.srch=1&WT.mc_id=Oodle'
location => 'Lisle'
registration => '0'
has_photo => 'Thumbnail'
make => 'Honda'
image_alt => '2008 Honda Civic Grey'
color => 'Grey'
year => '2008'
category_attr => 'vehicle/car/sedan'
mileage => '0'
category_name => 'Sedans'
model => 'Civic'
latitude => '41.8051987'
sellertype => 'Dealer'
source => 'Honda of Lisle (AutoMart.com)'
zipcode => '60532'
image_width => '112'
}
{
create_time => 1201697815
title => '2008 Honda Civic Grey'
trim => 'trim_hybrid:Hybrid'
privateparty => '0'
paid => TRUE (boolean)
condition => 'New'
id => '745134264'
longitude => '-88.0775986'
sbody => 'Power Passenger Mirror, Vehicle Anti-Theft System,Intermittent Wipers,Power Steering,Brake Assist,Variable Speed Intermittent Wipers,Rear Defrost,Adjustable Steering Wheel,Auxiliary Audio Input,Rear Spoiler,Driver Vanity Mirror,Front Wheel Drive,Remote Trunk Release,Side Air Bag,Daytime Running Lights,Rear Seat Heat Ducts,Tire Pressure Monitoring System,Integrated Turn Signal Mirrors,Power Windows,Tires - Rear All-Season,Side Head Air Bag,Front Disc/Rear Drum Brakes,Auxiliary Pwr Outlet,AM/FM Stereo,CD Player,Power Door Locks,Passenger Air Bag,A/C,Front Reading Lamps,Driver Air Bag,Tires - Front All-Season,Bucket Seats,Keyless Entry,Passenger Vanity Mirror,Passenger Air Bag On/Off Switch,Compact Spare Tire,4-Wheel ABS,Power Driver Mirror,Child Safety Locks,MP3 Player,Aluminum Wheels,Emergency Trunk Release,Climate Control,Front Floor Mats,Cruise Control'
image_height => '84'
price => '23235.0'
locationcity => 'Lisle'
thumb => 'http://i.oodleimg.com/item/745134264_1s.jpg?1201700580'
url => 'http://www.oodle.com/rx/745134264-1365p1025-TEST/www.automart.com/vehicledetail/adid-77146350/?CMP=KNC-Oodle&WT.srch=1&WT.mc_id=Oodle'
location => 'Lisle'
registration => '0'
has_photo => 'Thumbnail'
make => 'Honda'
image_alt => '2008 Honda Civic Grey'
color => 'Grey'
year => '2008'
category_attr => 'vehicle/car/sedan'
mileage => '0'
category_name => 'Sedans'
model => 'Civic'
latitude => '41.8051987'
sellertype => 'Dealer'
source => 'Honda of Lisle (AutoMart.com)'
zipcode => '60532'
image_width => '112'
}
{
create_time => 1201702864
title => '2008 Honda Civic Lx'
trim => 'trim_lx:LX'
sellername => 'Mcgrath Honda Of St Charles'
privateparty => '0'
paid => FALSE (boolean)
condition => 'New'
id => '745227261'
longitude => '-88.2987976'
sbody => '12V Power Outlet: Front And 1 13.2 Gallon Main Unleaded Fuel Tank Air Conditioning Audio System With AM/FM And CD Player CD Player Reads MP3 Bucket Driver Seat With Height Adjustment, Bucket Passenger Seat Cargo Area Light Cargo Capacity: All Seats In Place (cu Ft): 12 Clock Cloth Seat Upholstery With Additional Cloth Complex Surface Lens Halogen Bulb Headlights Cruise Control Dashboard Day Time Running Lights Delayed/fade Courtesy Lights Door Ajar Warning Driver And Passenger Power Body Color Door Mirrors Driver Front Airbag With Multi-stage Deployment, Passenger Front Airbag With Occupant Sensors And Mu Electronic Brake Distribution Emission Control Level ULEV Fixed Rear Window With Defogger Four Speaker(s) Four-wheel ABS Front And Rear Roof Airbag Front And Rear Steel Wheels With 16 Inch Rim Diam, 6.5 Inch Rim Width And Full Wheel Covers Front Power Windows With One One-touch, Rear Power Windows Front Reading Lights Front Seat Center Armrest Front Side Airbag With Occupant Sensors Illuminated Entry System'
image_height => '84'
price => '17595.0'
locationcity => 'Saint Charles'
thumb => 'http://i.oodleimg.com/item/745227261_1s.jpg?1201706598'
url => 'http://www.oodle.com/rx/745227261-122u1025-TEST/www.drivechicago.com/heart.php?setsub=3725&vehicle_id=257158'
location => 'St. Charles'
registration => '0'
has_photo => 'Thumbnail'
make => 'Honda'
image_alt => '2008 Honda Civic Lx'
year => '2008'
category_attr => 'vehicle/car/sedan'
category_name => 'Sedans'
model => 'Civic'
latitude => '41.9173012'
sellertype => 'Dealer'
source => 'Mcgrath Honda Of St Charles (DriveChicago.com)'
zipcode => '60174'
image_width => '112'
}
{
create_time => 1201699886
title => '2008 Honda Civic Hybrid 4dr Sdn'
trim => 'trim_hybrid:Hybrid'
privateparty => '0'
paid => TRUE (boolean)
condition => 'New'
id => '745152518'
longitude => '-87.9338989'
price => '23235.0'
locationcity => 'Gurnee'
url => 'http://www.oodle.com/rx/745152518-450p1025-TEST/www.vehix.com/SearchEngineMarketing.redirector?zip=60031&make=Honda&model=Civic%20Hybrid&year=2008&inventoryid=77000000D70BC7&To=Inventory%20Details&Vendor=Oodle&cid=49'
location => 'Gurnee'
registration => '0'
has_photo => 'No'
make => 'Honda'
color => 'Blue'
year => '2008'
category_attr => 'vehicle/car/sedan'
mileage => '11'
category_name => 'Sedans'
model => 'Civic'
sellertype => 'Dealer'
latitude => '42.3742981'
source => 'Rosen Honda (Vehix.com)'
zipcode => '60031'
}
]
}
