Connecting the app to a server
Up to this point we have created the bones of a chat app. But we need to add two important features that are missing.
- We need to retain messages when the app is closed
- We need to be able to chat with another person!!!
- A chat app is pretty useless when you can't talk to anyone else.
In order to let users talk to other people, we need to connect our app to a server.
Servers
This tutorial will focus on how to talk to the server, but the code for the server itself is detailed in the "Hello, Server!" book that can be found here.
Communicating with a server
Communication with a server is done via an API, in our case we will be specifically using a RESTful (or simply REST) API with JSON.
Breaking down that statement, REST is simply a way for organizing an API, and API is a way to communicate with the server, and JSON is a format of data which we will see more of soon.
All you need to know, is that the API we are using uses HTTP, the same protocol that is used to communicate with the websites you use everyday.
To see what I mean, navigate to http://hello-world-server420.cloudapp.net/messages.
(Note for future readers, the code for the server hosted at that URL is available in the Hello, Server tutorial linked above. The live version of the server will be taken down after Hello, World.)
In your browser you will see some text similar to the following
[{"_id":"57e453a469dfe113a3bc0154","message":"Hello world!","room":"B146","user":"David","__v":0},{"_id":"57e453a569dfe113a3bc0155","message":"Hello world!","room":"B146","user":"David","__v":0}]
This may look like gibberish now, but after adding the proper indentation and formatting (which can be done automatically by installing this Chrome extension).
[
{
"_id":"57e453a469dfe113a3bc0154",
"message":"Hello world!",
"room":"B146",
"user":"David",
"__v":0
},
{
"_id":"57e453a569dfe113a3bc0155",
"message":"Hello world!",
"room":"B146",
"user":"David",
"__v":0
}
]
This is called JSON, which stands for Javascript Object Notation. It is a common way of formatting data for APIs, and the chat API uses this format.
Dissecting the parts of this JSON shows that there are two messages, each with a message
, room
, and user
attribute. We will be using those attributes later to load these messages into the app.
Sending chat messages using the API
So far we have seen that the API lets us list messages, but it also allows us to SEND messages. To show how this is done we will be using a tool called Postman. It can be downloaded here, but is not completely necessary for completing the next portion of the app.
When we browse to a website using HTTP, our browser is downloading the necessary HTML and CSS by sending what is called a "GET Request" to the server. This is one of many methods that are available as part of HTTP. The other we will be using is called POST. It is used to send data back to the server.
Our browser is very good at executing GET requests, but Postman is necessary for sending the POST requests that we need.
In Postman we will run a POST request by first inserting the correct URL, which in our case is http://hello-world-server420.cloudapp.net/messages and then set the method to POST.
Then we need to set the body of the request. We do this by selecting the body tab, selecting the raw
radio button, changing the type from "Text" to "JSON (application/json)".
Insert the following into the text field:
{
"message": "Hello, this is a test message.",
"room": "Hello, World room.",
"user": "David"
}
Your window should look like the following.
This will send a message to the server with the message: "Hello, this is a test message.", and the above room and user.
After clicking send and reloading the webpage, you should see the new message listed in the window.
(Note: Because we expect many people to be hitting this server, we may be periodically clearing all messages from the database. Because of this, it is possible that your message will be deleted before you see it. If this happens you should send the message again.)
Review
We now know how to interact with the API, so all we need now is to do these operations from Android.