這可能是一個超級簡單的答案,但當我按下按鈕時,我的應用程序崩潰。這方法我嘗試運行按鈕的點擊:如何使按鈕執行此方法
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" />
嗯。打敗我10秒:P +1 –
@RaghavSood在同一時間..爲什麼你刪除,指出當你投票你 – stinepike
我錯過了關於UI線程聯網的問題。你的回答已經有了這些信息。沒有意義複製它:P –