1

I am a php/mysql developer learning android. I am creating an android app that receives info from my php app to create list views of different products which will open a web view of that product's detail.

Currently my php cms web application outputs xml lists for an iphone app.... (also, separately outputs html). I have full control of the php app so if there is a better way to output the data for the android app please let me know.

I have created code that reads the xml from the web and creates the list view. The list can be refreshed daily, so the data does not need to be read from the online xml every time the app starts.

So I was thinking to store the data retrieved locally to improve my apps responsiveness. there may be up to 500 product descriptions to be stored at any given time in up to 30 different xml lists. I am starting development with one xml list with about 30 products.

For best performance should i store the product info in a sqlLite db or should i store the actual xml file in the cache/db or some other method like application cache.

I also was think to create the update of the data as a service, would this be a good idea?

Traveling_Monk
  • 778
  • 2
  • 11
  • 28

2 Answers2

2

The most efficient way to store data is RAM. But if you want to cache it, then the most efficient way is Database.

I recommend you store your data in sqlite android database.

You could also consider zipping you xml for faster network transfer and unzipping through java.util.zip package classes. You could even consider a simpler format for transmitting data, less verbose than xml, using a datainput/outputstream. (I do that in of my apps and it works great)

Here are some details on data input / output stream method :

  • imagine a proprietary protocol for your data, only what you need. No tags, no attributes, just raw values in order.
  • on the client side, get an input stream on your data using URL.getContent() and cast it in input stream.
  • on the client side still, build a data input stream encapsulating your socket input stream and read data in order. Use readInt, readDouble, readUTF, and so on.
  • on the client side, from php, you need to find a way to save your data in a format that is compatible with the data format expected by the client. I can't tell much about PHP, I only program using java.

The advantage of this technique is that you save bandwith as there is only data and no verbose decoration due to xml. You should read about java specs to understand how double, int, strings are written in data output stream. But it can be hard using two languages to get the data right.

If php can't save format in a suitable way, use xml, it will be much simpler. First try with just plain xml, then give a try using a zip or tarball or xml file.

But all this is about speed gain during network connection.

The second part of what you have to do is to store each row of your list in a SQL table. Then you can retrieve it pretty fast using a CursorAdapter for your list view (it breaks the charming MVC model but it is quite fast !).

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Thanks Stéphane, can you expand on the RAM and datainput/outputstream methods? How would these work? if i chose the sqlite method should i save the whole xml as a record or should i parse and save the data in fields? – Traveling_Monk Jun 14 '11 at 17:00
  • @Traveling_Monk I will detail in anwser for data object input stream. For the RAM, I just meant that nothing is faster than having data in RAM, in memory, loaded in a list or adapter. But this is not enough if you don't want data to be erased when app exits. – Snicolas Jun 14 '11 at 17:03
1

Sorry about this, but it became too long to write as a comment. This is not intended to be an answer to your question, because in my opinion Stéphane answered very well. The best solution is indeed to store the data in an sqlite database. Then you need to create the class to be used as a connection between the data, the database and the app. I don't want to take credit for what is said here already (I, too, voted it up).

I'm concerned with the other suggestion (use of low level raw streams for data manipulation, the list steps on that answer). I strongly recommend you to avoid creating your own proprietary protocol. It goes like this:

  • I need to exchange data.
  • I don't want to deal with the hassle of integrating external APIs into my code.
  • I know I can write two 5 minute routines to read and write the data back and forth.
  • Therefore, I just created my own proprietary format for exchanging data!

It makes me cry whenever I need to deal with unknown, obscure and arbitrary sequence of data blobs. It's always good to remember why we should not use unknown formats:

  • Reinventing the wheel is counter-productive. It seems not, but on the middle term it is. You can adapt your project to other mediums (server-side, other platforms) easily.
  • Using off-the-shelf components help you scale your code later.
  • Whenever you need to adapt your solution to other technologies and mediums, you'll work faster. Otherwise, you would probably end up with ad hoc code solutions that are not (easily) extensible and interoperable.
  • Using off the shelf components enables you to leverage advances in that particular technology. That's particularly important when you are using Android APIs, as they are frequently optimized for performance later down the road (See Android's Designing for Performance). Rolling your own standards may result in a performance penalty.
  • Unless you document your protocol, it's extremely easy to forget the protocol you created yourself. Just give it enough time and it will happen: you'll need to relearn/remember. If you document, then you are just wasting the computational time of your brain.
  • You think you don't need to scale your work, but chances are you will most of the time.
  • When you do, you will wish you had learned how to easily and seamlessly integrate well known formats.
  • The learning curve is needed anyway. In my experience, when you learn, you actually integrate well known formats faster than imagining your own way of doing things.
  • Finally, trust your data to geniuses that take their lives into creating cohesive and intelligent standards. They know it better!

Finally, if the purpose is to avoid the typical verbosity of XML, for whatever reasons, you have several options. Right now I can think of CSV, but I'm no expert in data storage, so if you're not confortable with it, I'm sure you can find good alternatives with plenty of ready to use APIs.

Good luck!

davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • Thank you for the reasons why not to DIY your own stream, I was not looking forward to that method. CSV, hmm that might make insert into the db really easy, does sqlite on android support [LOAD DATA INFILE](http://dev.mysql.com/doc/refman/5.1/en/load-data.html)? or should i just read it like [this](http://stackoverflow.com/questions/2887119/populate-android-database-from-csv-file/4210473#4210473)? – Traveling_Monk Jun 14 '11 at 21:50
  • 1
    Good points @David. Sure it is always a decision that relies only on performance (or a poor obfuscation attempt) to create a proprietary protocole. Nevertheless, it's efficient. But as you mentioned it, csv could do the job or even better : JSON ! – Snicolas Jun 14 '11 at 21:52
  • Snicolas: +1 for JSON. You're right. It's efficient at first, but I wonder how much more efficient it is to be worth the trade-off. I know that rules are not carved in stone, common sense is (that's why I said your answer is 100% right). Say... for really small, bulk data app, that certainly can be applied, and XML would be inneficient. But on general apps, I don't think it's worth, and that difference is less and less perceptible when the app grows. But it's always good to return and see familiar code, or when someone deals with your code. I'm just a fan of XML because 1) it's readily (...) – davidcesarino Jun 15 '11 at 13:50
  • (...) available, very common, 2) it's more extensible, I believe: on this net age, XML is good for its extensibility and fitness for different purposes. 3) there are very slim implementations of XML that you can use, all conforming to the standards. They can be comparable to JSON in size (but I'm not sure about performance, as I never used JSON in Android). Anyway, since XML and (IIRC) JSON are both available in Android public API, I'm sure any alternative will benefit from eventual performance optimizations the Android team provides for the API. I'd chose whatever I'm comfortable with. (...) – davidcesarino Jun 15 '11 at 14:01
  • (...) Monk: I don't know. Perhaps the sqlite .import command would work? Try Googling. Good luck! – davidcesarino Jun 15 '11 at 14:04