简体   繁体   English

如何从Triptracker android应用程序接收php中的JSON数据?

[英]How to receive JSON data in php from Triptracker android app?

The app sends gps location to a php server of the user's setting. 该应用程序将gps位置发送到用户设置的php服务器。 I have a PHP MySQL model server. 我有一个PHP MySQL模型服务器。 I don't know how to receive the JSON that is sent from the app into my server's database. 我不知道如何接收从应用程序发送到服务器数据库的JSON。 ANy help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。 The code for this is at https://github.com/jcs/triptracker/blob/master/src/org/jcs/triptracker/TrackerService.java 此代码位于https://github.com/jcs/triptracker/blob/master/src/org/jcs/triptracker/TrackerService.java

The POSTed parameters are a locations array, each element being a hash including time (a Unix Timestamp from the Location service), latitude and longitude (two float values of arbitrary precision), and speed in meters per second. POSTed参数是一个locations数组,每个元素都是一个哈希,包括时间(Location服务的Unix Timestamp),纬度和经度(任意精度的两个浮点值)以及以米/秒为单位的速度。

Thanks in advance. 提前致谢。

You can use json_decode() to create an object out of the json you receive. 您可以使用json_decode()在收到的json之外创建一个对象。

An example would be: 一个例子是:

$data = json_decode($_POST['yourdata']);

Assuming that the JSON looks something like this: 假设JSON看起来像这样:

{"foo":"bar"}

You can now do: 您现在可以执行以下操作:

echo $data->foo;

Use this data in your mysql query. 在您的mysql查询中使用此数据。

you can get the variables in your php like this: 您可以像这样在您的php中获取变量:

$lat = $_REQUEST['lat'];
$lng = $_REQUEST['lng'];

Then you can insert that $lat and $lng in your database. 然后,您可以在数据库中插入$ lat和$ lng。

Example: 例:

function launchQuery($call)
{
    $link = mysqli_init();
    if (!$link) {
        die('mysqli_init failed');
    }

    if (!mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 1')) {
        die('Setting MYSQLI_INIT_COMMAND failed');
    }

    if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
        die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
    }

    if (!mysqli_real_connect($link, $dbhost, $dbuser, $dbpassword, $dbname, $dbport)) {
        die('Connect Error (' . mysqli_connect_errno() . ') '  . mysqli_connect_error());
    }       

    mysqli_query($link,"SET NAMES 'utf8'");
    $rs = mysqli_query($link, $call);

    mysqli_close($link);

    return $rs;
} 

launchQuery("insert into whatever_your_table_name_is (latitude, longitude) values (".$lat.",".$lng.");");

took me a while to decode the format but the following snippet is extracting the data from the App. 我花了一点时间来解码格式,但以下代码段正在从App中提取数据。 Note that trip tracker might submit an array of previous location points it couldn't transfer so far. 请注意,行程追踪器可能会提交一系列到目前为止无法转移的先前位置点。

<?php
$locations  = $_REQUEST['locations'];   // Get all POST arrays

i = 0;                                  // Choose first locations array

// Now you can access single fields with:
time      = $locations[i]['time']; 
latitude  = $locations[i]['latitude'];
longitude = $locations[i]['longitude'];
speed     = $locations[i]['speed'];

?>

Note that 'time' is MILLIseconds since epoch, for unix timestamp we'll need to divide by 1000 into a float to allow conversion. 请注意,“时间”是自纪元以来的MILLIseconds,对于unix时间戳,我们需要将其除以1000以成浮点数以便进行转换。 Again, there can be more than 1 locations array available, access the other arrays by counting up i in a loop or so. 同样,可以有1个以上的位置数组,可以通过在循环中加i来访问其他数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM