Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Mobile
Programming
Sharif University of TechnologySpring 2016 - Lecture 12
Lecturer: Omid Jafarinezhad
Overview of HTTP access on Android● Android contains the standard Java network java.net package which
can be used to access network resources. The base class for HTTP network access in the java.net package is the HttpURLConnection class
● Android also contains the Apache HttpClient library but it is not recommended anymore to use it, as Google is focusing their efforts on improving the HttpURLConnection implementation
● Performing network operations: You have to open and close a connections, enable caches and ensure to perform the network operation in a background thread.To simplify these operations several popular Open Source libraries are available. The most popular ones are the following: Volley, OkHttp
Java and HTTP accessJava provides a general-purpose, lightweight HTTP client API to access resources via the HTTP or HTTPS protocol
The main classes to access the Internet are the java.net.URL class and the java.net.HttpURLConnection class
“http://example.com”
Check the network availability
OkHTTPOkHTTP is an Open Source project designed to be an efficient HTTP client. It supports the SPDY protocol. SPDY is the basis for HTTP 2.0 and allows multiple HTTP requests to be multiplexed over one socket connection
OkHttp is an HTTP client that’s efficient by default:
● HTTP/2 support allows all requests to the same host to share a socket● Connection pooling reduces request latency (if HTTP/2 isn’t available).● Transparent GZIP shrinks download sizes.● Response caching avoids the network completely for repeat requests
Synchronous Get
Asynchronous Get
Accessing Headers
Posting a String
Posting a File
Posting form parameters
Timeouts
OkHttp recipesto solve common problems with OkHttp you can read the following link :
● https://github.com/square/okhttp/wiki/Recipes
Android NotificationsAndroid allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity
Setting up Notifications
notificationID: If you reuse the ID it will not add a new one, instead the old will be updated
AlarmManagerAlarmManager provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted
Scheduling tasks via the AlarmManager