2013-03-10 4 views
0

나는 가장 단순한 형태로 축소되었으며 여전히 걸림돌이되고 있습니다 ... 저는 30 시간 넘게 연구하고 테스트했습니다. 전체 서클 중 15 ° 이상을 표시하지 않는 모든 게시물에 따르면이 메시지는 매우 쉽습니다. WAMP 서버에 안드로이드 전화에서 (JSON)에JSON 전체 서클 (PHP)

  1. 보내기 쿼리 매개 변수 ...이, 로컬 SQLite는 테이블의 전체 덤프만큼 할 수있다 쿼리 문자열 있도록 :

    내가 원하는 그냥 자르지 않을거야.

  2. 는 WAMP 서버가 JSON 데이터를 읽을 SQL 쿼리를 공식화하고 MySQL 데이터베이스에
  3. 패키지
  4. 반환 응답 (간단한 "OK"전체 테이블 덤프에서) JSON 데이터로 응답을 제출 되세요 Android 폰에 패키지

이것은 이미 완전한 기능을 갖춘 WAMP 응용 프로그램이며 Android 액세스를 통합하려고합니다. 이런 이유로, 나는 이 실제로을 피하기를 원합니다, 왜냐하면 나는 이미 존재하는 것과 일관성을 유지하기를 원하기 때문입니다.

나는 이것을 가장 단순한 루프로 축소했으며 걸림돌을 치고있다. receive.php에 일부 JSON 데이터를 게시하기 위해 send.php를 사용하고 있습니다. 이 시점에서, 나는 단지 send.php에 데이터를 읽고 그것을 (약간 수정 한) send.php가 필요합니다.

send.php가 올바르게 읽는 주식 JSON은 receive.php에서 보내집니다. receive.php가 보낸 JSON을 인식하는 것조차도 삶의 흔적을 얻을 수 없습니다.

사항이 컬쪽으로 저를 직접하지 않습니다 ... 모든 것을에서 나는 안드로이드와 JSON에 대한 발견했습니다 컬은 nonfunctionality로 다시 나에게 완전한 원을 보내드립니다 접선이다. 내가 말했듯이, 나는 완전한 원을 보여주기 위해 간단한 형태이 감소했습니다 5.4.3

APACHE 2.2.22, PHP ...

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); 
?> 
+0

(호출)'삶의 흔적을 찾는다. – mkaatman

+0

@ user2147564 읽고있을 때 json_decode ($ data)를 사용해보십시오 (수신시.php) 보내진 JSON 데이터. – kalaero

+0

isset ($ _ POST)는 true를 반환하지만 아무것도 포함하지 않은 것처럼 보입니다. _by_send.php를 보낸 헤더에서 논리적으로 보이는 $ _POST [ 'content']에 액세스하려고하면 아무 것도 없습니다 (오류). json_decode ($ data_from_send_php)가 필요 하겠지만, 들어오는 JSON 데이터는 어떻게 가져 옵니까? – Chameleon

답변

0

그것은이다 실제로 쉽게 모든 불완전한 설명에 명시된 바와 같이 ... I g 일할 동그라미 전체 마침내

나는 완전한 원을 여행 할 수 있다는 것을 증명하기 위해 단순함을 선택했으며, 이제는 그렇게했습니다.

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

와 안드로이드의 통합 ... receive.php 사용`의 error_log 안쪽 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")); 
    }