title: Using AsyncTask for android network connection
body: <p>I am having some trouble using <code>AsyncTask</code> as I have never come across it before and have no clue what I am doing with it. </p>
  
  <p>Basically I am getting a force close because I am trying to run the connection on a main class. Could someone possibly help me with adding <code>AsyncTask</code> into the code: </p>
  
  <pre><code>package com.smarte.smartipcontrol;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  
  public class IPControl extends Activity {
  
    private Socket socket;
    private String serverIpAddress;
    private static final int REDIRECTED_SERVERPORT = 32;
    public PrintWriter out;
    public BufferedReader in ;
  
      @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // Get the message from the intent
          Intent intent = getIntent();
          serverIpAddress = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
          createConnection();
  
        }
  
    public void getModel(View view) {
      try {
        out.println("[m\r\n");
        //System.out.print("root\r\n");
        while (! in .ready());
        String textStatus = readBuffer();
  
      } catch (IOException e) {}
    }
  
    public void createConnection() {
      try {
        InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
        socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
      } catch (UnknownHostException e1) {
        e1.printStackTrace();
      } catch (IOException e1) {
        e1.printStackTrace();
      }
      try {
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        while (! in .ready());
        readBuffer();
        out.println("root\r\n");
        //System.out.print("root\r\n");
        while (! in .ready());
        readBuffer();
        out.println("root\r\n");
        //System.out.print("root\r\n");
        while (! in .ready());
        readBuffer();
      } catch (IOException e) {}
  
  
      //R.id.textStatus
    }
  
    private String readBuffer() throws IOException {
      String msg = "";
  
      while ( in .ready()) {
        msg = msg + (char) in .read();
      }
      //System.out.print(msg);
      if (msg.indexOf("SNX_COM&gt; ") != -1) return msg.substring(0, msg.indexOf("SNX_COM&gt; "));
      else return msg;
    }
  
  }
  </code></pre>