0

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:

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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(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
    }
} 

I made sure I have :

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

and here is my activity main:

 <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" />
BluGeni
  • 3,378
  • 8
  • 36
  • 64

3 Answers3

3

The signature of the method should be

public void postData(View v)

Another thing, You can not perform network operation in main UI thread. Use AsyncTask instead

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

The application may be crashing because connecting to internet is not allowed on UI Thread. So, you should AsyncTask to post data to HTTP Web Services.

More details can be found in this discussion (How to fix android.os.NetworkOnMainThreadException?)

Community
  • 1
  • 1
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
0

You should use a asynctask for network related opearation or create a new thread.

  HttpResponse response = httpclient.execute(httppost);

If not you will NetWorkOnMainThreadException.

On button click

        new PostData().execute();

Then

      class PostData extends AsyncTask<Void,Void,Void>
      {
              @Override
              protected void doInBackground(Void... params)
              {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

                    try {
                        // Add your data
                       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(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
                       }
                       return null;           
              }   
      }      

For more info check the docs

http://developer.android.com/reference/android/os/AsyncTask.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256