A java client for the MapQuest web service

By: Anders Ohlsson

Abstract: A simple java web service consumer

While travelling, I wrote a very simple screen scraping web service for MapQuest. It's implemented in Delphi 6 and uses the Indy components to get a specified URL from www.mapquest.com. After retrieving the resulting page, it formats the driving directions in a simple string and sends it back to the requestor.

But, we really don't need to know how it's implemented, we just know that we found it on www.xmethods.com and that the URL for the WSDL is http://ww6.borland.com/webservices/MapQuest/MapQuest.exe/wsdl/IMapQuest.

If you fire up JBuilder 7, and if you have the WebServices Pack installed, go ahead and start a new project (File | New Project). Also add a new application (File | New Application).

Now, the important piece... Select File | New... | WebServices | Import WSL and paste the URL for the WSDL into the wizard. When you hit OK, JBuilder 7 will create a bunch of files in a package typically called "org.tempuri". To make sure everything works correctly, as far as binding to the service, and calling it, let's expand the package node and run the test case that got automatically generated (right click on the test case node and select Run Test).

If you want to test a real call and see the output, go ahead and modify the test case so that the calling lines look like this:

  value = binding.getDirections("39th and 11th","New York","NY","US","1535 Broadway","New York","NY","US");
  System.out.println(value);

If you run the test case again with the above changes, you should see complete directions from the Javits Center in New York to the New York Marriott Marquis on Times Square.

Creating a simple GUI that allows us to specify the "from" and "to" addresses is fairly trivial - I'll give you a screen shot of the layout and the running client below:


The layout of the client


The running client

For the button, we need some code. Type this:

  IMapQuest binding = new IMapQuestserviceLocator().getIMapQuestPort();
  jTextArea1.setText(binding.getDirections(tfFromAddress.getText(),tfFromCity.getText(),tfFromState.getText(),"US",
        tfToAddress.getText(),tfToCity.getText(),tfToState.getText(),"US"));

JBuilder 7 will complain on "IMapQuest" - let's use the new refactoring features of JBuilder 7 to fix the error - sweep over "IMapQuest" and hit the magnifying glass and select the package that it suggests. Do the same thing for "IMapQuestserviceLocator".

Now that the lines make syntactic sense, JBuilder 7 will complain about two unhandled exceptions. Select the whole block and right click - surround with try/catch.

Your code should now look like this:

  void jButton1_actionPerformed(ActionEvent e) {
    try {
      IMapQuest binding = new IMapQuestserviceLocator().getIMapQuestPort();
      jTextArea1.setText(binding.getDirections(tfFromAddress.getText(),
          tfFromCity.getText(),tfFromState.getText(),"US",tfToAddress.getText(),
          tfToCity.getText(),tfToState.getText(),"US"));
    }
    catch (RemoteException ex) {
    }
    catch (ServiceException ex) {
    }
  }

That's how easy it is to create a consumer for a web service in JBuilder 7. The code for this article can be found in CodeCentral.


Server Response from: ETNASC04