Skip to content
Advertisement

Android Socket + ObjectOutputStream not working correctly

I am developing a client/server program where the client is an android device.

The server has a listener class that reads an object from the input stream. I created a client software for another COMPUTER that sends a small object over a local network. Computer to Computer works perfectly fine, i read the object and printed the contents. However, the SAME code ported over to android (I rewrote it just in case) does not work. I construct an object (ANY object), which is serializable, and send it via the ObjectOutputStream. My server running on the computer does connect to the device, but it gives me a ClassNotFound exception, even if im printing the object (Which has a toString). As i said, the same code running on another COMPUTER (as a .jar file) works perfectly fine.

Here is the really strange part, if i send a boolean or String (from the device) it works….its just my “custom” objects that dont. I presume this would work for any “standard” java object.

If you do find mistake please keep in mind that the code does work, but only from another computer to my computer…not the Android device to the Computer. If you still find another glaring mistake, then awesome 🙂

ANDROID PROGRAM:

package WaitDroid.Main;

import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity {
/** Called when the activity is first created. */
private Button a;
private TextView x;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.a = (Button) this.findViewById(R.id.Send_Order);
    this.x = (TextView) this.findViewById(R.id.TextView1);
    this.a.setOnClickListener(new OnClickListener()
    {
        
        @Override
        public void onClick(View arg0)
        {
            sendMenu();
        }
    });
}

private void sendMenu()
{
     try
     {
         InetAddress serverAddress = InetAddress.getByName("128.153.180.109");
         Socket socket = new Socket(serverAddress, 4322);
         ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
         TestObject send = new TestObject("Hi", 32);
         out.writeObject(send);
         out.close();
         socket.close();
         }
         catch(Exception e)
         {
         x.setText(e.getMessage());
         }
    }
}

TEST OBJECT:

package WaitDroid.Main;

import java.io.Serializable;

public class TestObject implements Serializable
{
    private String name;
    private int number;

    public TestObject(String a, int b)
    {
        name = a;
        number = b;
    }

    public String toString()
    {
        return name +" - "+ number;
    }
}

SERVER LISTENER:

package Main;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectInputStream.GetField;
import java.net.ServerSocket;
import java.net.Socket;

import Order.Order;

public class ServerListener extends Thread 
{

    public void run() {
        try {
            ServerSocket listen = new ServerSocket(4322);

            while (true) {
                Socket socket = listen.accept();
                String clientInetAddr = socket.getInetAddress().toString();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

                System.out.println("Connected to: " + clientInetAddr);
                
                try
                {
                    Object a = in.readObject();
                    System.out.println(a);
                    //RestaurantServerRun.n.server.addOrder(a);
                }
                
                catch(IOException e)
                {
                    System.err.println(e.getMessage());
                }

                in.close();
                socket.close();
            }
        }
        catch (Exception e) {
            System.err.println("Error in run()");
            e.printStackTrace();
        }
        
    }
}

Thanks!!

Advertisement

Answer

I suspect Android’s serialization format may be not compatible with Java VM’s format. Can you try converting your objects to XML or some other text format?

Advertisement