Error ClassNotFoundException in the return

0

The error I find is using a class that inherits from AsyncTask on Android.

Inside I use an object of my class User, it communicates with a server, it handles the data ... until I do the return and I skip this exception. The class is imported

   protected User doInBackground(Object... values){
     //código de conexión con el servidor
  ObjectInputStream stream = new ObjectInputStream(socket.getInputStream());
        user = (User) stream.readObject();
        // Aquí puedo obtener todos los datos del usuario
        String received = user.getPhone();
        Log.i("I/TCP Client", "Received " + received);
        Log.i("I/TCP Client", "");
        //cierra conexion
        socket.close();
        //publishProgress(received);
        return user; // y justo cuando llega aquí salta al error
        }catch...

Thank you very much.

    
asked by Handassendil Onotimo 11.06.2017 в 19:21
source

1 answer

1

The part where you can get this error is the following line:

       user = (User) stream.readObject();

This cast is insecure, because the object you want to allocate to the User type variable comes from the other side of the connection.

To avoid the error you have to make sure that the class of the serialized object sent from the other side of the connection is identical to the class in the code of your app where you receive it.

To be identical it is important that even the name of the package is equal in the class. For example server.model.User and client.model.User are not identical independent if the code is identical.

    
answered by 11.06.2017 в 21:45