ObjectInputStream 中断程序

ObjectInputStream haults program

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

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();
    }
}

Client/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();