I am working in an Android project designed for doctors. Doctors are required to authenticate when they open the app for the first time.
This authentication process is done through a HTTPS connection, using PHP code in the server-side that returns JSON code to the app, letting it know if the connection has been successful and, if it is the case, it also returns that doctor's list of patients. Let me show a piece of JSON code that would be returned in case of a successful log-in:
Obviously, if the log-in were unsuccessful, the "listOfPatients" attribute would carry no data. After the server generating this JSON code, the app would simply read through it using a JSON Parser.
Now imagine the doctor doesn't have just 3 patients, but 100 patients. And each patient doesn't have just 3 attributes ("Age", "Phone", "Smoker") but dozens of them appropriately nested where required. We would then have a somewhat large (but maybe not too complex) JSON code to read through.
In this project I am designing the Client code (i.e. the Android App), whereas the Server code is written by other guy. He is asking me how I'd like the server code to be written in order to facilitate the "Android Client - Server" interaction and achieve the best, smoothest user experience possible.
I answered (this is the really-short version of my answer; don't concern about the server-side-code security since it is not the goal of this question):
Create a login.php that allows for POSTS queries. My App would send "user" and "password" and the server would compare it with the database.
The server would then generate appropriate JSON code depending on the success of the doctor's log-in request.
The Android app would simply parse this JSON and display it to the user in form of list-views, and so on (the way I display this data to the doctor does not matter here in this question).
I was wondering two things:
Knowing that the JSON will contain hundreds of attributes, how efficient is this code? Is there a better way to achieve this functionality? How would you do it?
The vast majority of these attributes' values will change on a daily basis (for example, "bodyTemperature" or "bloodPressure"). Furthermore there will be "importantNotifications", where patients would notify their doctors in case of an emergency situation. I don't think it would be efficient to go through the entire process ("server create JSON ==> client read JSON ==> client display JSON") over and over again, minute after minute, hour after hour, day after day. There must be a better way to do it (maybe local storage? I would then have to discern which attributes to read only once a year ("age"), once a month ("phone"), once a day ("bodyTemperature") or every 30 minutes ("importantNotifications"); How could I then discriminate which values I'd need to read from the JSON in each session?)
Now you will be likely using GSON to parse the response from the server. Also you might define default values and tell the server not to return anything that is equal to default values like smoker - default NO (minimize the ammount of data to transfer). You are highly likely to display the patients in a ListView or RecyclerView.. Google a bit how to implement lazy loaders, meaning you tell the server to return just a few results, not all and when the user scrolls to end, you ask the server to give you more if there are any.
Also using caches on Android is a great way to save a couple of unnecessary requests to the server. You define of how long a cache is valid, say 5 mins and if you want to repopulate a list, check if it's still valid. But you should always leave a manual refresh option, SwipeToRefresh is a great and simple way to do just that.
Hope somebody else can have something more as I am interested in this also.
Related
I'm not sure where is the best place to ask this question, so maybe if this is the wrong place, someone can help move this question to a better group?
It has elements of programming, user experience and database, but doesn't really fit well into any one category!
I need to take data and display it in a graph on my site. This data is available from an API.
But I cant decide if it is best to just get this data from the API "live" when needed, or to save data from the API to a local (on my own server) database.
Both methods have pros and cons.
Getting the data live means more URL requests, more latency, and if the site is used by many users, may limit the API access. I 'assume' the site will always be available if using the data live. The API data is also restricted to the past 2000 historical data points.
If I use a cron job to request the data, say once an hour, and save it to my own database, then I am only calling the API once every hour. Accessing my own database should be faster than calling an API from a URL GET request when drawing my page. And if my site is up, then my database will be up, so I don't need to worry about the API site uptime. And I can store as many historical data points as I want to, if I am storing the data myself.
But it seems wasteful to simply duplicate data that is already existing elsewhere.
There could be millions of data points. Is it really sensible to store perhaps 50 millions pieces of data on my own server, when it already exists on an API?
From the user's perspective, there shouldn't be any difference as to which method I choose - other than perhaps if my site is up and the API site is down, in which case there would be missing data on my site.
I am torn between these two options and don't know how best to proceed with this.
I need some advice on website design.
Lets take example of twitter for my question. Lets say I am making twitter. Now on the home_page.php ,I need both, Data about tweets (Tweet id , who tweeted , tweet time etc. etc) and Data about the user( userId , username , user profile pic).
Now to display all this, I have two option in mind..
1) Making separate php files like tweets.php and userDetails.php. By using AJAX queries, I can get the data on the home_page.php.
2) Adding all the php code (connecting to db, fetching data ) in the home_page.php itself.
In option one, I need to make many HTTP requests, which (i think) will be load to the network. So it might slow down the website.
But option two, I will have a defined REST API. Which will be good of adding more features in the future.
Please give me some advice on picking the best. Also I am still a learner, so if there are more options of implementing this, please share.
In number 1 you're reliant on java-script which doesn't follow progressive enhancement or graceful degradation; if a user doesn't have JS they will see zero content which is obviously bad.
Split your code into manageable php files to make it easier to read and require them all in one main php file; this wont take any extra http requests because all the includes are done server side and 1 page is sent back.
You can add additional javascript to grab more "tweets" like twitter does, but dont make the main functionality rely on javascript.
Don't think of PHP applications as a collection of PHP files that map to different URLs. A single PHP file should handle all your requests and include functionality as needed.
In network programming, it's usually good to minimize the number of network requests, because each request introduces an overhead beyond the time it takes for the raw data to be transmitted (due to protocol-specific information being transmitted and the time it takes to establish a connection for example).
Don't rely on JavaScript. JavaScript can be used for usability enhancements, but must not be used to provide essential functionality of your application.
Adding to Kiee's answer:
It can also depend on the size of your content. If your tweets and user info is very large, the response the single PHP file will take considerable time to prepare and deliver. Then you should go for a "minimal viable response" (i.e. last 10 tweets + 10 most popular users, or similar).
But what you definitely will have to do: create an API to bring your page to life. No matter which approach you will use...
I have an html based application that allows users to store and search information in a mysql database. They run this on their own servers, so it isn't centralized. I'd like to add a function that allows them to see if their information corresponds to any known info in a central database, and if it isn't, they would have the option to add it to the central db. I'm not sure if the triggering script would be best placed on the client, or server side, so I'm at a loss as to where to start with this. Any script or config suggestions would be welcome.
Edit to add:
The data is preformatted, not created by the user. It consists of 7-10 fields of data that will likely be consistent with that seen by other users. The purpose is to build a troubleshooting database for users to reference or add to. The central server will be based on Q2A to allow upvotes, comments, etc.
This seems like the opposite of what Freebase does. In Freebase, users can connect to the Freebase API and check to see if something exists in the Freebase API if it does not already exist in their database. It is then up to them to cache the entry for faster retrieval in the future. Alternatively, at least in the past, the Freebase community enabled writing to the Freebase database using the MQL API.
If you are suggesting would strike me as being very involved. If you have content creators you really trust, you maybe can get away with not having any review process, but otherwise you will need some peer review and perhaps some programming. Unless you have no content standards, and it is anything goes, your database could quickly become overloaded with nonsense or things you don't want your website to be associated with (whatever those somethings might be).
Without knowing more about your database, I can't really say what those things would be, but what I will say is that if you are looking to have people throw stuff into a centralized location, you may want to (a) use something like OAuth, (b) set up some balances, because while one of your clients may think it's very important to have the 100 reasons why liberals/conservatives suck", another one of your clients may take offense. Guess who they will blame?
That being said, creating a RESTful API (don't know if you already have one or not) with a flag for insert_if_not_exists could work.
i.e. api.php?{json_string} would be picked up by a function/functions which determined what the user wanted to do in the json_string.
On the backend, your PHP function could parse it to an array very easily and if the insert_if_not_exists flag is triggered, you can create the post while you pull the data. Otherwise you just pull the data (or leave that part out if you only want to give them the option to post and not to pull in this fashion).
I am developing an iOS application from which users can post various kinds of events in a server and view the events created from all the users. The question i have , does not have a programming nature. I would like to show here , how i would design my servers database and tell me your opinion about it and how i could improve it.
The application has a very simple interface where the user when he wants to post an event , simply writes a title , some comments and attaches a photo if he wants . Then this information is sent through XML to my server and stored to the database.
The problem is that because some people are immature , they would try to post inappropriate words or even photos. So i would like to have some control on my users. What i am thinking is , at the first time that the application runs on the mobile phone and connects to the server , the server would send a user id back to the phone. Then every time the users sends an xml file , he attaches his user id with the xml (programatically attached). Also i would keep another database with all the user ids that have been created over time. So if i notice an inappropriate event , i could delete the user id from my database , and the next time the user tries to send something, the server would understand that this id is not in the database and so not allow the posting. Of course if someone decides to uninstall and reinstall the application and get a new user id , he could again post but thats ok with me.
Would it be an easier way to prevent immature behaviors or this one sounds ok?
Sure what you describe is possible. But there is one big problem with it: that interface can easily be used by a robot (a script). So if someone really wants to missuse your service, he can flood you with whatever he likes in a second. Or he can try again and again, until you have to give up removing his posts from the service.
I suggest you take a look at one of the existing frameworks instead. This way you do not have to reinvent the wheel (which has already been done 19562394792 times, and counting) and don't have to learn from your mistakes (which you certainly will make).
This is about the only workable solution with images since you can't easily perform any recognition on an image to decide if it is appropriate. The comments they post would be more easily scanned for inappropriate items.
If you are will to do this kind of moderation then your solution sounds like the right one. As with any open system you can't really stop people from creating new accounts as you mentioned. You could log and ban IP addresses, but that is not a very good solution anymore as most IP addresses could really be shared gateway addresses and those addresses rotate frequently between users.
Create an ID. Watch for behavior. Ban the ID. And encourage community involvment in alerting you of bad posts with some kind of a Flag button.
There are many ways to design a database and not just for use with an iOS app. However when I'm building a mobile app (iOS, Android or any other) I want to make sure that the amount of data being sent and received is as small as possible; this is why I use JSON instead of XML... smaller footprint.
Because I use JSON I like to use an object database like MongoDB (my favorite) or CouchDB, because I 1) don't need to worry about the structure of my data and 2) the database stores the objects in JSON format.
I then use Node.JS for my application server so now I have JSON database -> JSON objects in my server application code -> outputting JSON... seamless with no Mappers or serialisation required. FTW.
I have a certain concept in mind, but I an unsure what my possibilities are.
User enters a name (html form) on a webpage
Webserver sends push notification to the Android device of the user
Application looks for this name in the contactslist and finds the corresponding phone number
Application instantly sends this information back to the webserver, where the user can use the returned phone number.
The only step I am unsure about is #4. I presume I'd have to use a temporary MYSQL database and use reverse AJAX or something alike to get the returned data to the user without him having to refresh the page (this entire process needs to be completed within a couple of seconds at most - the faster, the better).
This all sounds like a very hack-ish approach to me though, I was hoping any of you could hint me with better ways of tackling this - or maybe a good resource to the approach I described.
It's hard to find solutions when you're not sure what it is you're looking for.
Much obliged!
I can think of two ways of solving #4
poll "did I get the response yet?, did I get the response yet?..."
or use websockets. here's a post I found that might be useful: post
Here's one interesting approach to it all using node.js: nowjs
If you plan to use Androids C2DM, check out the min-max delivery times.