我的PHP服务器上出现未知JSON错误
我正在开发一个允许数据从Android传输到PHP服务器的应用程序,我不知道为什么它不支持JSON 这是我的密码:我的PHP服务器上出现未知JSON错误,php,android,json,Php,Android,Json,我正在开发一个允许数据从Android传输到PHP服务器的应用程序,我不知道为什么它不支持JSON 这是我的密码: <?php JSON.parse(); $decode = json_decode($_REQUEST['request']); $json = $decode->name; header('Content-type:application/json'); echo json_encode($json); ?> 检查您的JSON地址 如果JSON有效,则php
<?php
JSON.parse();
$decode = json_decode($_REQUEST['request']);
$json = $decode->name;
header('Content-type:application/json');
echo json_encode($json);
?>
检查您的JSON地址
如果JSON有效,则php代码可能不正确
显示一些具体代码。您可以使用以下代码从android以字符串形式发送Json数据:
BufferedReader reader = null;
// Send data
try {
/* forming th java.net.URL object */
URL url = new URL(this.url);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
/* pass post data */
byte[] outputBytes = jsonData.toString().getBytes("UTF-8");
OutputStream os = urlConnection.getOutputStream();
os.write(outputBytes);
os.close();
/* Get Response and execute WebService request*/
int statusCode = urlConnection.getResponseCode();
/* 200 represents HTTP OK */
if (statusCode == HttpsURLConnection.HTTP_OK) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
ResponseData= convertStreamToString(inputStream);
} else {
ResponseData = null;
}
$post_body = file_get_contents('php://input');
$post_body = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($post_body));
$reqData[] = json_decode($post_body);
$postData = $reqData[0];
echo $postData->name;
在php中,您可以通过添加以下代码来获取数据:
BufferedReader reader = null;
// Send data
try {
/* forming th java.net.URL object */
URL url = new URL(this.url);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
/* pass post data */
byte[] outputBytes = jsonData.toString().getBytes("UTF-8");
OutputStream os = urlConnection.getOutputStream();
os.write(outputBytes);
os.close();
/* Get Response and execute WebService request*/
int statusCode = urlConnection.getResponseCode();
/* 200 represents HTTP OK */
if (statusCode == HttpsURLConnection.HTTP_OK) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
ResponseData= convertStreamToString(inputStream);
} else {
ResponseData = null;
}
$post_body = file_get_contents('php://input');
$post_body = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($post_body));
$reqData[] = json_decode($post_body);
$postData = $reqData[0];
echo $postData->name;
显示一些代码。这将有助于找出问题