2013-04-12 88 views
3

我想要采取一个特定的JSON文件,它是在Android设备上动态创建的,并使用HTTP POST请求将该数据发送到PHP脚本以存储到文本文件中供以后使用。最后我还需要做到这一点,以便将数据保存在MySQL数据库中,但我一次只能工作一步。什么样的JSON文件的格式看起来一个例子是这样的:从Android发送JSON数据到PHP并写入文本文件

{"timestamp": 1351181576.64078, "name": "engine_speed", "value": 714.0} 
{"timestamp": 1351181576.64578, "name": "vehicle_speed", "value": 0.0} 
{"timestamp": 1351181576.6507802, "name": "brake_pedal_status", "value": true} 

该文件是由对Android线动态创建的线,所以我不能一次发送整个文件,但在创建每个行,因为它我想发送到PHP脚本。我已经编写了一个基本的Android应用程序,当按下按钮时,它会使用JSONObject并使用HTTP Post将其发送到PHP脚本。现在我不担心将实际的JSON文件解析为要发送的对象,而只是使用两个测试JSONObjects。 PHP脚本获取第一个对象并将其存储到文本文件中没有问题,但是它的另一个对象一直保持为空,我无法弄清楚原因。我对PHP和Android编程比较陌生,不确定我是否以正确的方式发送和接收数据。以下是我的相关的Android应用程序的代码段:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final Button button = (Button) findViewById(R.id.sendData); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Perform action on click 
      Toast.makeText(MainActivity.this, "button was pressed", 
        Toast.LENGTH_SHORT).show(); 
      try { 
       JSONObject json = new JSONObject(); 
       json.put("timestamp", 1351181576.64078); 
       json.put("name", "engine_speed"); 
       json.put("value", 714.0); 
       postData(json); 

       JSONObject json2 = new JSONObject(); 
       json.put("timestamp", 1351181576.7207818); 
       json.put("name", "steering_wheel_angle"); 
       json.put("value", 11.1633); 
       postData(json2); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    });  
} 

public void postData(JSONObject json) throws JSONException { 
    HttpClient httpclient = new DefaultHttpClient(); 

    try { 
     HttpPost httppost = new HttpPost(URL); 

     List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);  
     nvp.add(new BasicNameValuePair("json", json.toString())); 
     //httppost.setHeader("Content-type", "application/json"); 
     httppost.setEntity(new UrlEncodedFormEntity(nvp)); 
     HttpResponse response = httpclient.execute(httppost); 

     if(response != null) { 
      InputStream is = response.getEntity().getContent(); 
      //input stream is response that can be shown back on android 
     } 

    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

下面是PHP代码从HTTP后取数据,并将其写入到一个文本文件:

<?php 
    $filename = __DIR__.DIRECTORY_SEPARATOR."jsontest.txt"; 
    $json = $_POST['json']; 
    $data = json_decode($json, TRUE); 

    if(is_null($json) == false){ 
     file_put_contents($filename, "$json \n", FILE_APPEND); 
     foreach ($data as $name => $value) { 
     file_put_contents($filename, "$name -> $value \t", FILE_APPEND); 
     } 
     file_put_contents($filename, "\n", FILE_APPEND); 
    } 

    $myFile = "jsontest.txt"; 
    $fh = fopen($myFile, 'r'); 
    $theData = fread($fh, filesize($myFile)); 
    fclose($fh); 
    ?> 

    <meta http-equiv="refresh" content="3" > 
    <html> 
     <!-- Displaying the data from text file, not really needed --> 
     <p><?php echo $theData; ?></p> 
    </html> 

眼下输出文本文件是这样的,有被保存的第一个对象完全正常,第二对象显示为空:

{"value":714,"timestamp":1.35118157664078E9,"name":"engine_speed"} 
value -> 714 timestamp -> 1351181576.64  name -> engine_speed  
{} 

回答

6

你的问题是在你的Android应用程序:

  JSONObject json = new JSONObject(); 
      json.put("timestamp", 1351181576.64078); 
      json.put("name", "engine_speed"); 
      json.put("value", 714.0); 
      postData(json); 

      JSONObject json2 = new JSONObject(); 
      json.put("timestamp", 1351181576.7207818); 
      json.put("name", "steering_wheel_angle"); 
      json.put("value", 11.1633); 
      postData(json2); 

你没有投入到json2任何数据,而是正在修改json。然后,您将完全未修改的 - 因此为空的 - json2对象发送给PHP,将其错误地打印为{}

如果您解决您的Android应用程序正确填写json2,事情应该工作:

  JSONObject json2 = new JSONObject(); 
      json2.put("timestamp", 1351181576.7207818); 
      json2.put("name", "steering_wheel_angle"); 
      json2.put("value", 11.1633); 
      postData(json2); 
+0

尼斯抓.......... – Triode

+0

噢,我的上帝,我觉得像这样一个白痴..它总是像我那样的小事。谢谢您的帮助! – mbecker73