Home  >  Article  >  Backend Development  >  php服务器如何接受android客户端发送的json数据

php服务器如何接受android客户端发送的json数据

WBOY
WBOYOriginal
2016-06-06 20:17:371332browse

想要实现php服务器与android客户端间的通信,双方传递json数据,应该要怎么做呢?

<code>$reciver=file_get_contents('php://input');
  5 //echo $reciver;
  6 
  7 
  8 if(!empty($reciver))
  9 {
 10   $reciver=json_decode($reciver);
 11 
 12   $code='200';
 13   $message='I have receiver it';
 14   $data='received';
 15 }
 16 else
 17 {
 18   $code='400';
 19   $message='reciver bad';
 20   $data='not received ';
 21 }
 22 
 23 
 24 
 25 Response::responseResult
 26 ($code,$message,$data);
</code>

android客户端可以接受到发出的json数据,但服务器端好像无法接收,android端可以接受到服务器发送的数据。。

各位大神,求教啊。。

回复内容:

想要实现php服务器与android客户端间的通信,双方传递json数据,应该要怎么做呢?

<code>$reciver=file_get_contents('php://input');
  5 //echo $reciver;
  6 
  7 
  8 if(!empty($reciver))
  9 {
 10   $reciver=json_decode($reciver);
 11 
 12   $code='200';
 13   $message='I have receiver it';
 14   $data='received';
 15 }
 16 else
 17 {
 18   $code='400';
 19   $message='reciver bad';
 20   $data='not received ';
 21 }
 22 
 23 
 24 
 25 Response::responseResult
 26 ($code,$message,$data);
</code>

android客户端可以接受到发出的json数据,但服务器端好像无法接收,android端可以接受到服务器发送的数据。。

各位大神,求教啊。。

postman模拟一下发送数据,然后输出一下就可以看到结构。知道结构了,处理还不容易?

抓下包,看数据有没传过来

看看数据格式,查看日志

服务器日志把请求打印出来,看看客户端发送到是什么格式的数据,然后做处理。

服务端客户端都最好检查一下,服务器接收,客户端发送,是否存在问题。服务器端你可以使用chorme浏览器插件postman模拟发送请求,看看你后端程序是否有问题;客户端检查请求协议地址、参数、请求方式等是否符合服务端的协议设计。这种问题还是很容易找出来的。

话说你的服务端是否有跳转?没有跳转的话,使用你的代码一定可以获取到的

检查一下你客户端的请求头,用$_POST试试

Android没有发送post原始数据包你就没办法获取

如果用这种方式,Android端发起请求时,不能使用表单方式提交。只能将JSON串当成请求体POST过来才行。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn