这是我的 Java 代码:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://iwindroids.ru/test.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("lal", "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
}
break;
default:
break;
}
}
当我按下按钮1时写入:
Unfortunately, [App name] App has stopped.
我在 list 中写的。
<uses-permission android:name="android.permission.INTERNET"/>
安卓。 eclipse 。邮政。 http
最佳答案
您不应该从主/UI 线程进行 http 调用。你应该在 android 中的一个单独的线程中执行此操作。因此,使用新的 Thread 或 AsyncTask 进行 http 调用并稍后更新 UI
关于java - 发布请求。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090672/