如何使按钮执行此方法

时间:2013-06-30 16:22:40

标签: java android

这可能是一个非常简单的答案,但是当我按下按钮时我的应用程序崩溃了。这是尝试运行单击按钮的方法:

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
    }
} 

我确定我有:

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

这是我的活动主页:

 <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" />

3 个答案:

答案 0 :(得分:3)

方法的签名应为

public void postData(View v)

另一件事,您无法在主UI线程中执行网络操作。请改用AsyncTask

答案 1 :(得分:0)

应用程序可能崩溃,因为UI线程上不允许连接到Internet。因此,您应该使用AsyncTask将数据发布到HTTP Web服务。

有关详细信息,请参阅此讨论(How to fix android.os.NetworkOnMainThreadException?

答案 2 :(得分:0)

您应该使用asynctask进行与网络相关的操作或创建新线程。

  HttpResponse response = httpclient.execute(httppost);

如果不是,您将NetWorkOnMainThreadException

点击按钮

        new PostData().execute();

然后

      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;           
              }   
      }      

有关详细信息,请查看文档

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