Skip to main content

sending an array of int to java web service from a java client in Eclipse

1 reply [Last post]
mikie
Offline
Joined: 2010-09-08
Points: 0

In the following, I create an XML-RPC client-server application, which consists of three parts: client.java, server.java and ActionHandler.java so that you add a new method which receives an array of numbers as parameter and returns the smallest value.

I am having problem sending an array of int to java web service from a java client in Eclipse and retrieving the smallest number from the web service

Client.java

//This is client.Client.java file.

package client;

import java.net.URL; import java.util.Vector;

import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;

public class Client {

@SuppressWarnings("unchecked") public static void main(String[] args) {

int numbers[] = { 1, 5, -9, 12, -3, 89, 18, 23, 4, -6 };

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

int minVal1, minVal2 = 0;

try {

config.setServerURL(new URL("http://127.0.0.1:" + Integer.parseInt(args[0]) + "/"));

XmlRpcClient client = new XmlRpcClient();

client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));

client.setConfig(config);

Object[] params = new Object[] { new String("Amanda") }; String msg = (String) client.execute("handlers.ActionHandler.getDate", params); System.out.println(msg);

// Create the request parameters using user input
Vector paramss = new Vector();
paramss.addElement(new Integer(numbers[0]));
minVal1 = (Integer) client.execute("handlers.ActionHandler.getMinValue", paramss);

// Find minimum (lowest) value in array using loop System.out.println("Minimum Value = " + minVal1);

//minVal2 = (Integer) client.execute("handlers.ActionHandler.minValue", paramss);

// Find minimum (lowest) value in array by sorting array //System.out.println("Minimum Value = " + minVal2);

} catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } } }

Server.java

//This is server.Server.java file

package server;

import java.io.IOException;

import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.server.PropertyHandlerMapping; import org.apache.xmlrpc.server.XmlRpcServer; import org.apache.xmlrpc.server.XmlRpcServerConfigImpl; import org.apache.xmlrpc.webserver.WebServer;

public class Server {

public static void main(String[] args) {

if (args.length < 1) {

System.out.println("Usage: java Server [port]"); System.exit(-1);

}

try {

WebServer webServer = new WebServer(Integer.parseInt(args[0]));

XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();

// Here we define a mapping of action handlers PropertyHandlerMapping phm = new PropertyHandlerMapping(); phm.addHandler(handlers.ActionHandler.class.getName(), handlers.ActionHandler.class); phm.addHandler(handlers.IActionHandler.class.getName(), handlers.IActionHandler.class);

// Here we append the handler mapping to the server xmlRpcServer.setHandlerMapping(phm);

XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig(); serverConfig.setEnabledForExtensions(true); serverConfig.setContentLengthOptional(false);

// Here we start the server using built-in version System.out.println("Attempting to start XML-RPC server...");

// webServer.setParanoid(false); webServer.start();

System.out.println("Server is started at port " + args[0]);

} catch (XmlRpcException e) {

System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage());

}

System.out.println("Now accepting requests. (Terminate the program to stop!)");

}

}

ActionHandler.java

//This is handlers.ActionHandler.java

package handlers;

import java.text.DateFormat; import java.util.Arrays; import java.util.Date;

public class ActionHandler implements IActionHandler { public String getDate(String name){

Date date = new Date(); return "Hello " + name + "! By the way, today is " + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date);

}

// Find minimum (lowest) value in array using loop public int getMinValue(int[] numbers) { int minValue = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < minValue) { minValue = numbers[i]; } } return minValue; }

// Find minimum (lowest) value in array using array sort public int minValue(int[] numbers) { Arrays.sort(numbers);
return numbers[0];
}
}

In the following, I create an XML-RPC client-server application, which consists of three parts: client.java, server.java and ActionHandler.java so that you add a new method which receives an array of numbers as parameter and returns the smallest value.

I am having problem sending an array of int to java web service from a java client in Eclipse and retrieving the smallest number from the web service

Client.java

//This is client.Client.java file.

package client;

import java.net.URL; import java.util.Vector;

