如何在我的 Android 应用程序中编写 StreamReader 程序?

发布于 2025-01-02 16:00:19 字数 1576 浏览 1 评论 0原文

我要开发一个 Android 应用程序,它从服务器接收消息,但我可以连接到服务器,但无法接收消息,因为当我单击按钮连接时会阻止。我认为这是因为循环,但不知道如何以其他方式解决这种情况,感谢您的帮助。

简而言之,我想知道是否有另一种方法可以检测我何时收到消息并呈现它。

更新:我找到了这样的解决方案:

String IP;

String comando;

PrintWriter msgout;

BufferedReader msgin;

Socket socket;

    private ConnectedThread mConnectedThread;



public void connect(){ //Function to Button connect

    IP = edit.getText().toString();

    try {

     InetAddress serverAddr = InetAddress.getByName(IP); //TCPServer.SERVERIP

     Socket socket = new Socket(serverAddr, 4444); //Porta 4444 serverAddr
     PrintWriter out = new PrintWriter( new BufferedWriter( new  OutputStreamWriter(socket.getOutputStream())),true);
     msgout = out;

     BufferedReader in = new BufferedReader(new  InputStreamReader(socket.getInputStream()));
     msgin = in;

 zav.text1.setText("Connectado.");

  //Wait to mensages
  mConnectedThread = new ConnectedThread();
  mConnectedThread.start();

     } catch (Exception e) {
         text1.setText("Erro! Não conectado." + e);
    }
}

public void send(String message){

     try {

         msg.println(message);


     } catch(Exception e) {
         text1.setText("Erro! Comando não enviado." + e);
 } 
}

private class ConnectedThread extends Thread {

    public void run(){

     while(true){

         //Read
     try {

                String str = msgin.readLine();
                 text1.setText(str);

              } catch(Exception e) {

                  text1.setText("Error" + e);

              } 
         }
   }
}

I am to develop an Android application that receives a message from server but I can connect to server but can't receive a message because when I clicked on Button connect blocks. I think it's because of the cycle but do not know how to resolve this situation in another way and would appreciate your help.

In short I want to know if there's another way to detect when I receive a message and presents it.

Update: I found the solution like this:

String IP;

String comando;

PrintWriter msgout;

BufferedReader msgin;

Socket socket;

    private ConnectedThread mConnectedThread;



public void connect(){ //Function to Button connect

    IP = edit.getText().toString();

    try {

     InetAddress serverAddr = InetAddress.getByName(IP); //TCPServer.SERVERIP

     Socket socket = new Socket(serverAddr, 4444); //Porta 4444 serverAddr
     PrintWriter out = new PrintWriter( new BufferedWriter( new  OutputStreamWriter(socket.getOutputStream())),true);
     msgout = out;

     BufferedReader in = new BufferedReader(new  InputStreamReader(socket.getInputStream()));
     msgin = in;

 zav.text1.setText("Connectado.");

  //Wait to mensages
  mConnectedThread = new ConnectedThread();
  mConnectedThread.start();

     } catch (Exception e) {
         text1.setText("Erro! Não conectado." + e);
    }
}

public void send(String message){

     try {

         msg.println(message);


     } catch(Exception e) {
         text1.setText("Erro! Comando não enviado." + e);
 } 
}

private class ConnectedThread extends Thread {

    public void run(){

     while(true){

         //Read
     try {

                String str = msgin.readLine();
                 text1.setText(str);

              } catch(Exception e) {

                  text1.setText("Error" + e);

              } 
         }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2025-01-09 16:00:19

您尝试过推送通知吗?这是一个很好的起点 http://tokudu.com /2010/如何实现 Android 的推送通知/

Have you tried Push Notification? Here is a good place to start http://tokudu.com/2010/how-to-implement-push-notifications-for-android/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文