3

我正在制作一个需要折线图的 Android 应用程序,因此我需要来自服务器的 (x,y) 坐标。

我们正在使用 Ruby on Rails 和 Heroku 构建后端(尽管我们也在考虑使用 Google AppEngine)。

通过网络(以 JSON 编码?)将数百个(x,y)坐标发送到 Android 设备的最佳方式是什么?

4

4 回答 4

6

如果您非常关心带宽,请将坐标打包成二进制格式。例如,假设 (x,y) 坐标的二维网格为 32 位整数。使用红宝石:

points = [[107897,598654], [876432,30001], [15,754689]]
# => [[107897, 598654], [876432, 30001], [15, 754689]]

# json size
points.to_json.length
# => 44

# make a byte stream of points
data = points.flatten.pack("V*")
# => "y\xA5\x01\x00~\"\t\x00\x90_\r\x001u\x00\x00\x0F\x00\x00\x00\x01\x84\v\x00"

# binary size
data.length
# => 24

# read a byte stream to points
points = data.unpack("V*").each_slice(2).to_a
# => [[107897, 598654], [876432, 30001], [15, 754689]] 
于 2012-04-25T00:31:53.070 回答
1

由于您正在生成折线图,您可能希望从 Google Chart 服务的简单和扩展文本编码中获得灵感。主要收获是您不需要传输实际值;只是它们的相对值和限制。

于 2012-04-26T05:57:14.597 回答
0

最好的方法可能是用JSONor对其进行编码XML

于 2012-04-24T23:44:30.983 回答
0

我没有 ruby​​ 经验,但是您可以按顺序发送坐标,用换行符分隔,在 XML 中(可能类似于:<coordinate use='errorbar'>1,2</coordinate>对于每个坐标 [在您的 ruby​​ 脚本中]),用换行符分隔,然后下载结果安卓使用:

public static String[] download(String web_url) throws IOException{
    URL website = new URL(web_url);
    BufferedReader in = new BufferedReader(
              new InputStreamReader(
              website.openStream()));
    String input;
    ArrayList<String> stringList = new ArrayList<String>();
    while ((input = in.readLine()) != null) {
        stringList.add(input);
    }
    String[] itemArray = new String[stringList.size()];
    String[] returnedArray = stringList.toArray(itemArray);
    return returnedArray;
}
// Of course, call this function with web_url as the URL to your script

这将为您提供 XML 输出,每行作为数组中的一个条目,然后您只需解析 XML。如果你确定你永远不需要额外的数据,你可以用纯文本发送它,而不需要解析 XML。

于 2012-04-24T23:56:42.290 回答