import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;

public class Client {

@SuppressWarnings("unchecked") public static void main(String[] args) {

int numbers[] = { 1, 5, -9, 12, -3, 89, 18, 23, 4, -6 };

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

int minVal1, minVal2 = 0;

try {

config.setServerURL(new URL("http://127.0.0.1:" + Integer.parseInt(args[0]) + "/"));

XmlRpcClient client = new XmlRpcClient();

client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));

client.setConfig(config);

Object[] params = new Object[] { new String("Amanda") }; String msg = (String) client.execute("handlers.ActionHandler.getDate", params); System.out.println(msg);

// Create the request parameters using user input
Vector paramss = new Vector();
paramss.addElement(new Integer(numbers[0]));
minVal1 = (Integer) client.execute("handlers.ActionHandler.getMinValue", paramss);

// Find minimum (lowest) value in array using loop System.out.println("Minimum Value = " + minVal1);

//minVal2 = (Integer) client.execute("handlers.ActionHandler.minValue", paramss);

// Find minimum (lowest) value in array by sorting array //System.out.println("Minimum Value = " + minVal2);

} catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } } }

Server.java

//This is server.Server.java file

package server;

import java.io.IOException;

import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.server.PropertyHandlerMapping; import org.apache.xmlrpc.server.XmlRpcServer; import org.apache.xmlrpc.server.XmlRpcServerConfigImpl; import org.apache.xmlrpc.webserver.WebServer;

public class Server {

public static void main(String[] args) {

if (args.length < 1) {

System.out.println("Usage: java Server [port]"); System.exit(-1);

}

try {

WebServer webServer = new WebServer(Integer.parseInt(args[0]));

XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();

// Here we define a mapping of action handlers PropertyHandlerMapping phm = new PropertyHandlerMapping(); phm.addHandler(handlers.ActionHandler.class.getName(), handlers.ActionHandler.class); phm.addHandler(handlers.IActionHandler.class.getName(), handlers.IActionHandler.class);

// Here we append the handler mapping to the server xmlRpcServer.setHandlerMapping(phm);

XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig(); serverConfig.setEnabledForExtensions(true); serverConfig.setContentLengthOptional(false);

// Here we start the server using built-in version System.out.println("Attempting to start XML-RPC server...");

// webServer.setParanoid(false); webServer.start();

System.out.println("Server is started at port " + args[0]);

} catch (XmlRpcException e) {

System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage());

}

System.out.println("Now accepting requests. (Terminate the program to stop!)");

}

}

ActionHandler.java

//This is handlers.ActionHandler.java

package handlers;

import java.text.DateFormat; import java.util.Arrays; import java.util.Date;

public class ActionHandler implements IActionHandler { public String getDate(String name){

Date date = new Date(); return "Hello " + name + "! By the way, today is " + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date);

}

// Find minimum (lowest) value in array using loop public int getMinValue(int[] numbers) { int minValue = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < minValue) { minValue = numbers[i];
}
}
return minValue;
}

// Find minimum (lowest) value in array using array sort public int minValue(int[] numbers) { Arrays.sort(numbers); return numbers[0]; } }

IActionHandler.java

//This is handlers.IActionHandler.java

package handlers;

public interface IActionHandler {

public String getDate(String name);
public int getMinValue(int[] numbers); public int minValue(int[] numbers);
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mikie
Offline
Joined: 2010-09-08
Points: 0

ActionHandler.java

//This is handlers.ActionHandler.java

package handlers;

import java.text.DateFormat; import java.util.Arrays; import java.util.Date;

public class ActionHandler implements IActionHandler { public String getDate(String name){

Date date = new Date(); return "Hello " + name + "! By the way, today is " + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date);

}

// Find minimum (lowest) value in array using loop public int getMinValue(int[] numbers) { int minValue = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < minValue) { minValue = numbers[i]; } } return minValue; }

// Find minimum (lowest) value in array using array sort public int minValue(int[] numbers) { Arrays.sort(numbers); return numbers[0]; } }

Message was edited by: mikie