Difference between revisions of "MAP524/DPS924 Lecture 7"

From CDOT Wiki
Jump to: navigation, search
(Created page with 'There are many reasons to connect to a server from your application - most often you'll want to retrieve content from the web to display and manipulate in your app. You'll need …')
 
(Checking connectivity)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
= Using HTTP =
 +
 
There are many reasons to connect to a server from your application - most often you'll want to retrieve content from the web to display and manipulate in your app.
 
There are many reasons to connect to a server from your application - most often you'll want to retrieve content from the web to display and manipulate in your app.
 +
 +
Any network operation can potentially take a very long time, so you'll have to do all your network stuff in a separate thread or AsyncTask.
 +
 +
As a demo for this week's lecture we're going to use the service provided by [https://www.ipify.org/ ipify] to obtain our IP address.
 +
 +
== Connecting ==
  
 
You'll need to use an [http://developer.android.com/intl/ja/reference/java/net/HttpURLConnection.html HttpURLConnection].
 
You'll need to use an [http://developer.android.com/intl/ja/reference/java/net/HttpURLConnection.html HttpURLConnection].
  
Any network operation can potentially take a very long time, so you'll have to do all your network stuff in a separate thread or AsyncTask.
+
It's possible to use both GET and POST to send parameters to the server, GET is easier as usual.
 +
 
 +
== Checking connectivity ==
  
 
Your app will get fewer bad reviews if it displays a good error message when a network is not available. See [http://developer.android.com/intl/ja/training/basics/network-ops/connecting.html#connection here] for a simple example of how to check the status of your connectivity.
 
Your app will get fewer bad reviews if it displays a good error message when a network is not available. See [http://developer.android.com/intl/ja/training/basics/network-ops/connecting.html#connection here] for a simple example of how to check the status of your connectivity.
Line 16: Line 26:
 
     // do something
 
     // do something
 
}</source>
 
}</source>
 +
 +
== Secure communication ==
 +
 +
You should never transfer any confidential information over an unencrypted channel. Using an HttpsURLConnection is pretty easy, unless you have a self-signed certificate on your server. You don't need to know the details of this implementetion but if you're curious: [http://littlesvr.ca/grumble/2014/07/21/android-programming-connect-to-an-https-server-with-self-signed-certificate/ here it is].
 +
 +
= Parsing XML =
 +
 +
More and more services are offering their data in Json format, but XML is still a very popular choice because there's been so much software written with it for almost 20 years. We'll only look at reading XML because creating it is unlikely to be necessary in an Android app.
 +
 +
The [http://www.vogella.com/tutorials/AndroidXML/article.html Vogella tutorial] for XmlPullParser is much easier to read than the [http://developer.android.com/training/basics/network-ops/xml.html official documentation].
 +
 +
= Reading and Writing JSON =
 +
 +
Other than dealing with exceptions using JSON in Android is trivial. If you're already familiar with JSON, all you need is [http://developer.android.com/reference/org/json/JSONObject.html here in the developer documentation].
 +
 +
If you haven't use JSON before: try to figure out the diagrams on the [http://www.json.org/ official site] and use [http://jsonlint.com/ the validator] to test your understanding.

Latest revision as of 07:54, 20 October 2015

Using HTTP

There are many reasons to connect to a server from your application - most often you'll want to retrieve content from the web to display and manipulate in your app.

Any network operation can potentially take a very long time, so you'll have to do all your network stuff in a separate thread or AsyncTask.

As a demo for this week's lecture we're going to use the service provided by ipify to obtain our IP address.

Connecting

You'll need to use an HttpURLConnection.

It's possible to use both GET and POST to send parameters to the server, GET is easier as usual.

Checking connectivity

Your app will get fewer bad reviews if it displays a good error message when a network is not available. See here for a simple example of how to check the status of your connectivity.

If your app does large downloads (megabytes) - you may want to only do those downloads when connected to wifi:

NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected())
{
    // do something
}

Secure communication

You should never transfer any confidential information over an unencrypted channel. Using an HttpsURLConnection is pretty easy, unless you have a self-signed certificate on your server. You don't need to know the details of this implementetion but if you're curious: here it is.

Parsing XML

More and more services are offering their data in Json format, but XML is still a very popular choice because there's been so much software written with it for almost 20 years. We'll only look at reading XML because creating it is unlikely to be necessary in an Android app.

The Vogella tutorial for XmlPullParser is much easier to read than the official documentation.

Reading and Writing JSON

Other than dealing with exceptions using JSON in Android is trivial. If you're already familiar with JSON, all you need is here in the developer documentation.

If you haven't use JSON before: try to figure out the diagrams on the official site and use the validator to test your understanding.