如何一次将多个值发送到Web服务器

时间:2021-07-08 22:09:59

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.

我的问题是,我每秒跟踪用户位置,并在一分钟内在数组中生成60个值。我在服务器上写了一个简单的php来将数据保存到mysql表中。由于连接的性质(据我所知),我可以在android端使用简单的httppost对象保存每个请求的记录。(请求如下:xxx.com/writelocation.php?lat=33.76&lon=45.0 &alt = 12000)我试图通过使用asynctask解决这个问题,但它没有用。连接速度很慢,数据很重。连接完成需要一秒多的时间,每隔一秒就会有一条新记录。我正在寻找一种方法,可以在每一分钟左右发送多个值。到目前为止,除了使用直接访问远程数据库之外,我找不到其他解决方案。但是我的托管不提供对我的mysql数据库的远程访问。

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.

提前致谢。

4 个解决方案

#1


2  

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.

Json编码速度非常快,而且带宽消耗非常小。我建议你使用json发送这些数据然后解析服务器端的json并将数据插入数据库。

To understand more abou json in android, see http://www.vogella.com/articles/AndroidJSON/article.html

要了解更多关于android的abou json,请参阅http://www.vogella.com/articles/AndroidJSON/article.html

What do you think about ?

你有什么想法 ?

#2


3  

PHP

With PHP it is possible to construct a multi-dimensional POST/GET array as follows:

使用PHP,可以构建一个多维POST / GET数组,如下所示:

(Assume this is part of the query string)
id[]=1&id[]=2&id[]=3

(假设这是查询字符串的一部分)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:

同样,如果在方括号之间放置文本,则可以使用PHP的关联数组功能:

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:

不用说,这适用于POST和GET。您可以在应用程序中使用它,如下所示:

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.

此外,请确保您的数据已正确转义。这可能会因您使用的数据库API而异。

#3


2  

use INSERT DELAYED instead of INSERT. also MySQL is not a really scalable solution for this case, maybe it's better to use MongoSQL fo this amount of requests. also you can save requests on the server as a text file and run queries as a batch every few seconds or minutes

使用INSERT DELAYED而不是INSERT。对于这种情况,MySQL也不是一个真正可扩展的解决方案,也许最好使用MongoSQL来满足这一要求。您还可以将请求作为文本文件保存在服务器上,并且每隔几秒或几分钟以批处理方式运行查询

#4


2  

Trying to send each new location every second is a fundamental design flaw, and I would recommend avoiding this strategy, because you cannot guarantee network availability and latency from a mobile device.

尝试每秒发送一个新位置是一个基本的设计缺陷,我建议避免这种策略,因为您无法保证移动设备的网络可用性和延迟。

The way to do it would be to buffer the data locally, and submit it over to the server in blocks. For example, collect ten minutes' worth of data and send that all in one go. Keep in mind that, aside from the network latency limitation that you'e already discovered, establishing and then tearing down connections has a network overhead and a battery life implication. Therefore it is better for many reasons that you buffer data locally and send the buffer after a sensible interval.

这样做的方法是在本地缓冲数据,并以块为单位将其提交给服务器。例如,收集十分钟的数据并一次性发送。请记住,除了您已经发现的网络延迟限制之外,建立然后拆除连接会产生网络开销和电池寿命限制。因此,由于许多原因,您可以在本地缓冲数据并在合理间隔后发送缓冲区。

Each time you send the data to the server, do so within a POST request. This is the kind of request that should be used for submitting data of any significant size, and it also means that the data could be encrypted using TLS if that is a requirement for you later.

每次将数据发送到服务器时,都要在POST请求中执行此操作。这是一种应该用于提交任何大小数据的请求,这也意味着如果以后需要,可以使用TLS对数据进行加密。

#1


2  

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.

Json编码速度非常快,而且带宽消耗非常小。我建议你使用json发送这些数据然后解析服务器端的json并将数据插入数据库。

To understand more abou json in android, see http://www.vogella.com/articles/AndroidJSON/article.html

要了解更多关于android的abou json,请参阅http://www.vogella.com/articles/AndroidJSON/article.html

What do you think about ?

你有什么想法 ?

#2


3  

PHP

With PHP it is possible to construct a multi-dimensional POST/GET array as follows:

使用PHP,可以构建一个多维POST / GET数组,如下所示:

(Assume this is part of the query string)
id[]=1&id[]=2&id[]=3

(假设这是查询字符串的一部分)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:

同样,如果在方括号之间放置文本,则可以使用PHP的关联数组功能:

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:

不用说,这适用于POST和GET。您可以在应用程序中使用它,如下所示:

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.

此外,请确保您的数据已正确转义。这可能会因您使用的数据库API而异。

#3


2  

use INSERT DELAYED instead of INSERT. also MySQL is not a really scalable solution for this case, maybe it's better to use MongoSQL fo this amount of requests. also you can save requests on the server as a text file and run queries as a batch every few seconds or minutes

使用INSERT DELAYED而不是INSERT。对于这种情况,MySQL也不是一个真正可扩展的解决方案,也许最好使用MongoSQL来满足这一要求。您还可以将请求作为文本文件保存在服务器上,并且每隔几秒或几分钟以批处理方式运行查询

#4


2  

Trying to send each new location every second is a fundamental design flaw, and I would recommend avoiding this strategy, because you cannot guarantee network availability and latency from a mobile device.

尝试每秒发送一个新位置是一个基本的设计缺陷,我建议避免这种策略,因为您无法保证移动设备的网络可用性和延迟。

The way to do it would be to buffer the data locally, and submit it over to the server in blocks. For example, collect ten minutes' worth of data and send that all in one go. Keep in mind that, aside from the network latency limitation that you'e already discovered, establishing and then tearing down connections has a network overhead and a battery life implication. Therefore it is better for many reasons that you buffer data locally and send the buffer after a sensible interval.

这样做的方法是在本地缓冲数据,并以块为单位将其提交给服务器。例如,收集十分钟的数据并一次性发送。请记住,除了您已经发现的网络延迟限制之外,建立然后拆除连接会产生网络开销和电池寿命限制。因此,由于许多原因,您可以在本地缓冲数据并在合理间隔后发送缓冲区。

Each time you send the data to the server, do so within a POST request. This is the kind of request that should be used for submitting data of any significant size, and it also means that the data could be encrypted using TLS if that is a requirement for you later.

每次将数据发送到服务器时,都要在POST请求中执行此操作。这是一种应该用于提交任何大小数据的请求,这也意味着如果以后需要,可以使用TLS对数据进行加密。