I am trying to take a specific JSON file that is dynamically created on an Android device and send that data using HTTP post requests to a PHP script to store into a text file for later use. Eventually I also need to make it so that the data is saved in a MySQL database but I am working one step at a time. An example of what the format of the JSON file looks is this:
{"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}
This file is dynamically created line by line on the Android, so I cannot send the whole file at once, but each line as it is created I want to send to the PHP script. I have written a basic Android application that, when a button is pressed, takes a JSONObject and sends it to the PHP script using HTTP Post. Right now I am not worrying about parsing the actual JSON file into objects to be sent, and am just using two test JSONObjects. The PHP script gets the first object and stores it into the text file no problem, but the other object its keeps reading as null, which I cannot figure out why. I am relatively new to PHP and Android programming and am not sure if I am sending and receiving the data in the right manner. Below are the segments of my related Android application code :
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();
}
}
Below is the PHP code to take the data from the HTTP Post and write it into a text file:
<?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>
Right now the output text file looks like this, with the first object being saved perfectly fine and the second object showing up as null:
{"value":714,"timestamp":1.35118157664078E9,"name":"engine_speed"}
value -> 714 timestamp -> 1351181576.64 name -> engine_speed
{}