18
Mobile Programming Sharif University of Technology Spring 2016 - Lecture 12 Lecturer: Omid Jafarinezhad

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

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

“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

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

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

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

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

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

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

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

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

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

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

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

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

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

Scheduling tasks via the AlarmManager