Write an Android terminal to chat with a server.
-
How this app works.
- A server is set up on AWS at
http://54.161.23.101
. We can visit it via either curl or browser.
Test: Open a console; type incurl http://54.161.23.101/basicRequest
to see the server's response. - The app sends out a message, and the server responds.
- More detailedly, when you click on "send", the app does the following:
(1) Updates the message display area.
(2) Sends out a request containing the message, and the server responds with a random sentence.
(3) After receiving the response, update the message display area again.
(4) How to update the message display area? For this tutorial since the pattern is "one ask, one reply" we can just utilize OkHttp's pattern.
- A server is set up on AWS at
-
Start an Android Studio Project
- An Android Studio project contains: AndroidManifest.XML, layout and value XML files, Java Classes, and other material
- Different functionalities of the project folders
-
Basic Contents on Android side
- Set up layout xml file.
- Talk about RelativeLayout, LinearLayout, and ListView. ListView shall not be placed within ScrollView
- XML attributes
- Resources: color, string, dimension, style, drawables. Where to put them, and how to link to them using
@
- Wire up EditText widget and the Button widget.
- addTextChangedListener: save the current text to variable;
- onClickListener: Write to file and update ListView.
- Activity lifecycle: how the activity components work together and where to put each functionality. Specifically, during start, configuration changes (rotation of screen, etc), and unexpected stops, which methods are called?
Preserve messages in onPause() and recover them in onResume() - Set up ListView and adapter.
- XML add ListView
- XML add row view
- MainActivity setAdapter() in onCreate().
- Java define own adapter class, implement its getView() method
- Update ListAdapter data every time the data needs to be (using
ListAdapter.notifyDataSetChanged()
). The most important thing is to keep the same reference to the Adapter (otherwise data would be lost). - Wrap them up in MainActivity in separate functions to ensure modularity.
Up till now you can see the demo:
- Set up layout xml file.
-
(Optional) Further steps:
- Shoot up a request to server.
- We ar eusing OkHttp. It is the standard way of internetworking since Android 5.0. Other packages include
HttpURLRequest
, etc. See this link for more detail. - OkHttp: in
build.gradle
import it to Android project: place OkHttp and Okio inapp/libs/
then File-project structure-dependencies-Add file dependency-select - OkHttpClient and Request. Remember to
runOnUiThread()
when you want to update the UI elements. - Add Internet Permission in
AndroidManifest.xml
Now the functionality is done.
- We ar eusing OkHttp. It is the standard way of internetworking since Android 5.0. Other packages include
- Customizing View.
- What if the network is slow? What if the Activity is unexpectedly closed when the other Thread is running?
- Further steps after the tutorial:
- If there is no response from Internet, set a timeout mechanism in OkHttp
- Server side: use more intelligent chatting algorithms.
- Shoot up a request to server.