Platinum Partner
netbeans,real estate,jax-ws,web service,java ee 5,zillow,pronetbeans.com

Consuming Zillow Web Services in NetBeans 6.1 Beta

With the release of NetBeans 6.1 Beta, a number of new web services have been added to the listing under the Web Services node in the Services window. One of them that caught my eye was the Zillow web service.

For those readers in the United States, Zillow.com is an interesting real estate site. It can provide an estimate on the value of a home, display home values for homes in your neighborhood, as well as allowing you to see recent home sales in your area. With a MapQuest/Google-style map interface you can zoom in & out, pan around your neighborhood, and more.

For those folks who would love to be able provide access to this data, especially if you run a small real estate firm, you can pull data through the Zillow API network.

To get started, sign up here. You need to register with Zillow, which is free, to obtain a Zillow API key. This is a pass-token Zillow uses to validate your access to the APIs and track number of API calls per day, to keep traffic sensible.

You can then quickly and easily use NetBeans 6.1 to consume the Zillow services. Open NetBeans 6.1 and create a new Java Web Application that uses Java EE 5.

Once the application is displayed in NetBeans, right-click the project name in the Projects window and select New >> Java Class. Name the class something appropriate, such as GetZillowData, enter a package hierarchy, such as com.pronetbeans.zillow, and click the Finish button.

Your new class will open in the Source Editor and look like this (Javadoc & comments removed for brevity) :

package com.pronetbeans.zillow;

public class GetZillowData {

}

Next create a new method such as :

public String getData() {

String result = "";

return result;
}

Click the tab for the Services window and expand the Web Services node. You will see a list of web services. Locate the Zillow node and expand it until you see the specific API method names. The services window should look like Figure 1.

List of web services in the NetBeans 6.1 Services window
Figure 1. List of web services in the Services window.

For this article, we will use the GetRegionChart method. This method accepts various parameters (such as city, state, zip) and returns a URL to a chart that displays house pricing information that matches the parameters passed in.

Click the getRegionChart node, as shown in Figure 1, and drag it over to the Source Editor that shows the GetZillowData class. Place your mouse inside the getData method between the between these two lines :

String result = "";

return result;

Then release the mouse. NetBeans will process for a moment and then display the Customize GET saas Service window, as shown in Figure 2

Customize GET saas Service window in NetBeans 6.1

Figure 2. The Customize GET saas Service window in NetBeans 6.1.

Click the OK button. NetBeans will then process and generate code for the new web service. The following code will be added to the getData method :

try {
String unitType = "";
String city = null;
String state = null;
String zIP = null;
String width = null;
String height = null;
String chartduration = null;

String result = ZillowService.getGetRegionChartResource(unitType, city, state,
zIP, width, height, chartduration);
System.out.println("The SaasService returned: "+result);
} catch (java.io.IOException ex) {
ex.printStackTrace();
}

In my GetZillowData class, I edit the getData method and remove the Strings that were generated (unitType, city, state, zIP, width, height, chartduration) and make them method parameters instead. This way I can pass in the values from a JSP and return the resultant URL for display.

Since the web service returns XML, I need to add code to quickly parse out the URL that I need to view the chart. This is definitely a hack for purpose of this article, so please don't rely on it. I use a combination String.substring and String.indexOf to figure out where the URL appears in the resultant XML. The return value typically contains this snippet:

<url>http://url-to-chart</url>

Knowing this I can use this code :

String subs1= result.substring(result.indexOf("<url>http://"));

result = subs1.substring(subs1.indexOf("http://"), subs1.indexOf("</url>"));

This will retrieve the HTTP URL that appears between the <url> and </url> tags in the XML. I then set that result to the result return variable.

The entire method looks like this :

public String getData(String unitType, String city, String state,
String zIP, String width, String height, String chartduration) {

String result = "";

try {

result = ZillowService.getGetRegionChartResource(unitType,
city, state, zIP, width, height, chartduration);

String subs1= result.substring(result.indexOf("<url>http://"));

result = subs1.substring(subs1.indexOf("http://"), subs1.indexOf("</url>"));

} catch (IOException ex) {
ex.printStackTrace();
}

return result;
}

NetBeans has generated the ZillowService class along with others. Go to the Projects window and expand the Source Packages node in your web application project. You should see the following classes that were generated:

org.netbeans.saas.RestConnection
org.netbeans.saas.zillow.ZillowAuthenticator
org.netbeans.saas.zillow.ZillowService
org.netbeans.saas.zillow.profile.properties

