I need to create an architecture design for my application.
App Details: In my app, i show some data which is fetched from server. So basically when you start the app, you get a list of categories and when you click on a category, application calls a web service using REST and get a JSON response. After getting the response, JSON data is parsed to create an Arraylist of objects and finally an adapter shows this data in a list view.
Package structure which i have thought of:
com.app.activities: contains all the activities required in the application.
com.app.customviews: custom views required for the application.
com.app.adapters: different list/grid adapters to create different types of list and grid views.
com.app.parsers: contains all parser classes to parse the JSON response received from the server. These classes basically will return an arraylist to the activities which will be further used by adapter class for creating list and grid views.
com.app.utils: contains functions which are used through out the application like function for getting the response from server for a request, getting a string from the inputstream, download an image from a certain url, logging etc.
com.app.model: contains all the model classes for various user-defined data types.
App work flow: When a certain category is selected, activity gets the response from utils and send it to parsers to get an arraylist of Model type. Now this arraylist is passed to the adapters which returns an adapter object which is finally used in showing the list/grid in the activity.
Now as per the application architecture, your code should be divided into following three layers:
Presentation Layer
Business Layer
Data Layer
Now i need to know, as per my application which part belong to which layer.
Please help, i am totally clueless about this.
thanks!!
Update: While googling i stumbled upon this link:
http://apparchguide.codeplex.com/wikipage?title=Chapter%2019%20-%20Mobile%20Applications
It says, your application should have some workflows, business components, entities etc.
So, i think my current package structure is incorrect as i am doing most of thing in Activities only.
So now, my question is: If i follow this architecture, what should be package structure or how do i setup my code base.
Is your app is only dedicated to presentation? The business layer is implemented in the server, since you do not change data, only show it.
For me model is in the data layer, all the rest is presentation.
your UI of application is, you can say it presentation layer. In business layer you do operations with REST and JSON web service. And the data layer resides on server. So basically you display the listview(Presentation) by using some services(Business) to get result(information) from server(Data).
Related
I'm writing some Android app which pulls data in JSON format and I'm using retrofit library.
I've got one question, do I need to make POJO class which contains all fields that JSON object has? because in my case JSON objects from API have about 40 fields and I need only few of them.
I have done some test with sample app. There were JSONs containing 3 fields and when commented everything related to one single field app stopped loading and showing any results (no adapter attached error).
So I'm guessing that I have to make call for full JSON object and eventually not inflate all data into views?
Hello Dear Developers,
I am working on a project which uses Flickr API, I parsed the data and took the necessary values from JSONArray and JSONObject, however the URL does not provide all the necessary information about the user (the user who shared the photo on Flickr ) for this reason I decided to use flickr.people.getInfo but in this situation, how can I handle response JSON, I mean if I parse this URL, I have to change RecyclerView Adapter, ViewHolder, but it is not sufficient way of handling multiple different API calls.
I hope, the explanation is clear, if it is not pleasing make comment then I will provide answers to your specific questions.
If I understood correctly you should make two calls, then merge responses together and show result into UI.
You can handle this with callbacks or RxJava observables.
The main idea is to create a single class - model that will contain all data you need to present on your UI. Then make the first network call and parse the response into this object. Pass this object to the next network call and update it with the corresponding user data. And only after these two calls are done and you collected all needed data you can insert the model into RecyclerView.
Hope it helps.
Question: Assuming I have a web service which returns a JSON which I deserialize into a (list of) POJO(s), should each Fragment get a copy of the datasource or should I keep the datasource only in the activity and let the fragment(s) get the data via a callback interface (which I set in onAttach())? Or is there even another, better way?
WHY
I work with Android for quiet a while now but I think I still have not fully understood some very basic principles and I hope you can help me clarify things. In one of my applications, I use Volley to call a web service which returns JSON formatted data.
I use GSON to deserialize the JSON into POJOs. I then use greenrobots EventBus to send the new data around so I can update the UI. Currently all my fragments are registered as subscribers and each fragment stores a reference to the data.
I think I am doing this wrong because what if a fragment is currently not being displayed (they unregister from EventBus in onStop()). They could miss an update of the model and when a Backstack is popped, they could display an outdated model, right?
So what would be the best way to store the model so that all my UI components and controllers always display the latest version of the model? I'm fearing that one or the other component (Fragment and/or Activity) might miss an update and then displays outdated data.
What is Androids way to store models retrieved from web services and make them accessible for activities and fragments?
I think my main problem is that I can refresh the data in multiple activities and fragments. For example I could reload the data after a pull-to-refresh of a list of entries but renaming an entry would also cause an update - but only of this particular entry.
Have you thought about using sqlite to store the data and then displaying it?
What I would do is as follows:
Fetch data from service as a json
Parse the json and store it in a sqlite table
Read from the sqlite table as and when required
The advantage in doing so is that you are able to store new data as well as update the old one and as you said you can use pull down to refresh functionality, this proves helpful to store the latest data as well as the old one and always display the latest data as you are fetching from a datasource that is being updated.
I'm trying to figure out the best strategy for data caching in Android, particularly in view of using the data binding library.
The Android documentation really seems to put a lot of emphasis on using the built-in SQLite database and to access this with the help of content providers. However, assuming that my app retrieves data in JSON format from a REST API, using this strategy would entail the following:
Make HTTP request --> Parse JSON --> Insert results into DB
--> Call content provider --> Build model from cursor --> Bind to view
Not only does this seem like a very roundabout way of doing something relatively simple, but assuming all this happens when the user first opens the app the result would be lots and lots of waiting before anything useful appears on the screen.
To speed things up I may then decide to create my model earlier and let the caching take place in a separate thread, like this:
Make HTTP request --> Build model from JSON --> Bind to view
-->(NEW THREAD) --> Insert results into DB
Once the data is cached, the next time the user opens the app the following would take place:
Call content provider --> Build model from cursor --> Bind to view
But of course this would add even more complexity, for example forcing me to maintain code to build the model from two sources: JSON and the cursors returned by the content provider.
Given the above I am even more tempted to do away with the SQLite/ContentProvider model and instead do the following:
Make HTTP request --> Build Model and Store JSON to file --> Bind to view
But while this would greatly reduce boilerplate, parsing (plenty of libraries available for JSON parsing) and overall complexity it would also mean I cannot take advantage of content providers nor of SQLite's functionality.
So the question is, which model should I follow? Are there situations in which one is better than the other? Or are there better ways of handling this process that I am not aware of?
A possible solution to your problem could be the one used in the Google IO app. They parse JSON available on a remote server and display that content directly. In order to avoid empty screens they use a bootstrap json file containing an image of the data available on the server. All this info is available here. More concretely:
Bootstrap data
When the user runs the app for the first time, they expect to see data. However, if we relied only on the sync mechanism to bring data into the app, a first-time user would stare at a blank screen while waiting for a sync to happen, and this would be a bad user experience.
This is why IOSched ships with preloaded "bootstrap data", which is essentially a preloaded offline snapshot of the JSON data. This data is parsed by the app and saved to the database on first execution.
You can find this file in res/raw/bootstrap.json. It is simply a text file with a combined snapshot of the JSON files on the server.
To get Data for my application, I parse a Json file, with Jackson, to (lists of) custom Objects. When I start my app, I check if there is a new Json file available and ask the user if they want to download it, else I use the "old" Json file. But every time I start my app I parse the Json. Then I use the Application Class to save my list of objects an go to my data when I want, most of the time I only need one object.
From the huge list, with multiple layer nested object, I create a simple "flat" arraylist of custom objects in which I put only the data I need to create listviews (name, id, second text and url of picture). When something is clicked, i use the id to get all the data.
Parsing this whole Json file every time is pretty time consuming and makes the startup time of my application long. Ofcourse, this sucks.
And having this huge list of custom objects saved in Application Class fills a lot of memory of my device, and sometimes after some use the class gets killed, and I need to reparse again.
Is there a way I don't need to reparse all my data?
I hoped for a process like this:
new Json file
first time parse total JSON to list of multilayered custom objects
create simple list for listviews
delete/clear the big list
some clever way to get only one of the giant items, without keeping the whole list in my memory. (maybe something with Jackson).
on destroying of the application maybe save the simple list, i read something about parceable or serializable?
Anyone knows how to achieve this?
Or has an other awesome idea?
Jackson has a streaming api. Also you can parse the json in a AsyncTask (in the background) and update your user interface once the new data is ready
I'd probably store the data in a SQLite database, in line with how the Android platform was designed.
As an alternative to streaming Jackson API (which is very fast, but still has to scan through most of the content), perhaps you could just save things in different files, one per entry? Or, if there is a way to group things, in multiple files each having some subset?
Of course, if you really have tons of entries, use of SQLite as Bruce suggested makes lots of sense.