title: My app freezes, how to implement Threads in it?
body: <p>Could you help me figure out how to implement Threads to this, so it won't freeze while it waits for the answer from the server?
  I've tried for 5 hours or so, i simple can't find a way to use it in a thread, and then return it to set the text with tv.setText();</p>
  
  <pre><code>package zee.rhs.dk;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class AndroidClientActivity extends Activity {
  
  private String ip = "90.184.254.246";
  private int port = 8081;
  private String line;
  private TextView tv;
  private Button btn;
  private Socket socket;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      tv = (TextView) findViewById(R.id.tv);
      btn = (Button) findViewById(R.id.btn);
      btn.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View v) {
              BufferedReader in = null;
              PrintWriter out = null;
              try {
                  socket = new Socket(ip, port);
                  out = new PrintWriter(socket.getOutputStream(), true);
                  out.println("update");
  
                  in = new BufferedReader(new InputStreamReader(socket
                          .getInputStream()));
  
                  while ((line = in.readLine()) == null) {
                  }
                  tv.setText(line);
  
              } catch (UnknownHostException e) {
                  Toast.makeText(AndroidClientActivity.this,
                          "Can't reach ip: " + ip, Toast.LENGTH_LONG).show();
                  e.printStackTrace();
              } catch (IOException e) {
                  Toast.makeText(AndroidClientActivity.this,
                          "Accept failed at port: " + port, Toast.LENGTH_LONG)
                          .show();
                  e.printStackTrace();
              } finally {
                  out.close();
              }
          }
      });
  }
  }
  </code></pre>