gpt4 book ai didi

java - ObjectInputStream 拖拽程序

转载 作者:行者123 更新时间:2023-12-01 11:41:59 24 4
gpt4 key购买 nike

我正在尝试将 ArrayList 发送到 Android 设备上的客户端。服务器说它发送了该对象,但是在 Android 设备上它挂起。我已经阅读过,在创建 ObjectInputStream 时,必须首先创建 ObjectOuputStream 然后刷新。我已经尝试过,但这对我不起作用。我没有发布获取客户端的代码,因为它只是简单地从文本文件中读取。 Client 类非常基本,只有很少的属性,例如用户名、密码和 friend 字符串数组列表。任何帮助将非常感激。服务器:

public class Server {
private static final int port = 9001;
private static final String IPAddr = "xxxxxxxxxxx";
ServerSocket server = null;
ArrayList <Client> users = new ArrayList<Client>();


public Server(){
try{
server = new ServerSocket(port);
System.out.println("connected server on port" + port);
while(true){
System.out.println("waiting for connection my ip add is "+ InetAddress.getLocalHost().getHostAddress());
Socket clientsocket = server.accept();
System.out.println("Connect to client:"+ clientsocket.getInetAddress().getHostName());
ClientThread client = new ClientThread(clientsocket);
client.start();
}
} catch(IOException e) {
System.err.println("Could not listen on port");
}
}


//Thread
public class ClientThread extends Thread {
private Socket sckt = null;

public ClientThread(Socket sckt){
super("ClientThread");
this.sckt = sckt;
}
public void run(){
try{
PrintWriter out = new PrintWriter(sckt.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(sckt.getInputStream()));
ObjectOutputStream objectOutput = new ObjectOutputStream(sckt.getOutputStream());
objectOutput.flush();

String Username = input.readLine();
String Password = input.readLine();
System.out.println("recieved from client: "+ Username);
int ClientIndex = isClient(Username);
if (ClientIndex != -1){
if(users.get(ClientIndex).password.equals(Password)){
//password correct -> send friends
out.println("correct");
out.flush();
System.out.println(Username + " is correct");
LoadClientFriends(Username, ClientIndex);

objectOutput.writeObject(users.get(ClientIndex).Friends);
System.out.println("Friends sent");
} else {
//password incorrect -> retry
out.println("password");
System.out.println(Username + " has wrong password");

}


} else {
//not a registered client
out.println("wrong");
System.out.println(Username + " is not a client");
}
} catch(Exception e){
System.err.println("Couldnt connect to Client socket");
}
}
}

public static void main(String[] args){
Server svr = new Server();
}
}

客户端/Android:

public class MainActivity extends ActionBarActivity {
//Varibles
EditText username;
EditText password ;
private static final int port = 9001;
private static final String IPAddr = "xxxxxxx";
//Methods

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
/* Drawable d = getResources().getDrawable(R.drawable.actionbar_background);
getActionBar().setBackgroundDrawable(d);*/
}
public void Login(View view) {
//connect to server

Thread myThread2 = new Thread(Connect);
myThread2.start();

}

public void Register(View view) {

Intent i = new Intent(this, register_screen.class);
startActivity(i);

}
Runnable Connect = new Runnable()
{
public void run()
{
try {

Socket connection = new Socket(IPAddr,port);

BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter output = new PrintWriter(connection.getOutputStream(), true);


//Sent the username a password for verifaction

username = (EditText)findViewById(R.id.edtName);
password = (EditText)findViewById(R.id.edtPassword);

output.println(username.getText().toString());
output.flush();
output.println(password.getText().toString());
output.flush();

//Receive confirmation of client
String res = input.readLine();

if (res.contains("correct")){
ObjectOutputStream objectOutput = new ObjectOutputStream(connection.getOutputStream());
objectOutput.flush();
ObjectInputStream objectInput = new ObjectInputStream(new BufferedInputStream(connection.getInputStream())); //Error Line!

Object object = objectInput.readObject();
ArrayList<String> friends = (ArrayList<String>) object;
Intent intent = new Intent(MainActivity.this,chat_screen.class);
intent.putExtra("Friends", friends);
startActivity(intent);

}else if (res.contains("password")){

Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);

}else {


}


}catch (Exception e){


}
}

};
}

最佳答案

您看到的错误是由于多种输出流格式造成的。坚持使用ObjectOutputStream/ObjectInputStreamPrintWriter/BufferedReader。我建议ObjectOutputStream/ObjectInputStream

服务器代码:使用objectOutPut进行所有写入。

// out.println("correct");
objectOutput.writeUTF("correct");

// Update code for password and wrong too - Use objectOutput.writeUTF("");

客户端代码:仅使用ObjectInputStream而不是BufferedReader

在此处定义 objectInput:

// BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
ObjectInputStream objectInput = new ObjectInputStream(connection.getInputStream());
PrintWriter output = new PrintWriter(connection.getOutputStream(), true);

// Read data as follows:
// String res = input.readLine();
String res = objectInput.readUTF();

关于java - ObjectInputStream 拖拽程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29451823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com