The RestConnection class is a utility class that handles REST-based calls to the web service. It contains code to handle connecting to the web service URL, encoding parameters, and so on.

The ZillowAuthenticator class is a small utility class that handles reading the value of your Zillow.com API Web Service key from the profile.properties file.

The ZillowService class contains the static utility method, getGetRegionChartResource. This method accepts the parameters you need to pass to Zillow to return the necessary data. It also lists the actual URL to the web service you will call.

public static String getGetRegionChartResource(String unitType, String city,
String state, String zIP, String width, String height,
String chartduration) throws IOException {

String result = null;
String apiKey = ZillowAuthenticator.getApiKey();
String[][] queryParams = new String[][]{{"zws-id", "" + apiKey + ""}, {"unit-type", unitType}, {"city", city}, {"state", state}, {"ZIP", zIP}, {"width", width}, {"height", height}, {"Chartduration", chartduration}};

RestConnection conn = new RestConnection
("http://www.zillow.com/webservice/GetRegionChart.htm", queryParams);

result = conn.get(null);
return result;
}

*** Note : When NetBeans generates the URL in the RestConnection constructor above, it actually generates this code :

RestConnection conn = new RestConnection("http://www.zillow.com/webservice/GetDeepSearchResults.htm/"
+"GetDeepComps.htm/GetComps.htm/"
+ "GetChart.htm/GetRegionChart.htm
", queryParams);

I added the lines breaks and concatenation to keep the code from running off the margin in this article. Notice that the URL contains multiple web pages references separated by slashes. I'm not sure if this is a typo in the template that is used by NetBeans, or is intended, but I could not get the web service to work correctly until I removed the extra Get***.htm references in the URL and made it match the URL for the method I actually needed : http://www.zillow.com/webservice/GetRegionChart.htm.

The parameters passed to the getGetRegionChartResource method are defined as follows :

Parameter Description Required
zws-id The Zillow Web Service Identifier; each subscriber to Zillow Web Services is uniquely identified by an ID sequence and every request to Web services requires this ID Yes
city The name of a city No
state The two-letter abbreviation for a state No
ZIP The 5-digit ZIP code No
unit-type A string value that specifies whether to show the percent change, parameter value of "percent," or dollar change, parameter value of "dollar" Yes
width An integer value that specifies the width of the generated image; the value must be between 200 and 600, inclusive No
height An integer value that specifies the height of the generated image; the value must be between 100 and 300, inclusive No
Chartduration The duration of past data that needs to be shown in the chart. Valid values are "1year", "5years" and "10years". If unspecified, the value defaults to "1year". No

Next, you need to add your Zillow API key to the profile.properties file. Open it in the Source Editor and set the api_key property to your API key you obtain from your Zillow account.

Finally, I create a JSP page that can pass in the parameters to the GetZillowData class and use the URL that is returned to display the chart.

I define the parameters and their values, instantiate an instance of the GetZillowData class, pass in the parameters and retrieve the URL. I then use that return value as the src attribute for an img tag. Once again, not the best solution, but good enough for a quick demo. The entire JSP should look like this :

<html>
<head>
<title>Display Zillow Region Chart</title>
</head>
<body>
<%
String unitType = "dollar";
String city = "Albany";
String state = "NY";
String zIP = null;
String width = "400";
String height = "300";
String chartduration = "10years";

GetZillowData zillData = new GetZillowData();

String url = zillData.getData(unitType, city, state, zIP, width, height, chartduration);

%>
<br><br>Zillow Region Data<br><br>
<img src="<%= url%>" border="0" alt="Zillow Chart"/>
</body>
</html>

When you run the application the JSP page should look similar to Figure 3.

Zillow GetRegionChart chart displayed.
Figure 3. The Zillow Region Chart displayed in the JSP.

You could easily enhance this by creating form fields on the JSP that allows you to enter the parameters once the JSP page is displayed and post the form to display the chart. There are a variety of additional Zillow API calls you can make.

Resources :

Full Project Source Code (zipped NetBeans 6.1 project) : http://www.pronetbeans.com/article-files/ProNetBeans-WebApplication1.zip

NetBeans 6.1 Beta : http://www.netbeans.org/community/releases/61/

Zillow : http://www.zillow.com

Zillow API Overview : http://www.zillow.com/howto/api/APIOverview.htm

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}