这是一个非常奇怪的部分,如果我发送一个布尔值或字符串(来自设备)它可以工作....它只是我的“自定义”对象,不。我认为这适用于任何“标准”java对象。
如果您发现错误,请记住代码确实有效,但只能从另一台计算机到我的计算机......而不是Android设备到计算机。如果你仍然发现另一个明显的错误,那么真棒:)
ANDROID计划:
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();
}
}
}
谢谢!
答案 0 :(得分:4)
我怀疑Android的序列化格式可能与Java VM的格式不兼容。您可以尝试将对象转换为XML或其他文本格式吗?