0

I'm trying to serialize a class Message from a java server and send them to an Android phone.

The phone hangs every time I try to open an ObjectInputStream object, without giving me any errors.

The encoding method -

public void sendMessagesToPhone(String userName) {

    System.out.println("Send messages to phone called");

    ObjectOutputStream outputObjects = null;

    ServerUser user = new ServerUser(userName, "");
    s.getKeys(user);


    user.setMessages();
    ArrayList<Message>  encryptedMessages = user.getMessages();
    try {
        //output = new DataOutputStream(socket.getOutputStream());
        outputObjects = new ObjectOutputStream(socket.getOutputStream());
        outputObjects.flush();

        for(Message m : encryptedMessages) {
            System.out.println(m);
            outputObjects.writeObject(m);


        }
    } catch (Exception e) {
        System.out.println("unable to create the streams");
        e.printStackTrace();
    }

try {
    outputObjects.close();

} catch (Exception e) {
    System.out.println("unable to close the streams");
    e.printStackTrace();
}
}
}

And the attempt to decode on the phone itself -

public void getNewMessages() {

        Socket connect = null;
        ArrayList<Message> incomingMessages = new ArrayList<Message>();
        ObjectInputStream input = null;
        PrintWriter pw = null;

        try {

            connect = new Socket("removed", 7777);
            //hangs here on creating the ObjectInputStream
            input = new ObjectInputStream(connect.getInputStream());
            pw = new PrintWriter(connect.getOutputStream(), true);

            //client parses text input and will respond to the pw input 
            //by calling the sendMessagesToPhone(String user) method
            pw.println("phonemessages");
            pw.println(currentUser.getName());

            Object obj = null;
            while((obj = input.readObject()) != null) {
                if(obj instanceof Message) {
                    incomingMessages.add((Message)obj);
                    }
                else toast = Toast.makeText(this, "object is not a        

    message", Toast.LENGTH_SHORT);
                toast.show();
            }

        } catch(Exception e) {

            toast = Toast.makeText(this, e.getMessage() + "error 1st catch",   

    Toast.LENGTH_SHORT);
            toast.show();
        }
        try {
        pw.close();
        } catch (Exception e) {
            toast = Toast.makeText(this, e.getMessage() + "error 2nd catch", 

    Toast.LENGTH_SHORT);
            toast.show();
        }

        try {
            input.close();

        } catch (Exception e) {
            toast = Toast.makeText(this, e.getMessage() + "error 3rd catch", 

    Toast.LENGTH_SHORT);
            toast.show();
        }

I'm not even sure this is the right approach to be taking, but I've found it very difficult to find examples anywhere.

Any helpful direction would be much appreciated.

idiottiger
  • 5,147
  • 2
  • 25
  • 21

1 Answers1

0

Could be that serialized object are from different packages - this should throw a class not found exception. Here is some more info. I have not found a solution other than making a common package or some custom mapping.

Community
  • 1
  • 1
subforry
  • 70
  • 5