NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

[Java Web Service <-> J2ME with Web Service Client ] Problem with return object

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
MestwinII



Joined: 08 Jan 2010
Posts: 7

PostPosted: Sat Jan 09, 2010 5:44 pm    Post subject: [Java Web Service <-> J2ME with Web Service Client ] Problem with return object Reply with quote

Hello!

I have a problem and I can not solve it. I create Java Web Application and Web Service. I add "gym" class to project:

Code:

package Classes;


public class gym {

    private int id = 0;
    private String name = null;

    public gym(){ }

    public gym(int id, String name)
    {
        this.id = id;
        this.name = name;
    }

    public void setId(int newValue) {
        id = newValue;
    }

    public void setName(String newValue) {
        name = newValue;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}


and WebMethod to WebService

Code:
  @WebMethod(operationName = "getUserGym")
    public gym getUserGym() {
        gym firstGym = new gym();
        int a = 7;
        String b = "test";
        firstGym.setId(a);
        firstGym.setName(b);
        return firstGym;
    }


In J2ME application I add new Java Me Web Service Client. Enter WSDL URL. Everything without any problems. I add instance of Stub Client and call method.

Code:

    private myGymWSService_Stub myGymWsClient = new myGymWSService_Stub();

   gym gt = new gym();
   gt = myGymWsClient.getUserGym();


and here is a problem


Code:

java.lang.ClassCastException
        at mygymws.myGymWSService_Stub.gym_fromObject(myGymWSService_Stub.java:137)
        at mygymws.myGymWSService_Stub.getUserGym(myGymWSService_Stub.java:123)
        at main.myGymMIDlet.getGym(myGymMIDlet.java:669)
        at main.myGymMIDlet.ifGymMethod(myGymMIDlet.java:562)
        at main.myGymMIDlet.MenuAction(myGymMIDlet.java:456)
        at main.myGymMIDlet.commandAction(myGymMIDlet.java:146)
        at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(Display.java:2093)
        at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(Display.java:2929)
        at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(DefaultEventHandler.java:297)
        at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(AutomatedEventHandler.java:667)
        at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(DefaultEventHandler.java:711)
        at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(DefaultEventHandler.java:608)


I would like to add that when you send an object from MIDlet to WebService everything was fine

exception indicates an error at this point:

Code:
    private static gym gym_fromObject( Object obj[] ) {
        if(obj == null) return null;
        gym result = new gym();
        result.setId(((Integer )obj[0]).intValue());
        result.setName((String )obj[1]);
        return result;
    }


It looks like that does not return an object but where is the error, since most of the code is generated automatically? Please help me!
Back to top
spacewatcher



Joined: 11 Oct 2009
Posts: 15

PostPosted: Sat Jan 09, 2010 7:56 pm    Post subject: It's embarrassing and have a lot of bugs. Reply with quote

Don't rely on the jax-rpc stub generated by neatbeans. It's embarrassing and have a lot of bugs.
Try replacing (Object[]) resultObj by (Object[])(((Object[])resultObj)[0])
on the last line of all methods on the stub (the file that ends with "_Stub")
You will need to fix the code of the methods to work, and you will need to learn how the stub is generated (o should be generated).
Read the invoke method in

http://www.forum.nokia.com/document/Java_Developers_Library_v2/GUID-233D0FA4-C649-4220-829B-5CEA8B6559DD/javax/microedition/xml/rpc/Operation.html

It will help you.
I hope that it help[
Back to top
MestwinII



Joined: 08 Jan 2010
Posts: 7

PostPosted: Sat Jan 09, 2010 8:35 pm    Post subject: Reply with quote

Thank you very much! It works! I thought that is the fault of the generator, but I am a beginner in this subject and I couldn't solve this problem. Once again thank you very much! Smile
Back to top
MestwinII



Joined: 08 Jan 2010
Posts: 7

PostPosted: Mon Jan 11, 2010 12:13 pm    Post subject: Reply with quote

One more question. There is a problem with return array of object.

Code:
java.rmi.MarshalException: Invalid Element in Response: exception


in here

Code:
public machine[] getGymMachines(int gymId) throws java.rmi.RemoteException {
        Object inputObject[] = new Object[] {
            new Integer(gymId)
        };

        Operation op = Operation.newInstance( _qname_operation_getGymMachines, _type_getGymMachines, _type_getGymMachinesResponse );
        _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 machine_ArrayfromObject((Object[])(((Object[])resultObj)[0]));
    }


What should I improve it? I will be grateful for help!
Back to top
spacewatcher



Joined: 11 Oct 2009
Posts: 15

PostPosted: Mon Jan 11, 2010 2:07 pm    Post subject: it's another bug Reply with quote

Paste the code of the machine class, and the machine_arrayfromobject method
I guess it's another bug . Rolling Eyes
Back to top
MestwinII



Joined: 08 Jan 2010
Posts: 7

PostPosted: Mon Jan 11, 2010 4:13 pm    Post subject: Reply with quote

machine class:

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Classes;

public class machine {

    private int id = 0;
    private String name = null;
    private String type = null;
    private String info = null;
    private String movie = null;

    public machine()
    {

    }
    public machine(int id, String name, String type, String info, String movie)
    {
        this.id = id;
        this.name = name;
        this.type = type;
        this.name = info;
        this.movie = movie;
    }

    public void setId(int newValue) {
        id = newValue;
    }

    public int getId() {
        return id;
    }

    public void setName(String newValue) {
        name = newValue;
    }

    public String getName() {
        return name;
    }

    public void setType(String newValue) {
        type = newValue;
    }

    public String getType() {
        return type;
    }

    public void setInfo(String newValue) {
        info = newValue;
    }

    public String getInfo() {
        return info;
    }

    public void setMovie(String newValue) {
        movie = newValue;
    }

    public String getMovie() {
        return movie;
    }
}



and machine_arrayfromobject

Code:
 private static machine[] machine_ArrayfromObject( Object obj[] ) {
        if(obj == null) return null;
        machine result[] = new machine[obj.length];
        for( int i = 0; i < obj.length; i++ ) {
            result[i] = new machine();
            Object[] oo = (Object[]) obj[i];
            result[i].setId(((Integer )oo[0]).intValue());
            result[i].setInfo((String )oo[1]);
            result[i].setMovie((String )oo[2]);
            result[i].setName((String )oo[3]);
            result[i].setType((String )oo[4]);
        }
        return result;
    }


This function probably is not called because the error appears in the resultObj = op.invoke( inputObject );

Maybe an error is in the WebMethod?

Code:
    /**
     * Web service operation
     */
    @WebMethod(operationName = "getGymMachines")
    public machine[] getGymMachines(@WebParam(name = "gymId")
    int gymId) {
        machine[] mach = new machine[2];
        mach[0].setId(1);
        mach[0].setName("example1");
        mach[0].setInfo("lalalalalalalalalallalalalalallalalalala");
        mach[0].setMovie("http://www.aaaa.com");
        mach[1].setId(2);
        mach[1].setName("example");
        mach[1].setInfo("lalalalalalalalalallalalalalallalalalala");
        mach[1].setMovie("http://www.aaaa.com");
        return mach;
    }


Thanks for reply.[/quote]
Back to top
spacewatcher



Joined: 11 Oct 2009
Posts: 15

PostPosted: Mon Jan 11, 2010 5:09 pm    Post subject: another bug Reply with quote

There are problems when transporting types int and byte.
Try use Integer in the id of machine (not int). Then if after change that, there are
a ClassCastException, try replacing :
result[i].setId(((Integer )oo[0]).intValue());
by
result[i].setId( new Integer( oo[0] ) )
in machine_arrayFromObject

I'm not sure if it is the problem you have, but i'm sure that int cause problems.
i hope it help you.
Back to top
MestwinII



Joined: 08 Jan 2010
Posts: 7

PostPosted: Mon Jan 11, 2010 6:35 pm    Post subject: Reply with quote

Unfortunately, it doesn't work Sad I tried to send a array of object consisting only of String, but the same exception occurred. In spite of all thank you very much for help.
Back to top
MestwinII



Joined: 08 Jan 2010
Posts: 7

PostPosted: Mon Jan 11, 2010 9:52 pm    Post subject: anoder bug: Resolved Reply with quote

Resolved!
Just as I thought the problem was in the WebMethod.
It should be something like this:
Code:
@WebMethod(operationName = "getGymMachines")
    public machine[] getGymMachines(@WebParam(name = "gymId")
    int gymId) {
        machine[] mach = new machine[2];
       
        machine mach1 = new machine();
        mach1.setId(1);
        mach1.setName("example1");
       
        mach[0] = mach1;
        machine mach2 = new machine();
        mach2.setId(2);
        mach2.setName("exemple2");
       
        mach[1] = mach2;



        //-------- NOT LIKE THIS!------------------
        //mach[0].setId(1);
        //mach[0].setName("example1");
        //
        //mach[1].setId(2);
        //mach[1].setName("example2");
        //--------------------------------------------
        return mach;
    }


spacewatcher thanks for your help in solving the case!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo