0

我已经简化为最简单的形式,但仍然在跌跌撞撞……我花了 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 是一个切线,它将让我回到无功能状态。

阿帕奇 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"
?>

接收.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);
?>
4

1 回答 1

0

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

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

发送.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>";
?>

接收.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"));
    }
于 2013-03-11T01:17:08.523 回答