My problem is, i keep track of users location per second, and it makes 60 values in an array in a minute. I wrote a simple php in my server to save the data to a mysql table. Because of the nature of the connection (as far as i know) , i can save a record per one request with simple httppost object on android side.(request is like : xxx.com/writelocation.php?lat=33.76&lon=45.0&alt=12000) I tried to solve this by using asynctask but it didnt work. Connection is slow and data is heavy.It takes more than a second for the connection to complete and at every second a new record comes.I am looking for a way to send multiple values at once, at every minute or so.So far, i couldn't find a solution other than using direct access to a remote database. But my hosting does not provide remote access to my mysql database.
I need a clever and cheap solution for my problem :) what do you suggest? is using remote database(by database hosting)only solution?
Thanks in advance.
PHP
With PHP it is possible to construct a multi-dimensional POST/GET array as follows:
(Assume this is part of the query string)
id[]=1&id[]=2&id[]=3
Produces:
array(1) {
"ID" => array(3) {
[1] => string(1) "1"
[2] => string(1) "2"
[3] => string(1) "3"
}
}
Similarly, if you put text between the square brackets, you can utilize PHP's associative array functionality:
form[fName]=John&form[lName]=Doe&form[age]=20
Produces
array(1) {
"form" => array(3) {
["fName"] => string(4) "John"
["lName"] => string(3) "Doe"
["age"] => string(2) "20"
}
}
Needless to say, this works with both POST and GET. You may be able to utilize it in your application, something like this:
loc1[lon]=longitude&loc1[lat]=latitude&loc2[lon]=longitude&loc2[lat]=latitude
So you get:
array(2) {
"loc1" => array(2) {
"longitude" => string(9) "longitude"
"latitude" => string(8) "latitude"
}
"loc2" => array(2) {
"longitude" => string(9) "longitude"
"latitude" => string(3) "latitude"
}
}
MySQL
Just a sidenote: Instead of running multiple insert queries, you may insert all the data at once and gain some speed advantages. eg.
INSERT INTO `users` ( `fName`, `lName`, `age` ) VALUES ( "John", "Doe", "20" ),( "John", "Citizen", "42" )
In the above, we inserted two rows of data...
( "John", "Doe", "20" )
( "John", "Citizen", "20" )
Also, make sure your data is properly escaped. This may vary depending on the database API you use.
Json encoding is very fast and the bandwish consuming is really small. I suggest to yo to use json to send those data and then to parse the json on the server side and insert the data in the database.
To understand more abou json in android, see http://www.vogella.com/articles/AndroidJSON/article.html
What do you think about ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With