Using Java application to access web application

I’ve been working a bit with needing to utilise a web service built in PHP from a Java application (it would be easier to just run a full-screen browser, but other constraints remove this option). What this has necessitated is scripting the Java app so that it makes calls to the web app’s API and then parses and utilises the data correctly.

Other than the writing of variations to the existing web app, so that function calls etc are accessed externally and correctly, the main functionality of the Java app has needed to be built into standard function calls which take in parameters and then parse them to dynamically create the correct URL to be called. Essentially the generalised function takes the parameters given and creates a URL which specifies the function required as well as necessary input data for the web app. Unfortunately, Java doesn’t have a nice foreach array iterator that can reference both the key and the value; like PHPs foreach($array as $key=>$val)):

public string makeURL(string baseURL, string pageName, string[][] params) {
    string queryURL = baseURL + "/" + pageName + "?";
    for(int i=0; i<params.length(); i++) {
	string param = params[i][0] + "=" + params[i][1] + "&";
	queryURL += param;
    queryURL = queryURL.subString(0, queryURL.length() - 1);
    return queryURL;

Next we use a function call to wrap up all the creation of a URL, requesting it, and receiving the response:

public string callURL(string queryURL) { 
    URLConnection queryConn = queryURL.openConnection();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
    String inputLine;
    String result = ""; 
    while ((inputLine = in.readLine()) != null) {
	result += inputLine;
    return result;

The second general function is given the returned data from the URL request and parses it into variables for use by the Java app; the data returned is structured as a 2-D array with the equivalent of a key and value held in the 2nd dimension:

public string[] extractData(string response, string[] nodeNames = null) { 
    string[] data = new Array();
    string[] tokens = StringUtils.splitspanserveAllTokens(response, ":");
    for(int i=0; i<tokens.length(); i++) {
	string[] step = StringUtils.splitspanserveAllTokens(tokens[i], "$");
	data[i][0] = step[0];
	data[i][1] = step[1];
    return data; 

Of course, my extractData function is extremely simplistic and only splits a string up. Parsing XML is a better solution if the URL returns XML formatted data. This would require either explicit foreknowledge of the exact nodes being returned by a request (I have allowed for this with the string[] nodeNames) or a much bigger function to allow for searching through and detecting all node names and then dynamically iterating them.

I’m sure there are cleaner, and better, implementations of this, however I felt the need to build my own since I’m dabbling back into Java at present. And once it’s built, I believe it’s best to share. Hopefully this helps someone, let me know.


About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

One Response to Using Java application to access web application

  1. Pingback: Java equivalent of PHP foreach ($array as $key=>$val) « whileloop

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: