我正在使用 Java 在 Android 和 Windows 上编写服务器-客户端应用程序。我想将用户的信息(用户名和密码)发送到服务器进行验证。客户端发送包含信息的对象,但服务器没有收到它!这是对象类:
package Model;
import java.io.Serializable;
public class StringObject implements Serializable {
private static final long serialVersionUID = 1L;
private String Username=null;
private String Password=null;
public void setPassword(String Password){
this.Password=Password;
}
public void setUsername(String Username){
this.Username=Username;
}
public String getPassword(){
return Password;
}
public String getUsername(){
return Username;
}
}
客户端代码:
private void Login(){
try {
ObjectOutputStream OutObj=new ObjectOutputStream(newBufferedOutputStream(connection.getOutputStream()));
OutObj.writeObject(UserPass);
OutObj.flush();
} catch (IOException e) {
Log.d("Application: ",e.toString());}
(我在这行之前设置了 Object 变量)
和服务器代码:
ObjectInputStream inObj = new ObjectInputStream(new BufferedInputStream(connection.getInputStream()));
UserPass=(StringObject) inObj.readObject();
System.out.println("UserName: " + UserPass.getUsername());
System.out.println("Password: " + UserPass.getPassword());
客户端程序(android)因错误而崩溃:线程以未捕获的异常退出。(套接字和 I/O 代码行在一个单独的线程中)
07-15 12:43:59.626: W/dalvikvm(1306): threadid=13: thread exiting with uncaught exception (group=0x40a71930)
07-15 12:43:59.626: E/AndroidRuntime(1306): FATAL EXCEPTION: Thread-89
07-15 12:43:59.626: E/AndroidRuntime(1306): java.lang.NullPointerException
07-15 12:43:59.626: E/AndroidRuntime(1306): at com.shayan.filesharing.ConnectionThread.Login(ConnectionThread.java:90)
07-15 12:43:59.626: E/AndroidRuntime(1306): at com.shayan.filesharing.ConnectionThread.ProcessMessage(ConnectionThread.java:70)
07-15 12:43:59.626: E/AndroidRuntime(1306): at com.shayan.filesharing.ConnectionThread.run(ConnectionThread.java:47)
但是当我用ObjectOutputStream
/ObjectInputStream
替换BufferedWriter
/bufferedReader
它工作正常!谁能告诉我为什么会这样?