【问题标题】:JSON full circle (PHP)JSON 整圈 (PHP)
【发布时间】:2013-03-10 03:00:55
【问题描述】:

我已经简化为最简单的形式,但仍然在跌跌撞撞……我花了 30 多个小时进行研究和测试。根据所有从不显示整个圆的 15° 以上的帖子,这应该很容易。

我想:

  1. 将查询参数(以 JSON 格式)从 Android 手机发送到 WAMP 服务器...这可能与本地 SQLite 表的完整转储一样多,因此查询字符串不会削减它。
  2. 让 WAMP 服务器读取 JSON 数据,制定 SQL 查询并提交到 mySQL 数据库
  3. 将响应打包为 JSON 数据(从简单的“OK”到完整的表转储)
  4. 将响应包返回给安卓手机

这已经是一个功能齐全的 WAMP 应用了,我想集成 Android 访问。出于这个原因,我真的想避免使用 AJAX,因为我想保持与已有内容的一致性。

我已将其简化为最简单的循环并且遇到了障碍。我正在使用 send.php 将一些 JSON 数据发布到 receive.php。此时,我只需要receive.php读取数据并将其发送回(稍作修改)到send.php

send.php 正在正确读取从 receive.php 发送的库存 JSON。我只是无法得到任何生命迹象,receive.php 甚至可以识别发送给它的 JSON。

请不要将我引向 cURL...从我发现的有关 Android 和 JSON 的所有内容来看,cURL 是一个切线,它将使我回到非功能性的完整循环。

APACHE 2.2.22、PHP 5.4.3

就像我说的,我已经把它简化为最简单的形式来展示一个完整的圆圈......

发送.php:

<?php
$url = "http://192.168.0.102:808/networks/json/receive.php";
$data = array(
        'param1'      => '12345',
        'param2'    => 'fghij'
);
$json_data = json_encode($data);

$options = array(
        'http' => array(
                'method'  => 'POST',
                'content' => $json_data,
                'header'=>  "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n" .
                'Content-Length: ' . strlen($json_data) . "\r\n"
        )
);

$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );

$response = json_decode( $result , true);
echo '[' . $response['param1'] . "]\n<br>";
//THIS WORKS!  send.php displays "Initialized"
?>

receive.php

<?php
$newparam = 'Initialized';
//HERE I NEED TO read the JSON data and do something

$data = array(
        'param1'      => $newparam,
        'param2'    => 'pqrst'
);

header('Content-type: application/json');
echo json_encode($data);
?>

【问题讨论】:

  • 在receive.php中使用error_log()寻找生命迹象。
  • @user2147564 在读取(在 receive.php 中)发送的 JSON 数据时尝试 json_decode($data)。
  • isset($_POST) 返回 true,但似乎不包含任何内容。如果我尝试访问 $_POST['content'],这从 by send.php 发送的标头看来是合乎逻辑的,那么那里什么都没有(错误)。我知道我需要 json_decode($data_from_send_php),但是如何检索传入的 JSON 数据?
  • 经过进一步测试,receive.php接收$_POST作为数组,但是count($_POST) = 0...问题(开始)是send.php
  • 只花了几百个帖子才找到它...发送实际上还可以-它正在正确地将 JSON 数据发送到 receive.php。 JSON 数据可通过 "$data_from_send_php = file_get_contents('php://input');" 检索

标签: php mysql json apache wampserver


【解决方案1】:

正如所有不完整的解释中所述,这实际上很容易......我得到了完整的工作终于

我选择了简单来证明我可以绕一圈旅行,现在我已经做到了。

发送.php

<?php 
//The URL of the page that will:
//  1. Receive the incoming data
//  2. Decode the data and do something with it
//  3. Package the results into JSON
//  4. Return the JSON to the originator
$url = "http://192.168.0.102:808/networks/json/receive.php";

//The JSON data to send to the page (above)
$data = array(
        'param1'      => 'abcde',
        'param2'    => 'fghij'
);
$json_data = json_encode($data);

//Prep the request to send to the web site
$options = array(
        'http' => array(
                'method'  => 'POST',
                'content' => $json_data,
                'header'=>  "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n"
        )
);
$context  = stream_context_create( $options );

//Make the request and grab the results
$result = file_get_contents( $url, false, $context );

//Decode the results
$response = json_decode( $result , true);

//Do something with the results
echo '[' . $response['param1'] . "]\n<br>";
?>

receive.php

<?php
//K.I.S.S. - Retrieve the incoming JSON data, decode it and send one value 
//back to send.php

//Grab the incoming JSON data (want error correction)
//THIS IS THE PART I WAS MISSING
$data_from_send_php = file_get_contents('php://input');

//Decode the JSON data
$json_data = json_decode($data_from_send_php, true);

//CAN DO:  read querystrings (can be used for user auth, specifying the 
//requestor's intents, etc)

//Retrieve a nugget from the JSON so it can be sent back to send.php
$newparam = $json_data["param2"];

//Prep the JSON to send back
$data = array(
        'param1'      => $newparam,
        'param2'    => 'pqrst'
);

//Tell send.php what kind of data it is receiving
header('Content-type: application/json');

//Give send.php the JSON data
echo json_encode($data);
?>

AND Android 集成...使用 Button.onClickListener 调用

public void getServerData() throws JSONException, ClientProtocolException, IOException {
    //Not critical, but part of my need...Preferences store the pieces to manage JSON
    //connections
    Context context = getApplicationContext();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String netURL = prefs.getString("NetworkURL", "");
    //  "http://192.168.0.102:808/networks/json"
    String dataPage = prefs.getString("DataPage", "");
    //  "/receive.php"

    //NEEDED - the URL to send to/receive from...
    String theURL = new String(netURL + dataPage);

    //Create JSON data to send to the server
    JSONObject json = new JSONObject();
    json.put("param1",Settings.System.getString(getContentResolver(),Settings.System.ANDROID_ID));
    json.put("param2","Android Data");

    //Prepare to commnucate with the server
    DefaultHttpClient httpClient = new DefaultHttpClient();
    ResponseHandler <String> resonseHandler = new BasicResponseHandler();
    HttpPost postMethod = new HttpPost(theURL);

    //Attach the JSON Data
    postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));

    //Send and Receive
    String response = httpClient.execute(postMethod,resonseHandler);

    //Begin reading and working with the returned data
    JSONObject obj = new JSONObject(response); 

    TextView tv_param1 = (TextView) findViewById(R.id.tv_json_1);
    tv_param1.setText(obj.getString("param1"));
    TextView tv_param2 = (TextView) findViewById(R.id.tv_json_2);
    tv_param2.setText(obj.getString("param2"));
    }

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多