简体   繁体   中英

How pass and receive object from android to Java server by socket?

I have simple Java server:

public class Main {

    public static void main(String[] args) throws IOException {
        System.out.println("Welcome to Server side");
        BufferedReader in;
        PrintWriter out;

        ServerSocket servers = null;
        Socket fromClient = null;

        // create server socket
        try {
            servers = new ServerSocket(4444);
        } catch (IOException e) {
            System.out.println("Couldn't listen to port 4444");
            System.exit(-1);
        }

        try {
            System.out.print("Waiting for a client...");
            fromClient = servers.accept();
            System.out.println("Client connected");
        } catch (IOException e) {
            System.out.println("Can't accept");
            System.exit(-1);
        }

        in = new BufferedReader(new InputStreamReader(fromClient.getInputStream()));
        out = new PrintWriter(fromClient.getOutputStream(), true);
        String input;

        System.out.println("Wait for messages");
        while ((input = in.readLine()) != null) {
            if (input.equalsIgnoreCase("exit")) break;
            out.println("S ::: " + input);
            System.out.println(input);
        }
        out.close();
        in.close();
        fromClient.close();
        servers.close();
    }
}

and simple Android client:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        new MyAsync().execute();
    }
---------------------
public class MyAsync extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            try {
                fromServer = new Socket("192.168.0.103",4444);
                in = new BufferedReader(new InputStreamReader(fromServer.getInputStream()));
                out = new PrintWriter(fromServer.getOutputStream(), true);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

-------------------

 @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button){
            out.println(editText.getText().toString());
        }
    }

All work good. I send message from Android to server and sever print this message in console. But I want send Object, for example User:

public class User {
    private int age;
    private String fio;

    public User() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getFio() {
        return fio;
    }

    public void setFio(String fio) {
        this.fio = fio;
    }

In Android i can write:

User user = new User();
out.print(user);

But i am not understanding that how can i read this on server side?

You can't do it with print() . Use an ObjectOutputStream.writeObject() at the sender, and ObjectInputStream.readObject() at the receiver. You will need to adjust your User class to implement Serializable and provide a private static long serialVersionUID value.

NB Don't write code like this. Code that depends on the success of code in a try block should be inside the same try block.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM