1

I need to send serialized Data from an Android Device to a Java Server. I don't know why, but the communication seems to be very unstable. In the Debugger, the java.io.StreamCorruptedException: is thrown immediately after the start. not one Object is passing. Without the debugger, nearly 10 Messages are passed until the same Exception is thrown. Anyone got an idea, please help me:) Thank you! Fabian

Exception:

java.io.StreamCorruptedException: invalid type code: 2F
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at androidAnbindung.AndroidVerwalter.run(AndroidVerwalter.java:114)

java-code:

public void Nachrichtenaustausch(){
    port ++;
    try {
        serverSocket = new java.net.ServerSocket(port);
        System.out.println("Warte auf 2. Verbindungsaufbau...");
        client = serverSocket.accept();
        System.out.println("Verbindung 2 ist eingegangen...");
        in = new ObjectInputStream(new ObjectInputStream(client.getInputStream()));
        Nachricht n;
        // starte den regulären Verkehr mit dem Androidgerät
        new Thread(this).start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
    public void run() {
        Nachricht speicher;
        while (true) {
            try {

                speicher = (Nachricht) in.readObject();
                if (speicher != null) {
                    System.out.println(speicher.getName()+"..............................."+speicher.getWerte().get(0));
                }synchronized (objekliste) {
                    for (AndroidObject ao : this.objekliste) {
                        if (speicher.getName().equals(ao.name)) {
                            ao.abstrakter_Wert = speicher.getAktuellerWert();
                            if (speicher.getWerte()!=null) {
                                ao.werte = speicher.getWerte();

                            }
                        }
                    }
                }
                Thread.sleep(50);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (Exception e) {
                // TODO: handle exception
            }

        }
    }

and the android thread:

@Override
public void run() {
    // TODO Auto-generated method stub  
    try {
        while (true) {
            if (!nachrichtenliste.isEmpty()) {
                Nachricht speicher = nachrichtenliste.get(0);
                try {
                    out.writeObject(speicher);
                    out.flush();
                    synchronized (nachrichtenliste) {
                        nachrichtenliste.remove(speicher);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Thread.sleep(50);
            handler.post(this);
        }


    } catch (Exception e) {
        // TODO: handle exception
    }       
}

1 Answer 1

1

I can imagine that Android and Java are not exactly serialization compatible here. So that one side sends a code that the other does not expect.

I'd rather go with some more textual protocol (json, xml) here, than with serialization.

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

1 Comment

hmm...ok, the "Nachricht" ist not that big, contains only of 2 Strings and an ArrayList of Floats. It won't be that much trouble to create a String with all informations. I just don't get why it works "sometimes" or at first. Before the Thread starts, android has already sent an ArrayList of another Object and it works correct, until the Thread starts. I'll try with the String, thanks.

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.