我正在尝试从 android 应用程序向 PHP 脚本发送一些 POST 数据。PHP 脚本应该是什么样子的?这是我尝试过的,但它不起作用;
安卓代码:
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.alex26.0fees.net/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;
}
@Override
protected void onPostExecute(String result) {
}
}
PHP 脚本:
<?php
if(isset($_POST['id']))
echo $_POST['id'];
if(isset($_POST['stringdata']))
echo $_POST['stringdata'];
?>