繁体   English   中英

如何通过套接字将对象从android传递到Java服务器?

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

我有简单的Java服务器:

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

和简单的Android客户端:

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

一切都很好。 我将消息从Android发送到服务器,并在控制台中将其打印出来。 但是我想发送对象,例如用户:

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

在Android中,我可以编写:

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

但是我不明白如何在服务器端阅读此内容?

您不能使用print()做到这一点。 在发送方使用ObjectOutputStream.writeObject() ,在接收方使用ObjectInputStream.readObject() 您将需要调整User类以implement Serializable并提供一个private static long serialVersionUID值。

注意:不要写这样的代码。 取决于try块中代码是否成功的代码应位于同一try块内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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