Android : JSON data POST to server

on Friday, August 15, 2014


I want to POST accelerometer JSON data to a server. acc is acceleration variable.



acc.x represents accelerometer x axis valye
acc.y (same) y axis value
acc.z (same) z axis value


This is the function which POST acceleration data as JSON format.



var acc;

function request() {
alert("Entering request function");
alert(acc.x);
alert(acc.y);
alert(acc.z);
var fromData = json; //Here to put JSON format that is given in next codeblock
alert(JSON.stringify(fromData));

var fromDatan = JSON.stringify(fromData);
alert(fromDatan);
$.ajax({
url: "abc.com",
headers: {
"x-api-key": "abcd=",
"content-type": "application/json"
},
type: "POST",
data: fromDatan,

success: function(fromData, status, jqXHR) {
alert(JSON.stringify(fromData));
},

error: function(jqXHR, status) {
alert(JSON.stringify(jqXHR));
}
});
return false;

};


Now the server expects this JSON:



{
"version": "1.0.1",
"observations": [{
"sensor": "", //sensor ID to be put manually or by form
"record": [{
"starttime": "1-JAN-2014 15:30:00 IST",
"output": [{
"name": "", //in "", Accelerometer_X
"value": "" //in "" acc.x
}, {
"name": "", //in "", Accelerometer_Y
"value": "" //in "" acc.y
}, {
"name": "", //in "", Accelerometer_Z
"value": "" //in "" acc.z
}]
}]
}]
}


Now, in previous codeblock, in var formData, I have to assign this JSON with accelerometer variables mentioned above. But as I am new to these things o I am unable to guess how to do that. So please help me out :)


After assigning it in formData, it is expected that it will take accelerometer value and POST to server. in 1st code, to debug I added alert of x, y, z values and it is giving perfectly accelerometer data. Now I need to POST. So please review code and suggest please :)


0 comments:

Post a Comment