0

我想在 android 中实现一个客户端-服务器聊天应用程序。试过这段代码。但它只向服务器发送数据,不会获取任何数据。

应用程序必须连续传输数据。

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try
    {
        socket = new Socket("192.168.50.11", 2001);

        out = new PrintWriter(socket.getOutputStream());
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        out.print("hello!");
        out.flush();

    this.reciveThread = new Thread(new reciveClass());
        this.reciveThread.start();
    }
    catch (Exception e)
    {
        Log.d(appTag, e.toString());
    }

}

class reciveClass implements Runnable
{
    public void run()
    {
        try
        {
            String readed;
            while (true)
            {
                try
                {
                    if ((readed = reader.readLine()) != null)
                    {
                        final String read = readed;
                        handler.post(new Runnable()
                        {
                            @Override
                            public void run()
                            {                                    
                                TextView tv = (TextView) findViewById(R.id.messageText);
                                tv.setText(tv.getText() + "\n" + read);
                            }

                        });
                    }
                }
                catch (Exception ee)
                {
                    Log.d(appTag, ee.toString());
                }
                Thread.sleep(100);
            }
        }
        catch (Exception e)
        {
        }
    }
}

问题出在哪里?谢谢。

4

0 回答 0