[重要]android socket传对象问题

package com.xiaoqiu.util;
public class User implements java.io.Serializable {

	private String name;
	private String password;

	public User() {

	}

	public User(String name, String password) {
		this.name = name;
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

发送端:

socket = new Socket(HOST, PORT);
			////////////////////////////////////
			//os = socket.getOutputStream();
			//oos = new ObjectOutputStream(socket.getOutputStream());
			ObjectOutputStream out1 = new ObjectOutputStream(socket.getOutputStream());  
		   // ObjectInputStream in1 = new ObjectInputStream(socket.getInputStream()); 
			
			User user = new User();
			user.setName("tianqiujie");
			
			out1.writeObject(user);
			out1.flush();

接收端:

ObjectInputStream in = new ObjectInputStream(
					currentSocket.getInputStream());
			// 读取客户端的对象数据流
			user = (User) in.readObject();
			System.out.println(user.getName());


最最重要的是:

要传送的对象的类必须在用一个包名下,服务器端和客户端的那个类一定要在用一包名下,我就是浪费了几个小时,终于发现了!!!


你可能感兴趣的:([重要]android socket传对象问题)