Difference between revisions of "MAP524/DPS924 Lecture 7"
(→Parsing XML) |
|||
Line 26: | Line 26: | ||
= Parsing XML = | = 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 = | = Reading and Writing JSON = |
Revision as of 13:20, 19 July 2015
Contents
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.
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
}
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.