Hey guys. 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 client software for another computer that sends a small object over a local network. Computer to computer works fine , I read the object and print the contents. However, the same code ported to android (I rewrote it just in case) does not work. I create an object (ANY object) that is serializable and sends it via ObjectOutputStream. My server running on a computer connects to the device, but it gives me a ClassNotFound exception, even if im printing an object (which has toString). As I said, the same code running on another computer (like a .jar file) works fine.
Here is the really weird part, if I send a boolean or String (from the device), it works ... its just my "user" objects that don't. I assume that this will work for any "standard" Java object.
If you find an error, keep in mind that the code really works, but only from another computer to a computer ... and not to an Android device on a computer. If you still find another glaring mistake, then amazing :)
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 { 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; } }
SERVICE LIGHT:
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);
Thanks!!
java android classnotfoundexception sockets objectoutputstream
somanys21
source share