Create a list in an Android application

I want to know how to create a list in an Android application. I have all the information in my database and I want to load data from it every time I launch the application and make a list from it (id and title).

What is the best approach?

Should I make a PHP script that responds with an encoded JSON array with all the elements of the list, or should I create an XML file that generates every time the data in the database changes, which I import into the application every time it starts? or any other good way to do this?

Since all things are created using XML files in android, it seems like importing XML would be nice, right? And how to import an XML file from a web server into an application?

//Daniel

0
source share
2 answers

In your situation, I would choose JSON over XML for the whole reason indicated in the following post: http://ajaxian.com/archives/json-vs-xml-the-debate

In addition, the android has a built-in JSON Array, so you do not need to perform additional code transfer.

return new JSONArray("my json string goes here..."); 

Since we are talking about a mobile device, I will always generate changes in your php script, and not have full synchronization, since it will be much smaller in size than full synchronization. However, you will need to provide the user with the ability to complete a full re-synchronization, if applicable to your application. I would use a SQLite database to store data and update only the changes.

To make the stream smaller, you can gzip compress your output from php since it can be read by an Android device from the beginning. In my application, I compress 500 kB to ~ 110 kB before transfer, which greatly saves on performance. Here is a partial example of how to read a stream:

 InputStream in = null; HttpURLConnection httpConn = null; // you will have to write your on code for this bit. if (httpConn.getContentEncoding() != null) { String contentEncoding = httpConn.getContentEncoding().toString(); if (contentEncoding.contains("gzip")) { in = new GZIPInputStream(httpConn.getInputStream()); } } else { in = httpConn.getInputStream(); } 

I hope this all makes sense, it was a long day of programming :)

Stu

0
source

You can use JSON or XML.

You can use the web service approach or include your db in your application.

In fact, I most often prefer to create my sqlite3 database of my data and include it in the resource folder, which you can copy to the application data folder at startup.

Regarding copying your sqlite3 database from assets / to the db data directory, I found these instructions useful.

+1
source

Source: https://habr.com/ru/post/885393/


All Articles