簡體   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