I am trying to send an object using TCP/IP using an Android application an a Java application. But I'm having the problem that the information coming is null.
The object belongs to a class called Mensaje, and in both projects has the same package name and have the same serialVersionUID:
package CLASSESS;
import java.io.Serializable;
public class Mensaje implements Serializable{
// private static final long serialVersionUID = 5950169519310163575L;
private static final long serialVersionUID = 9178463713495656548L;
public static int TYPE;
public static int ID;
public static String DATA;
}
The Android application class implements Serializable and handles the conexion into a Threat
public class SocketClient implements Serializable{
private Runnable send_ObjectMessage = new Runnable(){
public void run(){
try {
obj_Mensaje = new Mensaje();
obj_Mensaje.TYPE = 1;
obj_Mensaje.ID = 2;
obj_Mensaje.DATA= "hOKA MUNDO";
salida_mensaje = new ObjectOutputStream(socket_cliente.getOutputStream());
salida_mensaje.writeObject(obj_Mensaje);
salida_mensaje.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
The Server has the same Mensaje class and implements Serializable with the same serialVersionUID;
The server reads the object on a Threat.
@Override
public void run() {
try {
obj_message_read = new Mensaje();
ois_readMessage = new ObjectInputStream(clientSocket.getInputStream());
while(clientSocket.isConnected()){
Object aux = ois_readMessage.readObject();
if(aux instanceof Mensaje){
obj_message_read = (Mensaje)aux;
setDATA(obj_message_read.DATA);
setTYPE(obj_message_read.TYPE);
setID(obj_message_read.ID);
System.out.println("Data" + obj_message_read.DATA);
}
}
}catch (IOException ioe){
ioe.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
The problem is that the information coming in the Mensaje is null. So when i print System.out.println("Data" + obj_message_read.DATA);
the Server prints
Data Null
I think the problem is that the fields you are trying to send are declared as static
, which are transient for serialization.
Change it removing that keyword:
public class Mensaje implements Serializable{
// private static final long serialVersionUID = 5950169519310163575L;
private static final long serialVersionUID = 9178463713495656548L;
public int TYPE;
public int ID;
public String DATA;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.