Php 从Android发送到计算引擎服务器的JSON返回null
我正在将JSON对象从Android应用程序发送到服务器。在我开发的本地机器中,它运行良好。我得到数据并解码。但当我将服务器部署到计算引擎时,我不会在服务器端接收数据。数据从Android客户端发送。我还从浏览器中测试了JSON,得到了200个响应代码。以下是代码片段: //客户 //在这里创建JSONObject 这是我正在发送的JSON{“test”,“1234”} //服务器Php 从Android发送到计算引擎服务器的JSON返回null,php,android,json,xampp,google-compute-engine,Php,Android,Json,Xampp,Google Compute Engine,我正在将JSON对象从Android应用程序发送到服务器。在我开发的本地机器中,它运行良好。我得到数据并解码。但当我将服务器部署到计算引擎时,我不会在服务器端接收数据。数据从Android客户端发送。我还从浏览器中测试了JSON,得到了200个响应代码。以下是代码片段: //客户 //在这里创建JSONObject 这是我正在发送的JSON{“test”,“1234”} //服务器 <?php $json = file_get_contents("php://input");
<?php
$json = file_get_contents("php://input");
var_dump($json); //this returns String(0)
$decoded = json_decode($json, TRUE);
$pasid = $decoded['test'];
echo"test if it works";
var_dump($pasid); //returns null
向服务器发送post请求后,您可以使用以下代码获取post正文:
<?php
$post_body = file_get_contents("php://input");
$json = json_decode($post_body);
$pasid = $json->{"test"};
echo $pasid;
?>
向服务器发送post请求后,您可以使用以下代码获取帖子正文:
<?php
$post_body = file_get_contents("php://input");
$json = json_decode($post_body);
$pasid = $json->{"test"};
echo $pasid;
?>
您的代码非常混乱
这不是将数据从应用程序发送到远程数据库的好方法
你可以这样做`
形成应用程序
String uri="But your http URL";
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStreamWriter writer=new OutputStreamWriter(con.getOutputStream());
StringBuilder sb=new StringBuilder();
sb.append("username="+"Naham");
sb.append("password="+"passNaham");
writer.write(sb.toString());
writer.flush();
了解简单的php web服务
$data = array("result" => 0, "message" => "Error!");
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$user_name = isset($_POST['username']) ? $_POST['user_name'] : "";
$user_password = isset($_POST['user_password']) ? $_POST['user_password'] : "";
// do some thing here
$data = array("result" => 1, "message" => "welcome");
} else
$data = array("result" => 0, "message" => "Error!");
/* Output header */
header('Content-type: application/json');
echo json_encode($data);
?>
$data=数组(“结果”=>0,“消息”=>“错误!”);
如果($\u服务器['REQUEST\u METHOD']==“POST”){
$user\u name=isset($\u POST['username'])?$\u POST['user\u name']:“”;
$user\u password=isset($\u POST['user\u password'])?$\u POST['user\u password']:“”;
//在这里做点什么
$data=数组(“结果”=>1,“消息”=>“欢迎”);
}否则
$data=数组(“结果”=>0,“消息”=>“错误!”);
/*输出标题*/
标题('Content-type:application/json');
echo json_编码($data);
?>
你的代码很混乱
这不是将数据从应用程序发送到远程数据库的好方法
你可以这样做`
形成应用程序
String uri="But your http URL";
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStreamWriter writer=new OutputStreamWriter(con.getOutputStream());
StringBuilder sb=new StringBuilder();
sb.append("username="+"Naham");
sb.append("password="+"passNaham");
writer.write(sb.toString());
writer.flush();
了解简单的php web服务
$data = array("result" => 0, "message" => "Error!");
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$user_name = isset($_POST['username']) ? $_POST['user_name'] : "";
$user_password = isset($_POST['user_password']) ? $_POST['user_password'] : "";
// do some thing here
$data = array("result" => 1, "message" => "welcome");
} else
$data = array("result" => 0, "message" => "Error!");
/* Output header */
header('Content-type: application/json');
echo json_encode($data);
?>
$data=数组(“结果”=>0,“消息”=>“错误!”);
如果($\u服务器['REQUEST\u METHOD']==“POST”){
$user\u name=isset($\u POST['username'])?$\u POST['user\u name']:“”;
$user\u password=isset($\u POST['user\u password'])?$\u POST['user\u password']:“”;
//在这里做点什么
$data=数组(“结果”=>1,“消息”=>“欢迎”);
}否则
$data=数组(“结果”=>0,“消息”=>“错误!”);
/*输出标题*/
标题('Content-type:application/json');
echo json_编码($data);
?>
原来问题是由HttpRLConnection的故障引起的。就像刚搬走一样`
urlConnection.setChunkedStreamingMode(0)
在哪里
`urlConnection=HttpUrlCooenction()
`结果表明,问题是由HTTPRL连接故障引起的。就像刚搬走一样`
urlConnection.setChunkedStreamingMode(0)
在哪里
`urlConnection=HttpUrlCooenction()
`如果您回显$json
,会发生什么?“这是你所期望的吗?”乔恩谢谢你提醒我。我现在更新了我的问题。返回字符串(0)。我期望从Android发送一个数值。如果您回显$json
,会发生什么?“这是你所期望的吗?”乔恩谢谢你提醒我。我现在更新了我的问题。返回字符串(0)。我希望从Android发送一个数字值。你必须使用var_dump($pasid)进行回送;你的代码和我的代码一样返回null;您的代码返回空值,就像我的一样。用户名不必与用户名匹配吗?为什么是用户名?应该是用户名吗?我在服务器端收到了字符串(0),但我在代码中使用的字符串不是空的。是的,你是对的,我将编辑该字符串,谢谢,欢迎兄弟。谢谢你的帮助。我还想提醒你,sb.append()只接受一个字符串参数,而不是两个。对不起,兄弟,我已经快速编写了代码。看,编辑后的PostUserName不必与用户名匹配吗?为什么是用户名?应该是用户名吗?我在服务器端收到了字符串(0),但我在代码中使用的字符串不是空的。是的,你是对的,我将编辑该字符串,谢谢,欢迎兄弟。谢谢你的帮助。我还想提醒你,sb.append()只接受一个字符串参数,而不是两个。对不起,兄弟,我已经编写了代码,请快速查看编辑后的文章