gpt4 book ai didi

java - 可序列化类出错

转载 作者:搜寻专家 更新时间:2023-11-01 08:14:36 25 4
gpt4 key购买 nike

我有一个 java 服务器,它通过套接字将一些数据发送到在 android 中开发的客户端。发送的数据是序列化的……在客户端尝试从 ObjectInputStream 读取时出现以下错误:

 java.lang.ClassNotFoundException: servers.Coordinate    
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:237)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2604)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1860)
java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:840)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2080)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:943)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:2299)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2254)
at com.Server_1.ClientThread_special.run(ClientThread_special.java:30)
at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.NoClassDefFoundError: servers.Coordinate
Caused by: java.lang.ClassNotFoundException: servers.Coordinate in loader dalvik.system.PathClassLoader@43d02e18
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)

现在这是我在服务器端的代码:

clientSocket = serverSocket.accept();
System.out.println("S-a conectat clientul de monitorizare!");


os=new ObjectOutputStream(clientSocket.getOutputStream());
try{
while(true){
coord=(Coordinate)queue.take();
System.out.println(coord.getLat());
os.writeObject(coord);

os.flush();

}
}
catch(Exception e)
{
e.printStackTrace();

}

在客户端我有这个:

public void run() {

try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);

Socket socket = new Socket(serverIpAddress, serverPort);

is=new ObjectInputStream(socket.getInputStream());



try{
while(!stop){

coord=(Coordinate)is.readObject();

System.out.println("Date de la clientul de monitorizare:"+coord.getLat());

}

}
catch(Exception e)
{
e.printStackTrace();
}

is.close();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

这是我的可序列化类,我在客户端和服务器端都有它:

 public class Coordinate implements Serializable{

private final double lon;

private final double lat;

private final int workerId;



public Coordinate(double lat, double lon, int workerId) {

this.lat = lat;
this.lon = lon;

this.workerId=workerId;
}

public double getLon() {
return lon;
}

public double getLat() {
return lat;
}
public int getwId(){
return workerId;
}


}

这个类的一个实例就是我通过套接字发送的内容。


编辑:

在服务器上我有以下内容:

包服务器;

导入 java.io.Serializable;

公共(public)类 Coordinate 实现 Serializable{

  private final double lon;

private final double lat;

private final int workerId;



public Coordinate(double lat, double lon, int workerId) {

this.lat = lat;
this.lon = lon;

this.workerId=workerId;
}

public double getLon() {
return lon;
}

public double getLat() {
return lat;
}
public int getwId(){
return workerId;
}


}

在客户端我有以下内容:

包 com.Server_1;

导入 java.io.Serializable;

公共(public)类 Coordinate 实现 Serializable{

  private final double lon;

private final double lat;

private final int workerId;

public Coordinate(double lat, double lon, int workerId) {

this.lat = lat;
this.lon = lon;

this.workerId=workerId;
}

public double getLon() {
return lon;
}

public double getLat() {
return lat;
}
public int getwId(){
return workerId;
}


}

我不明白我怎么能把两个包都改成一样的!!!!!!

最佳答案

类的包名在服务端和客户端必须相同。我还会将 serialVersionUID 添加到类中。 Implementing Serializable

关于java - 可序列化类出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6076034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com