从android应用程序,我正在调用我的php脚本。
HttpPost httppost = new HttpPost("http://www.smth.net/some.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("pa", "555"));
nameValuePairs.add(new BasicNameValuePair("pb", "550"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}
如何在我的PHP脚本中获取“ pa”和“ pb”的值。
我做了$ _POST ['pa']和urldecode($ _ POST ['pa']),但这两个都给了我空字符串。
您可以print_r($_POST)
用来调试正在发送的内容。
如果有帮助,这就是我使用POST从Android发送信息的方式:
JSONObject requestBody = new JSONObject();
requestBody.put("pa", "550");
requestBody.put("pb", "550");
HttpPost requestBase = new HttpPost("url");
StringEntity stringEntity = new StringEntity(requestBody.toString());
stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
requestBase.setEntity(stringEntity);
HttpResponse response = httpclient.execute(requestBase);
不过,我是从内存中进行工作的-您需要插入几个try / catches。
可能不需要使用JSONObject,但我发现它为我提供了更好的灵活性和可靠性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句