Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active August 29, 2015 14:27
Show Gist options
  • Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
HKUSTx: COMP107x Introduction to Mobile Application Development using Android
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Android HTTP\n",
"---\n",
"\n",
"## Objectives\n",
"\n",
"In this exercise you will learn how to communicate with a server using the HTTP protocol and how to download the JSON string from the server. At the end of this exercise you will be able to:\n",
"\n",
"* Use the HTTPURLConnection class to establish a connection between your app and a server\n",
"* Send HTTP requests to the server and receive replies in the form of JSON strings from the server\n",
"* Process the received reply to extract information\n",
"* Set up the permissions in the Android Manifest file\n",
"\n",
"\n",
"## Introduction\n",
"\n",
"This project deals with the communication process between your app and a server. We will use the HTTP protocol for communication. In addition, we will use the HTTPURLConnection class of Android to establish connection to a server, send HTTP requests to the server and download the data from the server in the form of JSON strings. The received data is extracted from the body of the HTTP reply message from the server and processed to render the UI's ListView for the ChatClient application.\n",
"\n",
"## Textual Instructions\n",
"\n",
"---\n",
"### Step 1: Examining the Project Files\n",
"\n",
"1. Download the [ChatClientNetwork.zip](http://w02.hkvu.hk/edX/COMP107x/w5/ChatClientNetwork.zip) file, unzip it and import the project into Android Studio.\n",
"2. Open the **Contacts.java** file.\n",
"3. Open the **AndroidManifest.xml** file.\n",
"4. Follow the video instructions.\n",
"\n",
"---\n",
"### Step 2: Adding User Permissions to the Android Manifest file\n",
"\n",
"Open the AndroidManifest.xml file in the manifests folder. Since we are accessing a server using the Internet connectivity, we need to request for permission in the manifest file. In addition, since we will be examining the network state, we need to ask for permission to access the network state. This is accomplished by including the following permission requests in the manifest file:\n",
"\n",
" <uses-permission android:name=\"android.permission.INTERNET\"/>\n",
"\n",
" <uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" />\n",
"\n",
"---\n",
"### Step 3: Updating the Contacts Activity\n",
"\n",
"1. Replace the *// TODO* comment in the **Contacts.java** file in the AsyncTask class' doInBackground() method with the following code:\n",
" HttpURLConnection urlConnection = null;\n",
" Integer result = 0;\n",
" try {\n",
" /* forming th java.net.URL object */\n",
" URL url = new URL(urls[0]);\n",
" urlConnection = (HttpURLConnection) url.openConnection();\n",
"\n",
" /* optional request header */\n",
" urlConnection.setRequestProperty(\"Content-Type\", \"application/json\");\n",
"\n",
" /* optional request header */\n",
" urlConnection.setRequestProperty(\"Accept\", \"application/json\");\n",
"\n",
" /* for Get request */\n",
" urlConnection.setRequestMethod(\"GET\");\n",
" int statusCode = urlConnection.getResponseCode();\n",
"\n",
" /* 200 represents HTTP OK */\n",
" if (statusCode == 200) {\n",
" inputStream = new BufferedInputStream(urlConnection.getInputStream());\n",
"\n",
" // Convert the read in information to a Json string\n",
" String response = convertInputStreamToString(inputStream);\n",
"\n",
" // now process the string using the method that we implemented in the previous exercise\n",
" processFriendInfo(response);\n",
" result = 1; // Successful\n",
" } else {\n",
" result = 0; //\"Failed to fetch data!\";\n",
" }\n",
"\n",
"2. Run the application to see the result."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment