我已经完成了 raw rest get 和 post,也可以在 namevaluepair 中进行 post,但无论我遵循哪个教程,我都无法完成 json post ...
安卓应用:
ArrayList<String> stringData = new ArrayList<String>();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://192.168.1.10/people.php");
JSONObject holder = new JSONObject();
holder.put("name", "Foo");
holder.put("occupation", "Bar");
StringEntity se = new StringEntity(holder.toString());
postMethod.setEntity(se);
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
String response=httpClient.execute(postMethod,resonseHandler);
stringData.add(response);
return stringData;
PHP 文件:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST);
}
var_dump 只是给出了一个空数组,而 $_POST["name"] 给出了 undefined index ......任何帮助都会很好
最佳答案
您是否尝试过将 holder.toString()
与 BasicNameValuePair
类一起发布?
喜欢
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myjson", holder.toString());
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
然后就在你的 php 中你得到它就像
$jsonstring = $_REQUEST['myjson'];
关于php - android json 发布到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7222014/