0

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

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

3 回答 3

3

方法的签名应该是

public void postData(View v)

另一件事,您不能在主 UI 线程中执行网络操作。改用AsyncTask _

于 2013-06-30T16:24:23.087 回答
0

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

更多细节可以在这个讨论中找到(How to fix android.os.NetworkOnMainThreadException?

于 2013-06-30T16:25:59.920 回答
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

于 2013-06-30T16:29:08.933 回答