Due: Tuesday April 28, end of day.
Submission instructions: Submit your assignment here.
In this assignment, we are going to build a local messaging app. The idea is that people in the same place should be able to exchange messages.
There is a server.
The methods it supports are described by example, as follows:
put_local
You pass the parameters:
Sample call is as follows. Of course, you would do this using a POST and not a GET, but this gives you an idea of the parameters:
https://luca-teaching.appspot.com/store/default/put_local?lat=120.1111&lng=33.333333&msg=tomato&msgid=aba7dc32
Sample result (this is Json):
{"messages": [{"msg": "tomato", "msgid": "aba7dc32", "ts": "2015-04-22T05:45:12.580370"}, {"msg": "tomato", "msgid": "aba7dc32", "ts": "2015-04-22T05:27:21.955330"}, {"msg": "tomato", "msgid": null, "ts": "2015-04-22T05:22:52.978460"}, {"msg": "tomato", "msgid": null, "ts": "2015-04-22T05:22:45.292740"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T05:22:30.987420"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T04:07:22.684170"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T04:06:58.485890"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T04:02:18.937910"}]}
get_local
Reads the messages. Sample call:
https://luca-teaching.appspot.com/store/default/get_local?lat=120.1111&lng=33.333333
Sample Json result:
{"messages": [{"msg": "tomato", "msgid": "aba7dc32", "ts": "2015-04-22T05:45:12.580370"}, {"msg": "tomato", "msgid": "aba7dc32", "ts": "2015-04-22T05:27:21.955330"}, {"msg": "tomato", "msgid": null, "ts": "2015-04-22T05:22:52.978460"}, {"msg": "tomato", "msgid": null, "ts": "2015-04-22T05:22:45.292740"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T05:22:30.987420"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T04:07:22.684170"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T04:06:58.485890"}, {"msg": "potato", "msgid": null, "ts": "2015-04-22T04:02:18.937910"}]}
You can examine the server code at https://bitbucket.org/luca_de_alfaro/backingstore , in fact, it is advised for you to do so. Look here in particular. The code is written the way it is for a reason; you would do well to study it. A fully scalable geographical message server that can serve thousands (at least) of rps (requests per second) in 40 lines of code.
The screen must contain the following.
List View: On top there must be a listview to display the recent messages you receive from the server. For each message, you should have a list item that displays the message text, and the timestamp. If you wish you can work on the timestamp and display time information as "1 minute ago", "5 minutes ago", "2 hours ago", ..., etc, but this is not required.
EditText: Here is where you can enter messages to post.
Post button: A button to post the message you enter in the EditView.
Refresh button: A button to re-fetch the recent messages.
Progress bar: A spinner (an indefinite progress bar) that must be displayed while the server calls are in progress.
TextView: A small TextView at the bottom of the screen should display the latitude and longitude updated in real time. This is useful mainly for debugging and testing.
You must have running a location listener that continually updates the location, as we did in this example.
When you post a message, attach a random id to it, and set the lat and lng according to latitude and longitude.
The server will reply with the 10 most recent messages close to you.
The msgid is used so that, if the message you just posted is by any chance not part of the server response, you can detect it because you see that its message id is missing from the response, and you can add it back on the client (on the phone), so that the user can see it among the messages. Display the messages you get from the server in order of most recent first.
The refresh button calls get_local and displays the messages that are returned.
Please log via Log.i
all the interaction with the server (your server calls, and the return values).
Options:
You can test your application by looking at the server content. Note: everybody is using the same backend (this is the point of the homework, local communications!), so you will see you messages mixed with those by others. You can search, sort by timestamp or other fields, etc.
To view the messages, simply go to http://luca-teaching.appspot.com/store/default/local_index .
I strongly advise you to use this app as starting point: https://bitbucket.org/luca_de_alfaro/cmps121-bboard-android
Given the format of the Json that is returned from the server calls, you will have to build two classes to decode it. One class, say, MsgInfo
, will decode the following Json:
{"msg": "tomato", "msgid": "aba7dc32", "ts": "2015-04-22T05:45:12.580370"}
The outer class will decode the entire message, and will contain a single field:
public MsgInfo[] messages;
Please refer to the Gson documentation for more information.
Note also that the bboard app stores the messages it receives from the server in the Preferences. You need to delete those if you fail to parse them, as they might be in an old format.