从服务器到客户端传输ArrayList <object>期间出错

时间:2015-11-22 15:30:10

标签: java object arraylist socket.io client

我实现了一个允许服务器和客户端之间通信的代码。我从服务器发送了ArrayList<Object>,客户收到了它。 问题是当我尝试在客户端打印我的列表时,因为我想验证它是否已损坏。实际上程序启动ClassCastException

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

public class Server {
     public final static int PORT = 6543;
     ServerSocket ss = null;
     Socket s = null;
     DataInputStream in;
     DataOutputStream out;
     ObjectInputStream objIn;
     ObjectOutputStream objOut;
     Connection conn;
     protected final static String DRIVER = "com.mysql.jdbc.Driver";
     protected final static String SERVERURL = "jdbc:mysql://localhost:3306/datazanzariere?zeroDateTimeBehavior=convertToNull";
     protected final static String USER = "root";
     protected final static String PASSWORD = "admin";

     public Socket waitObj(){
         try {
              System.out.println("inizializzo il server");
              ss = new ServerSocket(PORT);
              System.out.println("server pronto in ascolto");
              s = ss.accept();
              System.out.println("connessione stabilita");
              objOut = new ObjectOutputStream(s.getOutputStream());

         } catch (IOException ex) {
             Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
         }
         return s;
     }

     public void sendList() {
         List<PersonaFisica> pfList = new ArrayList<PersonaFisica>();
         PersonaFisica pf = new PersonaFisica();
         int i=0;

         try{
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(SERVERURL, USER, PASSWORD);
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM personafisica");
            while(rs.next()) {
                   pf.setClienteF(rs.getInt("ClienteF"));
                   pf.setCodFiscale(rs.getString("CodFiscale"));
                   pf.setNome(rs.getString("Nome"));
                   pf.setCognome(rs.getString("Cognome"));
                   pfList.add(pf);
                   System.out.println(pfList.get(i).getClienteF() + " " + pfList.get(i).getCodFiscale()+ " " + pfList.get(i).getNome()+ " " + pfList.get(i).getCognome());
                   i++;
            } 
            objOut.writeObject(pfList);

            objOut.close();
            s.close();
            ss.close();
            }catch (Exception e){
                JOptionPane.showMessageDialog(null, e);  
             } 
    }
 }

这是客户端的代码:

public class Client {

private final static int PORT = 6543;
Socket s = null;
DataInputStream in;
DataOutputStream out;
ObjectOutputStream outObj;
ObjectInputStream inObj;
private List<PersonaFisica> pfList = new ArrayList<PersonaFisica>();


public Socket connectObj(){

    try {
        System.out.println("Provo a connettermi al server....");
        s = new Socket("192.168.0.7",PORT);  

        System.out.println("Connesso.");

        inObj = new ObjectInputStream(s.getInputStream());
        //outObj = new ObjectOutputStream(s.getOutputStream());

    } catch (IOException ex) {
        System.out.println("Non posso connettermi.");
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
     return s;
}

public void receiveList(){
    try {

        System.out.println("Dentro prova prima del flusso");
        pfList = (ArrayList<PersonaFisica>)inObj.readObject();

        System.out.println("Dentro prova dopo il flusso");
        inObj.close();
        s.close();

    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Dentro prova fuori catch");
        System.out.println(pfList);


}

public void printList(){

    for(int i = 0; i < pfList.size(); i++){
        System.out.println(pfList.get(i).getClienteF() + " " +pfList.get(i).getCodFiscale() + " " +pfList.get(i).getNome() + " " +pfList.get(i).getCognome());
    }
}}

我已经从服务器向客户端导入了包含PersonaFisica类的包。有解决方案还是我必须改变方法?

1 个答案:

答案 0 :(得分:0)

当然,这是代码:

public class PersonaFisica extends Cliente implements Serializable {

private int clienteF = 0;
private String codFiscale = "";
private String nome = "";
private String cognome = "";
public Connection conn;
public Statement st;
public ResultSet rs;



@Override
public List<String> recuperaPers(String DBresult) throws SQLException{


    List <String> cognome = new LinkedList <String>();

    try {

        Class.forName(DRIVER); //avvio il driver
        conn = DriverManager.getConnection(SERVERURL, USER, PASSWORD);
        st = conn.createStatement();
        rs = st.executeQuery("SELECT Cognome FROM personafisica");

        while(rs.next()){
                cognome.add(rs.getString("Cognome"));

        } 

    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(PersonaFisica.class.getName()).log(Level.SEVERE, null, ex);
    }
    finally{
        if(conn!=null){
            conn.close();
        }
    }
    return cognome;
}

public void setClienteF(int clienteFPar){
    this.clienteF = clienteFPar;
} 

public void setCodFiscale(String codFiscalPar) {
    this.codFiscale = codFiscalPar;
}

public void setNome(String nomePar) {
    this.nome = nomePar;
}

public void setCognome(String cognomePar) {
    this.cognome = cognomePar;
}

public int getClienteF() {
    return clienteF;
}

public String getCodFiscale() {
    return codFiscale;
}

public String getNome() {
    return nome;
}

public String getCognome() {
    return cognome;
}
}

Cliente类是一个抽象类,只包含recuperaPers抽象方法。

这是一个例外:

Exception in thread "main" java.lang.ClassCastException: javaapplication10.PersonaFisica cannot be cast to clientrete.PersonaFisica
at clientrete.Client.printList(Client.java:92)
at clientrete.Home.main(Home.java:325)