Adam has posted 5 posts at DZone. View Full User Profile

Consuming Zillow Web Services in NetBeans 6.1 Beta

03.10.2008
| 14167 views |
  • submit to reddit

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

References
Published at DZone with permission of its author, Adam Myatt. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Walaa Saad replied on Fri, 2008/12/05 - 6:04pm

Hi,I have tried to follow tutorial myself but i found that the generated code in getData is like : try { String unittype = "dollar"; String city = null; String state = null; String zIP = null; String width = null; String height = null; String chartduration = null; RestResponse result = ZillowRealEstateService.getRegionChart(unittype, city, state, zIP, width, height, chartduration); zillow.realestateservice.regionchart.Regionchart resultObj = result.getDataAsObject(zillow.realestateservice.regionchart.Regionchart.class); //TODO - Uncomment the print Statement below to print result. //System.out.println("The SaasService returned: "+result.getDataAsString()); } catch (Exception ex) { ex.printStackTrace(); }And when i tried to change the result to a string , it do not accept saying incompatible type. So what is Wrong??Also when i tried to download the zipped version , it didnot run saying exception org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 2219: 20: GetZillowData zillData = new GetZillowData();21: 22: String url = zillData.getData(unitType, city, state,23: zIP, width, height, chartduration);24: 25: %> Thanks for your efforts

Jeff Rubinoff replied on Sat, 2008/12/06 - 2:38pm in response to: Walaa Saad

This is quite an old article and both NetBeans IDE and the Zillow SaaS have changed quite a bit since it was published. I suggest trying the tutorial version at http://www.netbeans.org/kb/docs/websvc/zillow.html.

Dilanja kodi replied on Sun, 2009/04/19 - 12:17am

try {
String unittype = "dollar";
String city = null;
String state = null;
String zIP = null;
String width = null;
String height = null;
String chartduration = null;
RestResponse result = ZillowRealEstateService.getRegionChart(unittype, city, state, zIP, width, height, chartduration);
try {
zillow.realestateservice.regionchart.Regionchart resultObj = result.getDataAsObject(zillow.realestateservice.regionchart.Regionchart.class);
 //zillow.realestateservice.regionchart does not exists

//out.println("The SaasService returned: "+result.getDataAsString());
} catch (Exception ex) {
ex.printStackTrace();
}
 

 

I tried this tutorial, however I got a compilation error saying. "package zillow.realestateservice.regionchart does not exists" . Can somebody help me out?

Eugenios Ampelius replied on Wed, 2013/07/24 - 2:29am

 I know of a folk who wanted to know if he can see inside of the "classroom" with Google Maps, because he was a "teacher" doing an "experiment". Right... That man had too much Dish network Tucson and therefore probably imagined that the satellites will work for him in his attempt to see what other places he can view aside classrooms, if you catch my drift.

Cata Nic replied on Fri, 2013/09/06 - 2:47am

 The browser seems to run the code from the comments. I've seen some warnings generated my the code.

Fernando Soares replied on Mon, 2014/01/20 - 6:18pm

 This page is generating some weird javascript alerts upon page load. I think there is a security breach in the comments sanitization. Better take a look at it! -Fernando Cavendish

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.