如何让PHP服务器将数据发送到Android设备发送到、服务器、数据、设备

2023-09-06 18:58:15 作者:许你万丈光芒

我想在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.