0

I had built an Android app which tracks vehicle location (Android device).My app updates the locations and speed to the server @ every t seconds, using mobile data.App also monitors the vehicle speed which is under the speed limit or not.Definitely my app will not be able to contact server if the data connectivity is lost. At this point which is the best practice to store the location data in the device.

My plan is to store the datas in an xml file and update it to server later.Since the app wont update each and every locations but only after 19 secs or greater, I think this is the simplest option.please guide me in right path, if im wrong. And also I dont know how to update the bunch of data from xml to server.

My current location saving strategy is: Calling a php page(asyncTask) on server by passing location and other relevent datas. But how will I implement if I have a bunch of datas!

Shan
  • 1,081
  • 1
  • 12
  • 35

3 Answers3

1

You can store your data in any format: SQLite or Text file with XML/JSON/CSV. But personally I would store it in SQLite. The reason is following. You can create two parallel tasks, one of which collects data into SQLite database and the other sends bunches of that data to the server asynchronously when it's possible. SQLite is more convenient to organize this than text files, since you always work with records.

About sending a bunches of data to the server. You can send your data as a JSON inside a BODY of a HTTP POST request. Then on a server you should read this data and save it to the server database in one SQL statement. This is definitely possible, it's called a batch INSERT statement.

This is example of sending JSON data in a BODY of HTTP POST request:

HttpPost httpPost = new HttpPost(urlString);
String bodyJsonString = this.getBodyJsonString();
httpPost.setEntity(new StringEntity(bodyJsonString, HTTP.UTF_8));

@Override
String getBodyJsonString() {
    JSONObject jsonObject = new JSONObject();

    try {
        jsonObject.put("text", this.message);
        jsonObject.put("theme", this.theme);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    String resultString = jsonObject.toString();
    Log.v(Helper.TAG, "resultString = " + resultString);

    return resultString;
}
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
  • u personally suggests me to use sqlite db? – Shan Mar 18 '14 at 14:53
  • Fine.. I respect your reputations and accepts this as an answer. Thanks for the assistance. – Shan Mar 18 '14 at 18:22
  • Text files are also very slow comparing to SQLite. Since the data structure in which you store your data is a string. For example, if you have a string like "234 345 656 2 3425 234" and want to find if 345 is there, it will be very slow, comparing to the situation, when you have those records organized in a table of database, because there they are arranged to something like a binary tree data structure, which has O(log(2)(n)) complexity, but linear search has O(n) complexity. – Denis Kutlubaev Mar 31 '14 at 16:52
0

If you will have alot of information, I think you should build a local database.

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

xanexpt
  • 713
  • 8
  • 20
0

I think nosql is good way to work with it.

get it on github:

couchbase lite on android

Alexander.Iljushkin
  • 4,519
  • 7
  • 29
  • 46
  • but In couchbase ,,datas are synced to couchbase server .right? – Shan Mar 18 '14 at 14:32
  • @Shan ya sure... if you're planning to get ur program on market, it's normal practice in creating client-server architecture. but for the first time this more simplier to create local db with sqlite, as explained in other comments – Alexander.Iljushkin Mar 19 '14 at 06:19