JSON全圈(PHP)

时间:2013-03-10 03:00:55

标签: php mysql json apache wampserver

我已经沦为最简单的形式,我仍然磕磕绊绊......我花了30多个小时进行研究和测试。根据从未显示超过整个圆圈15°的所有帖子,这应该是非常容易的。

我想:

  1. 从Android手机向WAMP服务器发送查询参数(以JSON格式)...这可能与本地SQLite表的完整转储一样多,因此查询字符串不会削减它。
  2. 让WAMP服务器读取JSON数据,制定SQL查询并提交到mySQL数据库
  3. 将响应打包为JSON数据(从简单的“OK”转换为完整的表转储)
  4. 将响应包退回Android手机
  5. 这已经是一个功能齐全的WAMP应用程序了,我希望集成Android访问权限。出于这个原因,我确实想要避免使用AJAX,因为我希望与已经存在的内容保持一致。

    我把它减少到最简单的循环并且遇到障碍。我正在使用send.php将一些JSON数据发布到receive.php。此时,我只需要receive.php来读取数据并将其发回(略微修改)到send.php

    send.php正在读取从receive.php发送的股票JSON。我只是无法获得任何生命迹象,即使接收到它也会识别发送给它的JSON。

    PLEASE 不要指引我走向cURL ...从我发现的关于Android和JSON的所有内容中,cURL是一个切线,它会将我的完整循环发送回非功能性。

    APACHE 2.2.22,PHP 5.4.3

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

    send.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);
    ?>
    

1 个答案:

答案 0 :(得分:0)

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

我选择简单来证明我可以走完整圈,现在我已经这样做了。

send.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"));
    }