help with web services and arraylist as parameter
Hi,
I'm trying to create a Web Service able to give back a list of movies directed by a director whose name has been provided. The WS accesses to a database where the movies and its directors are kept.
The class "BuscadorPelicula3" adds the list of movies into an ArrayList (I started using a String[] instead but it caused a lot of errors).
And the client (the class "ClientePelicula2" ) gives the name of the director and asks for this list.
The problem appears when using the command call.setReturnType(?).
If I write XMLType.SOAP_ARRAY and then make a cast:
call.setReturnType (XMLType.SOAP_ARRAY);
ArrayList ret = (ArrayList) call.invoke(new Object []{ nombreDirector});
I get the following error:
-> Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
I have also tried:
call.setReturnType (XMLType.SOAP_ARRAY);
Object ret = call.invoke(new Object []{ nombreDirector});
but the error then comes later when trying to print "ret":
System.out.println(ret);
-> [Ljava.lang.Object;@1630ab9
even changing it to a String before:
resultado = ret.toString();
System.out.println(resultado);
the error remains the same...
What am I doing wrong? Should I use something else instead of an ArrayList?
Lots of thanks for your help.
(Both classes are written next)
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
import javax.xml.rpc.soap.SOAPFaultException;
import java.util.*;
[b]publicclass ClientePelicula2{[/b]
publicstaticvoid main(String [] args)throws Exception{
String endpoint ="http://localhost:" + 8080 +"/axis/BuscadorPelicula3.jws";
if (args ==null){
System.err.println("Uso Correcto: ClientePelicula buscarPeliculaPorDirector \"Nombre completo del director\" ");
return;
}
String method = args[0];
String nombreDirector="";
//String resultado ="";
for (int i=1; i<args.length; i++){
nombreDirector= nombreDirector.concat(args[i]+" ");
}
Service service =new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint) );
call.setOperationName( method );
call.addParameter("nombreDirector", XMLType.XSD_STRING, ParameterMode.IN );
call.setReturnType( XMLType.SOAP_ARRAY);
Object ret = call.invoke(new Object []{ nombreDirector});
//ArrayList ret = (ArrayList) call.invoke( new Object [] { nombreDirector});
//resultado = ret.toString();
System.out.println("La/s pelicula/s del director " + nombreDirector +" son:");
System.out.println(ret);
}
}
/*
Web Service que permite acceder a una base de datos MySQL
(Those lines I wrote when using String[] instead of ArrayList are commented.)
*/
import java.sql.*;
import java.util.*;
[b]publicclass BuscadorPelicula3{[/b]
//public String[] buscarPeliculaPorDirector (String args) throws Exception {
public ArrayList buscarPeliculaPorDirector (String args)throws Exception{
String login ="root";
String password ="semantica";
String url ="jdbc:mysql://localhost/piloto";
//String todas[]={};
ArrayList todas =new ArrayList();
Connection conn =null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url,login,password);
if (conn !=null){
Statement stmt = conn.createStatement();
ResultSet res = stmt.executeQuery("SELECT titulo FROM peliculas WHERE director=\""+args+"\"");
res.last();
int longitud= res.getRow();
//todas = new String [longitud];
res.first();
for (int k=0; k><longitud; k++){
String titulo = res.getString("titulo");
//todas [k]= titulo;
todas.add(titulo);
res.next();
}
res.close();
stmt.close();
conn.close();
}//end if
}//end try
catch(SQLException ex)
{
System.out.println(ex);
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}
return todas;
}//end method
}//end class
[code]
[/code]>

