title: How do i make a button execute this method
body: <p>This may be a super easy answer but my app crashes when I push the button. This is the method im trying to run the the click of a button:</p>
  
  <pre><code>public void postData() {
      // Create a new HttpClient and Post Header
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
  
      try {
          // Add your data
          List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
          nameValuePairs.add(new BasicNameValuePair("id", "12345"));
          nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
          // Execute HTTP Post Request
          HttpResponse response = httpclient.execute(httppost);
  
      } catch (ClientProtocolException e) {
          // TODO Auto-generated catch block
      } catch (IOException e) {
          // TODO Auto-generated catch block
      }
  } 
  </code></pre>
  
  <p>I made sure I have :</p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
  </code></pre>
  
  <p>and here is my activity main:</p>
  
  <pre><code> &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView1"
      android:layout_below="@+id/textView1"
      android:layout_marginTop="16dp"
      android:onClick="postData"
      android:text="Button" /&gt;
  </code></pre>