Easy Debugging Of Webservice Calls

Copied with permission from blog post by Eric Coolman, all rights reserved

Notice that you can debug most webservice calls with the Simulators Network Monitor tool which works rather well. This article is mostly applicable for lower level networking issues and on-device debugging.

I just want to share a tip that will make life much easier when dealing with webservices. Often, developers will start by blindly making API calls to inspect the headers and payload to determine what they are receiving from the webservice. There is a much easier way that does not involve modifying your code at all, nor is there a great deal of setup, it takes about 2 minutes total.

A tool has been around for the last decade that has been included with the Apache Axis Webservice toolkit, but is now packaged as a standalone product. The tool is a simple Java Swing App, that couldn’t be easier to use. Click the download here:

http://ws.apache.org/commons/tcpmon/download.cgi

Unzip the package, and start it by going into the build directory and executing tcpmon.bat (in Windows) or tcpmon.sh (in Unix). You will be prompted with the screen on the right.

Simply define a port number (common practice is to pick a number above 8000, but don’t use 8080, it’s overused), I chose 8888 above. Then click Proxy, then click Add. A new tab will open with the name ‘Port 8888‘. Click on that, and you will see it is waiting for connections.

That is all the setup needed for TCPMon, from here on, you are only using it to see the data going back and forth between your mobile device, and the webservice you are calling.

Back to Codename One. We want to tell our app to make all connections through a proxy. Scroll down for instructions on how to do it for the various platforms.

Now when you run your app, you will see all the traffic between your mobile and the webservice. That’s it!

Setting a network proxy for Codename One Simulator on Eclipse

In Eclipse, click on the Simulator_*.launch file that is at the root of your project. Now click Run, then Run Configurations, then ensure your Simulator launcher is selected on the left, and click on the Arguments tab.

The key here is in the VM arguments section, you want to add the arguments:

-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888

where 8888 is the port you chose in TCPMon above. In this case, I’m using the loopback (127.0.0.1), but you can easily run the TCPMonitor on another machine, and set the proxyHost to the IP address of that machine.

Setting a network proxy for Codename One Simulator on Netbeans

In Netbeans, right click on your project, then click Properties then click Run.

The key here is in the VM options section, you want to add the arguments:

-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888

where 8888 is the port you chose in TCPMon above. In this case, I’m using the loopback (127.0.0.1), but you can easily run the TCPMonitor on another machine, and set the proxyHost to the IP address of that machine.

Setting a network proxy for debugging webservice calls on a Physical device

NOTE: This method will only work on non-CLDC devices, and only if you unrestrict your Java environment, which by default is restricted to CLDC1.1 compliance, to support the broad range of devices. This is the subject of another future blog post.

NOTE 2: The configuration below will work as-is if the device has WiFi capabilities. For devices that don’t, use a dynamic DNS service such as No-IP, which may involve some setup on your router. Again, out of the scope of this tutorial.

In your application init method, you want to add the following 2 VM environment variables:

System.setProperty("http.proxyHost", "192.168.1.10"); System.setProperty("http.proxyPort", "8888");

where the host is the IP address of the machine running the TCPMon tool, and the Port is the Listening Port you chose in the TCPMonitor tool when activating the proxy.

In a handcoded Codename One app, this is usually done within the init(Context) method. In a GuiBuilder based application, this is usually done within the StateMachine initVars() method.

Alternatives: You can also set the proxy address on the device itself without code, but the drawback (or benefit) of this method is that all HTTP traffic will be proxied through the TCP mon app: