【发布时间】:2013-12-16 17:34:31
【问题描述】:
我正在尝试将 json 对象从 Android 设备发送到 PHP 以将其插入数据库,但服务器找不到它。我做错了什么?谢谢。
脚本在 isset 检查中失败。
PHP 脚本:
<?php
$json_array = json_decode($_POST['messages']) -> messages;
if(isset($json_array))
{
//connect to database and do insertions.
} else
{
echo "Not found array with messages in POST request\n";
}
发布请求包含:
{"messages":[{"message":"Hello. Test from ukraine","id":1,"from_sender":"1111-111-1111","box":"2","type":"sms"}]}
它是这样形成的:
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost httpPost = new HttpPost(BASE_URL + "/josyko.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("messages", request.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
result = EntityUtils.toString(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
-
var_dump($_POST, $json_array);