title: Display ListView when button Click
body: <p>I'm new to android and I hope someone could help me here. I have an activity Faculty and a button. </p>
  
  <p>This is my XML layout browse_faculty:</p>
  
  <pre><code>&lt;Button
      android:onClick="searchFSTEHandler"
      android:id="@+id/bFSTE"
      android:layout_width="220dp"
      android:layout_height="80dp"
      android:layout_alignLeft="@+id/bFBE"
      android:layout_below="@+id/bFBE"
      android:layout_marginTop="20dp"
      android:text="@string/fste" /&gt;
  </code></pre>
  
  <p>and this is my Faculty Activity which displays the buttons:
  I use Intent to view ListView</p>
  
  <pre><code>public class Faculty extends Activity{
  
      @Override
      protected void onCreate(Bundle BrowseFaculty) {
          // TODO Auto-generated method stub
          super.onCreate(BrowseFaculty);
          setContentView(R.layout.browse_faculty);
      }
  
      //on the XML,this is the "searchFSTEHandler" i want to use to show ListView
      public void searchFSTEHandler(View target){
          Intent courseList = new Intent(this, HttpActivity.class);
          startActivity(courseList);
      }
  }
  </code></pre>
  
  <p>and below is the "HttpActivity" class is the class that displays my ListView. This class read a php file which gets data from a MySQL server and converts to JSON data then parses it into a array list.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.ArrayList;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  
  
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.app.ListActivity;
  import android.widget.ArrayAdapter;
  
  public class HttpActivity extends ListActivity {
  
  public String f1 = "FSTE";
  
  
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.faculty_course);
  
  
      new getHttp().execute();
  
      getHttp test =new getHttp();
  
  
      String strJsonData = test.doInBackground(f1);
  
      // Convert String JSON data to Java class
      ArrayList&lt;Courses&gt; arrayCourse= test.parseJsonData(strJsonData);
  
      //Create an ArrayAdapter which shows the ArrayList data
      this.setListAdapter(new ArrayAdapter&lt;Courses&gt;(this,android.R.layout.simple_list_item_1,arrayCourse));
  
  
  
  }
  
  
  
  
  private class getHttp extends AsyncTask&lt;String, Void, String&gt; {
  
    public getHttp() { 
  
  
  
  
  
  
  
  
  
  
  
    }
  
    @Override
      protected String doInBackground(String... faculty) {
  
  
  
      InputStream is = null;
      try {
          URL url = new URL("http://10.0.2.2/sqlWebService.php?faculty=FSTE");
          URLConnection con = url.openConnection();
          con.setConnectTimeout(10000);
          con.setReadTimeout(10000);
          is = con.getInputStream();
  
          BufferedReader br = new BufferedReader(
                  new InputStreamReader(is));
          StringBuffer sb = new StringBuffer();
          String str;
          while ((str = br.readLine()) != null) {
              sb.append(str);
          }
          return sb.toString();
  
      } catch (IOException e) {
          e.printStackTrace();
          return "";
  
      } finally {
          try {
              if (is != null)
                  is.close();
          } catch (IOException e2) {
              e2.printStackTrace();
          }
      }
  
  
  }
  
  //------------------------------------------------------  
    private ArrayList&lt;Courses&gt; parseJsonData(String strJson) {
          ArrayList&lt;Courses&gt; Course = new ArrayList&lt;Courses&gt;();
  
          try {
              // Generate JSONArray object by JSON String data
              JSONArray arr = new JSONArray(strJson);
  
              //from the JSONArray, get one element (row) of JSONData
              for (int i = 0; i &lt; arr.length(); i++) {
  
                  //Get JSON Object which is one element of the array
                  JSONObject ob = arr.getJSONObject(i);
  
                  Courses exam = new Courses();
                  //get the value by key, and set to exam class
                  exam.setCode(ob.optString("code"));
  
                  //save exam class to exam array list
                  Course.add(exam);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
          return Course;
      }
  
  
  
  
  
   }
  
  
  
  
  
  
  
  
  
   } 
  </code></pre>
  
  <p>The application crashes as soon as I click on the button and gives a error:
  "android.os.NetworkOnMainThreadException"</p>
  
  <p>Help Please !</p>