序列化类出错

时间:2011-05-20 18:12:55

标签: java android objectoutputstream

我有一个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;
  }


}

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


编辑:

服务器上的

我有以下内容:

包服务器;

import java.io.Serializable;

public class 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;
  }


}

在客户端我有以下内容:

package com.Server_1;

import java.io.Serializable;

public class 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;
  }


}

我不明白我怎样才能将包改为两者都相同!!!!!!

1 个答案:

答案 0 :(得分:4)

在服务器和客户端上,类的包名称必须相同。我还要在类中添加serialVersionUID。 Implementing Serializable