How to send many coordinates (x, y) over the network?

I am creating an Android application that requires a line graph, so I need the coordinates (x, y) from the server.

We are creating a back-end using Ruby on Rails with Heroku (although we are also considering the Google AppEngine app).

What's the best way to send hundreds (x, y) coordinates over the network (encoded in JSON?) To an Android device?

+3
source share
4 answers

If you're seriously concerned about bandwidth, set the coordinates to binary. For example, assuming that the 2nd grid of coordinates (x, y) will be a 32-bit integer. Using ruby:

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]] 
+6
source

, Google Chart. , ; .

+1

, , JSON XML.

0

ruby, , , XML (, - <coordinate use='errorbar'>1,2</coordinate> [ ruby ​​script]), android, :

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

This will give you XML output, each line will be like a record in an array, and then you just need to parse the XML. If you are sure that you do not need additional data, you can simply send it in plain text, without having to parse the XML.

0
source

All Articles