0

私は最も単純な形に縮小しましたが、まだつまずいています...私は30時間以上かけて調査とテストを行いました。円全体の 15° を超えることは決してないすべての投稿によると、これは非常に簡単なはずです。

したい:

  1. Android Phone から WAMP サーバーに (JSON で) クエリ パラメーターを送信します...これは、ローカルの SQLite テーブルの完全なダンプと同じくらいになる可能性があるため、クエリ文字列ではうまくいきません。
  2. WAMP サーバーに JSON データを読み取らせ、SQL クエリを作成し、mySQL データベースに送信します。
  3. 応答を JSON データとしてパッケージ化します (単純な「OK」から完全なテーブル ダンプまで)
  4. 応答パッケージを Android フォンに返す

これはすでに完全に機能する WAMP アプリケーションであり、Android アクセスを統合したいと考えています。このため、既にあるものとの一貫性を維持したいので、AJAX は避けたいと思っています

私はこれを最も単純なループに減らしましたが、問題が発生しています。send.php を使用して、JSON データを receive.php に投稿しています。この時点で必要なのは、データを読み取って (少し変更して) send.php に送り返すための receive.php だけです。

send.php は receive.php から送信されたストック JSON を適切に読み取ります。receive.php が送信された JSON を認識することさえできません。

私をcURLに誘導しないでください... Android と JSON に関して私が見つけたすべてのことから、cURL は機能しない状態に完全に戻る接線です。

アパッチ 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);
?>
4

1 に答える 1

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"));
    }
于 2013-03-11T01:17:08.523 に答える