public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/howdy/welcome.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", "rxn"));
nameValuePairs.add(new BasicNameValuePair("id", "b15803"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
System.out.println("data posted, status = " + status);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
这是我的代码张贴名称和id到我的本地服务器。现在PHP代码将接受这些参数,以便我能够存储在一个变量和网页上的回声。提前感谢
您将此数据作为POST
请求发送,因此在另一边只需使用$_POST
变量获取它。下面是一个示例:
$name = $_POST['name'];
$id = $_POST['id'];
echo "Hi, my name is $name and my id is $id'n";