Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016...
Transcript of Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016...
![Page 1: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/1.jpg)
Mobile
Programming
Sharif University of TechnologySpring 2016 - Lecture 12
Lecturer: Omid Jafarinezhad
![Page 2: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/2.jpg)
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
![Page 3: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/3.jpg)
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
![Page 4: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/4.jpg)
“http://example.com”
![Page 5: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/5.jpg)
Check the network availability
![Page 6: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/6.jpg)
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
![Page 7: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/7.jpg)
Synchronous Get
![Page 8: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/8.jpg)
Asynchronous Get
![Page 9: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/9.jpg)
Accessing Headers
![Page 10: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/10.jpg)
Posting a String
![Page 11: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/11.jpg)
Posting a File
![Page 12: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/12.jpg)
Posting form parameters
![Page 13: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/13.jpg)
Timeouts
![Page 14: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/14.jpg)
OkHttp recipesto solve common problems with OkHttp you can read the following link :
● https://github.com/square/okhttp/wiki/Recipes
![Page 15: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/15.jpg)
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
![Page 16: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/16.jpg)
Setting up Notifications
notificationID: If you reuse the ID it will not add a new one, instead the old will be updated
![Page 17: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/17.jpg)
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
![Page 18: Programming Mobile Lecturer: Omid Jafarinezhad Spring 2016 ...ce.sharif.edu/courses/94-95/2/ce327-1/resources/root/Lecture Notes... · Overview of HTTP access on Android ... improving](https://reader035.fdocuments.net/reader035/viewer/2022071212/6025df25e7dea400df11d82b/html5/thumbnails/18.jpg)
Scheduling tasks via the AlarmManager