I need to persist a object. Unfortunately sqlite in android does not support ORM. Therefore I need to choose a different way to persist my data.
Is it better to persist the data via java serialization (ObjectOutputStream) within a blob, or via Gson's json serialization as a json string? Afterwards I need to serialize the data again as a json string in order to transmit it to a restful wcf service.
I'm worried about the serialization speed because afaik working with strings isn't very efficient.
In my experience serialization works faster than gson, but I haven't done any test on it. If speed is of concern I'd recommend you to do that test yourself.
On another note, you can use ORM on java/Android, read here: Any good ORM tools for Android development?
Related
I have always used a fromJson method to convert my json object to my model object. So let's say that my JSON has a car field which has my car models data, so I always parse the Json. Now I found out that it's also a common practice to use the json objects directly in the application. That has gotten me thinking
should I parse the json back to my models, if yes then why and if no then why not?
It depends but in general - yes, you should have transformation logic to convert your jsons (essentially, DTOs) into your entities (models as you call them). Here is my reasoning:
Your entities will likely be different from corresponding DTOs. As an example, your json model can have date/time information as long UTC msecs, but your entity model will likely be more usable if it has localized Date's
If you're exposing your entities (for example, as part of a library), you'll have much more flexibility in making changes to your remote apis (if such exist of course) without breaking your library's consumers codes
It completely depends on your project structure, if you have POJO classes defined in the models then converting them is the way to go. It makes your code more readable and fulfils your model which makes fetching the data easier. If you use a JSONObject directly then pulling data from it requires a bit more code. In terms of performance they both should have equal impacts since both are 'Objects' and will consume resources equally.
I vote for parsing them back to Models because of the simplicity it
enables for using those values further in your coding.
I cannot find any explanation about why we use JSON when parsing it in Android?. Are we required to use it or is this just optional? What's the advantage and disadvantage of using it to extract data from the internet? Do we have to encode every data from PHP & MySQL into a JSON Format?
See this question. JSON is known as JavaScript Object Notation, it's a lot easier to parse then XML data, and there are libraries to help you retrieve the values from a JSON structure. As for the PHP & MySQL question, Android doesn't use MySQL, it uses SQLite instead. Hope this helps.
JSON is just a format to represent Objects in textform. Its used often in Android because its nice to work with and has many libraries to help parse it.
Sometimes other formats are used like XML or something else to represent the data.
You can use any communication protocol you want, json is just an accepted standard. You can't just 'dump the mysql database' to the app, there has to be some sort of communication protocol that the server and client agree on to transport the data.
Some alternatives are xml or a binary format (such as protobuf).
I'd recommend json due to the abundance of tools and literature on it.
(Heres a blog post I wrote on using Jackson to parse json in Android http://shmuelrosansky.com/jackson/android/2015/07/20/jackson-android/)
I need to know the purpose of using JSON in android ?
Please anyone tell me in a simple way...
Thanks
The same reason you'd use it on any platform. JSON is a way of storing and expressing information. It uses attribute-value pairs in a hierarchical structure. In Android specifically, you may need to download some information from a database, which could be stored in JSON and then read by your app. Alternatively, you could store data locally in JSON but there are probably better and more efficient ways to do that if you're not sending data across a network.
https://en.wikipedia.org/wiki/JSON
JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server
For example, you can get data Json if you work with database. Or if you work with some API's then you can get data in format Json.
For example an app could fetch data from a server. When using JSON to get the data, the traffic is quite small and the app can easily work with it.
For example you have a server with a database with recipes, and your app displays recipes, the app could ask the server for recipes, and it gets a JSON in return. for example:
{
name: 'Cookies'
ingredients: { 'Butter', 'Eggs', ... /* I don't know, I'm not a chef :D */
...
}
The app can then just read the properties and display them in a neat list ;)
JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way objects are built in JavaScript
Pls go through this link: http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/
JSON stands for JavaScript Object Notation
JSON is lightweight text-data interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
* JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.
Using JSON in Android is not different than using it on any other platform. The main advantage of the format (in comparison to XML for example) is the small size of the data. This is very important for mobile devices due to the scarce resource those application use - i.e. your mobile app should be able to run with little memory usage, slow internet connection and so on.
Besides Android's framework has built-in tools for parsing / creating JSON objects. Thus it is both easy and efficient to use JSON rather than XML. If you have any project specific reason to prefer another data presentation format - don't worry. It is perfectly fine NOT to use JSON as long as some other format is more suitable for your project.
In short JSON is usually the right choice due to its small footprint and easy of use.
I have an application where I use Jsoup to get HTML file from the internet and parse it into POJOs. I use a custom Content Provider then to persist my POJOs into an SQLite database. It's a lot of code, and certain things are tricky to implement, caching especially (i.e. how to determine that my object is already in database, how to manage expiration, etc.). From looking over the internet I understood that RoboSpice might come to the rescue, since in handles caching transparently. However, I haven't found any example on how to plug in custom parser (my results are neither JSON nor XML, just pure HTML which I'm parsing with Jsoup currently). I'd therefore appreciate if you could point me to some related example.
Here's a more detailed description of what I'm doing. My app reads certain website to get the lists of certain entries. Those entries are calendar-based, and I'm requesting them month by month. Every month's request returns me a list of entries from that month. I want to make those requests cacheable and queryable, therefore I need a database backend, so that I can run custom SQL queries against it. Which RoboSpice configuration should I use, which extensions, and which code samples could I refer to?
Thanks in advance.
It looks like a good idea to use RoboSpice here, but the way you want to use is a bit out of its natural scope.
Usually people annotation a Pojo, let's say for Jackson, and they request a webservice, then the result is parsed via jackson and you get your Pojo. RoboSpice will simply reformat your pojo into json using jackson as parsing / formatting is a considered a bijection.
In your case, you will have to call your own ObjectPersister for your Pojo class and take care of its persistence format yourself. As you store your pojos into a database, the RoboSpice ormlite module may help but it is still experimental.
Have a look at the sample of the ormlite module of RoboSpice.
I commonly see data stored in JSON objects/arrays in open sourced Android code.
Why/When should I use JSON over normal Java ArrayLists?
JSON is a standard for transferring data between servers. It's a format that can be understood by any language, whether it be Java, C#, PHP, Python, Ruby, or JavaScript. Since it's a widely recognized format, it's much easier to work with JSON.
Many Android apps do communicate with a server, so it was probably easier for those developers to transfer the data to and from their server, which may or may not have been using Java.
If your client android application need data from a web service, then JSON data type can be used instead of XML because it's less character cost and it's easier to read