Android - modify a textview from asynctask (doInBackground)

0

Good morning: I'm doing an Android chat client that connects to a local java server, and I've already pretty much finished.

The only thing I need is that the messages received by the AsyncTask class, which runs a getinputstream from the server, I write it in the text view of mainactivity, but not having access to the user interface for being in a class secondary does not leave me, and I do not know how to do it.

I copy you in case the part of the code that receives the messages helps you:

public void escribirMensaje(String m){
  tvChat.append(m + System.lineSeparator());
}
class RecibirMensajes extends AsyncTask {
  Socket socket;
  RecibirMensajes(Socket s){
      this.socket=s;
  }
  @Override
  protected Object doInBackground(Object[] params) {
      // Obtiene el flujo de entrada del socket
      DataInputStream entradaDatos = null;
      String mensajeRecibido="";
      String mensajeAntiguo=mensajeRecibido;
      try {
          entradaDatos = new DataInputStream(socket.getInputStream());
      } catch (IOException ex) {
          System.out.println("Error al crear el stream de entrada: " + ex.getMessage());
      } catch (NullPointerException ex) {
          System.out.println("El socket no se creo correctamente. ");
      }

      // Bucle infinito que recibe mensajes del servidor
      boolean conectado = true;
      while (conectado) {
          try {
              mensajeRecibido = entradaDatos.readUTF();
              if(mensajeRecibido!=mensajeAntiguo){
                  escribirMensaje(mensajeRecibido);
                  mensajeAntiguo=mensajeRecibido;
              }
          } catch (IOException ex) {
              System.out.println("Error al leer del stream de entrada: " + ex.getMessage());
              conectado = false;
          } catch (NullPointerException ex) {
              System.out.println("El socket no se creo correctamente. " + ex.getMessage());
              conectado = false;
          }
      }
      return null;
  }

I hope you can help me! Thank you very much in advance.

    
asked by walkerdeath 12.11.2017 в 19:44
source

0 answers