0

I'm developing an app where the phone (Android program) is the client trying to send data through a socket to a Java receiver program on my computer.

So far I've been able to send simple strings or whatever, but now I'm trying to send custom objects that I create. I should note that both programs are separate Eclipse projects, and I seem to be having trouble including the same custom class "Order" on the server side (even though I have Order.java in the src folder of the server project).

Some code, for reference: Server:

private void run() throws Exception {

    ServerSocket mySS = new ServerSocket(4443);     

    while(true) {

        Socket SS_accept = mySS.accept(); 

        InputStream is = SS_accept.getInputStream();  
        ObjectInputStream ois = new ObjectInputStream(is);  
        Order order = (Order) ois.readObject();  

        if (order!=null){
            java.util.List<String> items = order.getOrders(); 
            int chair = order.getChair();
            int table = order.getTable(); 
            double price = order.getPrice(); 
            System.out.println("Table: "+ table + " || Chair: " +chair);
            for(String food: items) {
                System.out.println(food); 
            }
            System.out.println("Price: $"+price); 

        }  
        is.close();  
        SS_accept.close();  
        mySS.close();

}

And the relevant part of the client:

try {
        mySocket = new Socket(serverService.ipAddress, serverService.port);
        os = mySocket.getOutputStream();  
        oos = new ObjectOutputStream(os);  
    } catch(Exception e) {
        Toast.makeText(PlaceOrder.this, "Error - not connected to server.", Toast.LENGTH_SHORT).show(); 
    }

try {
                oos.writeObject(order);
                Toast.makeText(PlaceOrder.this, "Order Submitted!", Toast.LENGTH_SHORT).show(); 
                refreshOrderPage(); //refresh page, allow waiter to make more orders
                oos.close();  
                os.close(); 
                mySocket.close(); 
            } catch (Exception e) {
                Toast.makeText(PlaceOrder.this, "Error - not connected to server.", Toast.LENGTH_SHORT).show();
            } 

Any ideas why I'm getting this error when trying to send objects through sockets?

Thanks.

3 Answers 3

2

You should probably set the serialVersionUID in the class, then build a jar with the shared classes and include that jar in both projects.

However, given you are using different JVMs (Oracle and Dalvik) there's no guarantee that the byte-level encoding is the same. You should either manually override the serialization using readObject/writeObject or use a different object encoding system that is guaranteed to be identical independent of the environment.

Sign up to request clarification or add additional context in comments.

Comments

0

A stack trace would help, but almost certainly you're serializing an object on one side, sending it to the other, and the other side doesn't have a class definition with which it can reconstruct the object. In this case, it sounds like maybe your Server doesn't know about the com.foo.Order class.

2 Comments

hi. It's weird, I don't know how else to include the file. I literally copy and pasted the code into a file called "Order.java" in the Server's project...
Is it in the same package on both the server and the client? Does the class get compiled and deployed/run along with all the other server classes?
0

You can also serialize object to some string format (json, yaml, xml) and pass it. It would much easier to maintain, I suppose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.