problem with sockets and textView

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

problem with sockets and textView

Postby misato » Mon Jan 12, 2009 12:07 pm

Hi again,
I'm doing a simple irc client (just to test sockets). I've got a textView in the main layout where to write server responses, and a socket to connect and send commands to the server.
The problem is the textView is not writting anything but if i debug it, it does!
My code is like this (sorry for the mess, it's just a proof of concept):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2.  
  3.  
  4.  
  5. private TextView text;
  6.  
  7. private PrintWriter output;
  8.  
  9. private BufferedReader input;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. public void onCreate(Bundle savedInstanceState) {
  16.  
  17.         super.onCreate(savedInstanceState);
  18.  
  19.         setContentView(R.layout.main);
  20.  
  21.         text = (TextView) findViewById(R.id.text);
  22.  
  23.        
  24.  
  25.         try{
  26.  
  27.                 text.setText("Welcome to droidChat \n");
  28.  
  29.                 Socket socket = new Socket(SERVER, PORT);
  30.  
  31.                
  32.  
  33.                 input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  34.  
  35.                 output = new PrintWriter(socket.getOutputStream(), true);
  36.  
  37.                
  38.  
  39.                String reply = sendCommand("PASS NOPASS\n\r");
  40.  
  41.                reply += sendCommand("NICK " +NICK+"\n\r");
  42.  
  43.                reply += sendCommand("USER "+NICK+EOF );
  44.  
  45.                reply += sendCommand ("JOIN #misatofans");
  46.  
  47.                
  48.  
  49.                text.setText(reply);
  50.  
  51.                
  52.  
  53.         } catch(Exception ex){
  54.  
  55.                 ex.printStackTrace();
  56.  
  57.                 text.append(ex.getMessage());
  58.  
  59.         }
  60.  
  61.     }
  62.  
  63.    
  64.  
  65.     private String  sendCommand(String msg){
  66.  
  67.         String reply = "";
  68.  
  69.         try{
  70.  
  71.                 output.println(msg);
  72.  
  73.                 reply = input.readLine();
  74.  
  75.                
  76.  
  77.         }catch (Exception ex){
  78.  
  79.                 text.append(ex.getMessage());
  80.  
  81.         }
  82.  
  83.         output.flush();
  84.  
  85.         return reply;
  86.  
  87.        
  88.  
  89.     }
  90.  
  91.    
  92.  
  93.  
  94.  
  95.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I've read something about handlers and threads but i don't understand how to use it propperly in my case.

What i've figured out, it may be a problem with the refresh of the screen or something similar (maybe i'm wrong) but i don't know how to solve it.
Thanks in advance
misato
Freshman
Freshman
 
Posts: 7
Joined: Wed Jan 07, 2009 11:55 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests