Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 35
Registered: ‎10-11-2008
My Device: Not Specified

Web Service using JSR-172 array String array cast error after successful operation invoke

Hi,  my environment is

 

JDE 4.5.0.0

Curve 8300 (EDGE) v 4.5.0.102 ( platform 2.7.0.85 )

GlassFish 2.1

 

I wrote a Web Service using jsr-172 to send back an array of objects of the type testBonifica

 

 

public class testBonifica { private String oferta; private String descripcion; private String criterio; private int multiplo; private int minimo; private int cantidad; private String[] codigo; private boolean exclusiva; ..... public getters and setters

}

 

 After invoke the operation, it returns successfully.  Here is the Stub that process the call to the Web Service:

 

import javax.xml.rpc.JAXRPCException; import javax.xml.namespace.QName; import javax.microedition.xml.rpc.Operation; import javax.microedition.xml.rpc.Type; import javax.microedition.xml.rpc.ComplexType; import javax.microedition.xml.rpc.Element; public class ofertaTestService_Stub implements ofertaTestService, javax.xml.rpc.Stub { private String[] _propertyNames; private Object[] _propertyValues; public ofertaTestService_Stub() { _propertyNames = new String[] { ENDPOINT_ADDRESS_PROPERTY }; _propertyValues = new Object[] { "http://www.myserver.com:8080/ofertaTestService/ofertaTest" }; } public void _setProperty( String name, Object value ) { int size = _propertyNames.length; for (int i = 0; i < size; ++i) { if( _propertyNames[i].equals( name )) { _propertyValues[i] = value; return; } } String[] newPropNames = new String[size + 1]; System.arraycopy(_propertyNames, 0, newPropNames, 0, size); _propertyNames = newPropNames; Object[] newPropValues = new Object[size + 1]; System.arraycopy(_propertyValues, 0, newPropValues, 0, size); _propertyValues = newPropValues; _propertyNames[size] = name; _propertyValues[size] = value; } public Object _getProperty(String name) { for (int i = 0; i < _propertyNames.length; ++i) { if (_propertyNames[i].equals(name)) { return _propertyValues[i]; } } if (ENDPOINT_ADDRESS_PROPERTY.equals(name) || USERNAME_PROPERTY.equals(name) || PASSWORD_PROPERTY.equals(name)) { return null; } if (SESSION_MAINTAIN_PROPERTY.equals(name)) { return new Boolean(false); } throw new JAXRPCException("Stub does not recognize property: " + name); } protected void _prepOperation(Operation op) { for (int i = 0; i < _propertyNames.length; ++i) { op.setProperty(_propertyNames[i], _propertyValues[i].toString()); } } public testBonifica[] probarBonifica(long fechaDeCompra, String codigoSku, String codigoCliente, double pCantidad, String token) throws java.rmi.RemoteException { Object inputObject[] = new Object[] { new Long(fechaDeCompra), codigoSku, codigoCliente, new Double(pCantidad), token }; Operation op = Operation.newInstance( _qname_operation_probarBonifica, _type_probarBonifica, _type_probarBonificaResponse ); _prepOperation( op ); op.setProperty( Operation.SOAPACTION_URI_PROPERTY, "" ); Object resultObj; try { resultObj = op.invoke( inputObject ); } catch( JAXRPCException e ) { Throwable cause = e.getLinkedCause(); if( cause instanceof java.rmi.RemoteException ) { throw (java.rmi.RemoteException) cause; } throw e; } return testBonifica_ArrayfromObject((Object[])((Object[]) resultObj)[0]); } private static testBonifica[] testBonifica_ArrayfromObject(Object obj[]) { if (obj == null) { return null; } testBonifica result[] = new testBonifica[obj.length]; for (int i = 0; i < obj.length; i++) { result[i] = new testBonifica(); Object[] oo = (Object[]) obj[i]; result[i].setCantidad(((Integer) oo[0]).intValue()); result[i].setCriterio((String) oo[2]); result[i].setDescripcion((String) oo[3]); result[i].setExclusiva(((Boolean) oo[4]).booleanValue()); result[i].setMinimo(((Integer) oo[5]).intValue()); result[i].setMultiplo(((Integer) oo[6]).intValue()); result[i].setOferta((String) oo[7]); // it can not cast a String[] array Object[] codigos = (Object[]) oo[1]; String[] unCodigos = new String[codigos.length]; for (int idx = 0; idx < codigos.length; idx++) { unCodigos[idx] = (String) codigos[idx]; } result[i].setCodigo(unCodigos); } return result; } protected static final QName _qname_operation_probarBonifica = new QName( "http://comm.sales.myserver.com/", "probarBonifica" ); protected static final QName _qname_probarBonifica = new QName( "http://comm.sales.myserver.com/", "probarBonifica" ); protected static final QName _qname_probarBonificaResponse = new QName( "http://comm.sales.myserver.com/", "probarBonificaResponse" ); protected static final Element _type_probarBonifica; protected static final Element _type_probarBonificaResponse; static { _type_probarBonifica = new Element( _qname_probarBonifica, _complexType( new Element[] { new Element( new QName( "", "fechaDeCompra" ), Type.LONG ), new Element( new QName( "", "codigoSku" ), Type.STRING, 0, 1, false ), new Element( new QName( "", "codigoCliente" ), Type.STRING, 0, 1, false ), new Element( new QName( "", "pCantidad" ), Type.DOUBLE ), new Element( new QName( "", "token" ), Type.STRING, 0, 1, false )}), 1, 1, false ); _type_probarBonificaResponse = new Element( _qname_probarBonificaResponse, _complexType( new Element[] { new Element( new QName( "", "return" ), _complexType( new Element[] { new Element( new QName( "", "cantidad" ), Type.INT ), new Element( new QName( "", "codigo" ), Type.STRING, 0, Element.UNBOUNDED, true ), new Element( new QName( "", "criterio" ), Type.STRING, 0, 1, false ), new Element( new QName( "", "descripcion" ), Type.STRING, 0, 1, false ), new Element( new QName( "", "exclusiva" ), Type.BOOLEAN ), new Element( new QName( "", "minimo" ), Type.INT ), new Element( new QName( "", "multiplo" ), Type.INT ), new Element( new QName( "", "oferta" ), Type.STRING, 0, 1, false )}), 0, Element.UNBOUNDED, true )}), 1, 1, false ); } private static ComplexType _complexType( Element[] elements ) { ComplexType result = new ComplexType(); result.elements = elements; return result; } }

 

 

 In the description of the Element, you will see that property "codigo" is an String[] array.

 

These code works fine and I get the complete object.

 

If you  look at the method where the cast is made from the received objecto to testBonifica, at the end there is a for cycle to cast each array object to String object.

 

private static testBonifica[] testBonifica_ArrayfromObject(Object obj[]) {
if (obj == null) {
return null;
}
testBonifica result[] = new testBonifica[obj.length];
for (int i = 0; i < obj.length; i++) {
result[i] = new testBonifica();
Object[] oo = (Object[]) obj[i];
result[i].setCantidad(((Integer) oo[0]).intValue());
result[i].setCriterio((String) oo[2]);
result[i].setDescripcion((String) oo[3]);
result[i].setExclusiva(((Boolean) oo[4]).booleanValue());
result[i].setMinimo(((Integer) oo[5]).intValue());
result[i].setMultiplo(((Integer) oo[6]).intValue());
result[i].setOferta((String) oo[7]);
// it can not cast a String[] array
Object[] codigos = (Object[]) oo[1];
String[] unCodigos = new String[codigos.length];
for (int idx = 0; idx < codigos.length; idx++) {
unCodigos[idx] = (String) codigos[idx];
}
result[i].setCodigo(unCodigos);
}
return result;
}

 

If I try to cast directly with the following instruction, the program fails, and no Exception message is available.

 

 

result[i].setCodigo((String) oo[1]);

  

At some point I was able to find in the phone log a "classCastException"

 

 

The strange thing is that the direct cast can be done for int[] and double[] arrays.

 

Is this a bug or I am missing something ?   Please advise.

 

Thank you

 

 

Developer
Posts: 35
Registered: ‎10-11-2008
My Device: Not Specified

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

Sorry, there is a typo in the previous instruction that generates the error

 

it must read

 

 

result[i].setCodigo((String[]) oo[1]);

 thank you

 

 

 

New Developer
Posts: 1
Registered: ‎11-21-2009
My Device: Not Specified

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

Hello Antonio,

 

I had a question related to your post. I'm using NetBeans 6.7.1 and JSR-172 for generating the web service client from the valid WSDL. What I want to achieve is the following:

 

Send from the mobile an array of objetcs, to be stored in a DB in the back end. For example, I need to send an array of objects type Report (my class) and then the WS should be able to receive them.

 

I have created a service operation in the back-end which receives an Object as parameter (with the intention to receive and array of my objects, since an array is essentially an object). When I go to my mobile project and try to generate the WS Client, I get an error from the IDE saying "Unsupported tag in element parameter" to indicate that it can't generate the stubs from that valid WSDL.

 

Any ideas on what is going on??

 

Thank you!!

Developer
Posts: 35
Registered: ‎10-11-2008
My Device: Not Specified

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

Hello Juan Carlos,

 

sorry I got this too late, I never got a reply warning in my email.

 

I do not have experience with Netbeans 6.7, but you should check:

 

1.- The web service can not receive an "Object" type.  It must receive your "Report" class.

 

2.- Check the Web Services Specification to validate the kind of types you can send back and forth within your classes.  For example your "Report" class can include properties type int, long, String, etc. as a complexType, but never "Object" types.

 

 

 

 

New Member
Posts: 2
Registered: ‎03-29-2010
My Device: wert

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

as u written in function testBonifica_ArrayfromObject(), u will get an array on [0] index , which will give actual result.

As in return testBonifica_ArrayfromObject((Object[])((Object[]) resultObj)[0]);

once agin u have to do this obj = ((Object[])((Object[]) resultObj)[0]);

hope, this will help you.

but problem is this why gives as another array. i mean why we have to take it as 

return testBonifica_ArrayfromObject((Object[])((Object[]) resultObj)[0])

New Member
Posts: 2
Registered: ‎03-29-2010
My Device: wert

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

when stub is created end line of called finction is this in your stub

return testBonifica_ArrayfromObject((Object[])((Object[]) resultObj)[0]);

 

but i got this  return JqData_ArrayfromObject((Object[]) resultObj);

plz help me

Developer
Posts: 223
Registered: ‎06-08-2010
My Device: pearl8100

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

Hi antonio,

 

I am trying to to do a mysql connector in webservice it return a groups of values.So i am using vector/arraylist/hashtable.But nothing works it return same "unsupported tag in element return".Can u suggest me how can i return those values from database via webservice and invoke at my JavaME client.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Please press the kudos button on the left side to thank the user that helped you.
Developer
Posts: 35
Registered: ‎10-11-2008
My Device: Not Specified

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

Hi,

 

You can send only basic types on this kind of web services

Please review JSR-172 from jcp.org

To test a web service, the best way to start is sending a simple int of string parameter.

From there, you can grow up.

 

Developer
Posts: 223
Registered: ‎06-08-2010
My Device: pearl8100

Re: Web Service using JSR-172 array String array cast error after successful operation invoke

Hi, i got something in exploring as this.In this instead of using set get i passed my web param in callable statement

  @WebMethod(operationName = "Authentication")
    public  Authentication getAuthentication (@WebParam(name = "username")
    String username, @WebParam(name = "password")
    String password) {
                //TODO write your implementation code here:
         Authentication ack = null;

        try{
            CallableStatement cs = con.prepareCall(simpleProc);
            cs.setString(1, username);
            cs.setString(2, password);

} like this.But i cant get how to take result set values now.Since in class we cant take result set if we define things globally.So how to take those vlaues

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Please press the kudos button on the left side to thank the user that helped you.