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.
Related
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 would like some advice. I'm going to be using an sqlite database that will be pulling down information from my server and then saving it in the DB then displaying it. Could someone advise me of the best way to populate the DB, should I...
Use a http request and return a string de-liminated with say a | and use a loop to write to the data base.
Use a JSON to retrieve the information and then store it in the database.
The information is going to be just text and some fields will contains links to images I want to then download (get to that later). Just wanted some advice on best practices. I have done some searches on SO and other sites but can't find much advice. Also as a side note any examples you know of that are good for noobs :)
Based on what you write here I would pick JSON.
To core points:
JSON is a standard format.
Android ships with a JSON lib (org.json) making it easy to handle it (encode / decode data).
JSON is known by a large community so you can ask questions and get them answered rather easily. With a custom format you cannot tag the question as 'json' here at SO... ;-)
Using standard formats and libraries helps you to avoid designing and implementing this stuff yourself, which makes your software more robust.
Sometime later you might need to add more complex data to your project. By that time it will be rather straightforward to use JSON's array and objects. With your private scheme you will have to add this capability to it and extend your parsing code. That can easily introduce subtle bugs. Or you might decide at that point that it is too hard with your custom format and decide to move over to a standard like JSON, XML, etc. At that time it costs you much more to shift over than if you start with a standard format. Consider time invested to write and test the current code and then the extra time to change to the standard format for the current system.
I have a project which requires the json objects to be saved in database and then based on the users action will display info.
I can get the json from the API using here:http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ but need help with parsing the following which is a part of json objects.
It really depends on your data structure and amount of entries. If amount is small and queries / sorting requirements are simple, you may just bypass database entirely and parse JSON with buil it (not so good solution in terman of memory consumption) or pull parser ( like GSON - small external dependency bu way better perfromance )
Another option would be just store JSON documents in database, and some fields from them as separate columns for querying and indexing.
If you like to have complex object hierarchy you will find that you are reimplementing
some ORM solution.
I'm writing an application that communicates with a web API, which responds with JSON. Currently, I'm translating the JSON objects to Java objects using gson (which is awesome, by the way).
Now, I want to store some of these objects in an SQLite database. However, they have lots of properties that would never be used in queries (i.e. I won't be ORDERing, WHEREing, or anything like that with those properties), so I feel it's unnecessary to create columns for all of them. What I'm thinking of doing is:
Only have columns for the essential data that will be used when querying the database
Have one TEXT or BLOB column (which one do you recommend?) that stores the actual JSON, so I can recreate my Java object from it and access all the data.
This would both make my life easier and streamline my code (I would not have to write very different code when dealing with data from the API vs. data from the database).
However, although I see no downsides, it feels a bit fishy.
What kind of trouble do you think I would run into if I use this technique?
The main thing I wouldn't like about it is relying on the structure of the stored/retrieved JSON to be valid, since it's completely out of the hands of the database. Not that you can't take precautions against possible issues, but if the JSON is somehow truncated or otherwise compromised in a way that trips up the parser, you're then missing the entire object instead of just one invalid or truncated property. If that's an acceptable risk, then it's probably a reasonable technique.
I am making an Android app, and I need to connect to a remote database with a webservice.
I will receive an XML file from the webservice with the results of my SELECT query (various rows in some cases).
I don't know much about XML or web services, I only know that I will receive an XML file and that I have to parse it to obtain the data.
Is there an XML parser for this purpose that is easy to add to my app and configure?
I would suggest not using XML and instead use JSON. JSON is much cleaner and much less in kilobytes. Then I would use Jackson to deserialize the JSON string to an object. You really don't need to do any work. Just point to the URL and you get an object back. You just have to make sure on the webservice you also generate correct JSON, which you can also use Jackson.
Here is how easy it is:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
User user = mapper.readValue(new File("user.json"), User.class); // can use File, URL, String!
Designing XML parsers are not very difficult. You can probably google for xml parsers and then change them to look for the tags you need or values.
Google is your friend.
You don't need an in depth knowledge about XML but you should know a bit about it. A recommended read would be this article. You should also at least know how the server is handling the communication. Is it a unidirectional communication (your application only fetching data) or is it a bidirectional communication (you are sending requests to the server). If the later how is the server handling them and so on. A bit of background knowledge is required.
I always recommend using the SAX parser as it seems so be the most efficient one due to its concept (being event based). A good read about dealing with XML files on Android can be found here. And also don't forget to search for similar questions here on StackOverflow as it is quite a popular question with some quality answers.