如何让PHP服务器将数据发送到Android设备 [英] how to make PHP server sends data to an Android device

查看:98
本文介绍了如何让PHP服务器将数据发送到Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android设备将数据发送到一个PHP服务器。收到后,PHP服务器发送其他数据到Android。第一部分可以通过使用JSON来完成。但是,我不知道如何让PHP服务器将数据发送到Android。对不起,我是新来的PHP!

I want the Android device to send data to a PHP server. After receiving, the PHP server sends other data to Android. The first part can be done by using JSON. However, I don't know how to make the PHP server sends data to Android. Sorry, I am new to PHP!

推荐答案

无论数据是由你的PHP脚本印将在应对Android设备上发出的呼吁被退回。

Whatever data is "printed" by your PHP script will be returned in the response to the call made on the Android device.

您可以做这样的事情在PHP中:

You can do something like this in PHP:

<?php
// TODO: Handle incoming data

// Send JSON data back to client
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');

// Compute data
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

// Encode/print data
echo json_encode($data);
?>

您想更换您的code中的第一个评论,以处理从客户端(Android版)提交的数据。然后,设置响应报头的类型为应用程序/ JSON 回声回你带codeD数据。

You would want to replace the first comment with your code to handle the data that was submitted from the client (Android). Then you set the response headers to be of type application/json and echo back your encoded data.

从技术上讲,你可以回声 / 打印回任何你想,但使用像JSON格式使得它更更容易去code在客户端的数据。

Technically you could echo/print back anything you would like, but using a format like JSON makes it much easier to decode the data on the client side.

这篇关于如何让PHP服务器将数据发送到Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