USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.

Slides:



Advertisements
Similar presentations
USING ANDROID WITH THE INTERNET. Slide 2 Network Prerequisites The following must be included so as to allow the device to connect to the network The.
Advertisements

1 Java Networking – Part I CS , Spring 2008/9.
Getting a Web Page (And what to do once you’ve got it)
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.
Building Apps with Connectivity & the Cloud. Connecting Devices Wirelessly Performing Network Operations Transferring Data Without Draining the Battery.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
AJAX Chat Analysis and Design Rui Zhao CS SPG UCCS.
CS378 - Mobile Computing Web - WebView and Web Services.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Networking Android Club Networking AsyncTask HttpUrlConnection JSON Parser.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
HTTP and Threads. Getting Data from the Web Believe it or not, Android apps are able to pull data from the web. Developers can download bitmaps and text.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Working in the Background Radan Ganchev Astea Solutions.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
MAKANI ANDROID APPLICATION Prepared by: Asma’ Hamayel Alaa Shaheen.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Android networking 1. Network programming with Android If your Android is connected to a WIFI, you can connect to servers using the usual Java API, like.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
SE-2840 Dr. Mark L. Hornick 1 Introduction to Ajax Asynchronous Javascript And XML.
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Android Programming Steve Ko Computer Sciences and Engineering University at Buffalo.
Adding network connections. Connecting to the Network To perform the network operations, your application manifest must include the following permissions:
David Sutton MOBILE INTERNET. OUTLINE  This week’s app – an RSS feed reader  Mobile internet considerations  Threads (recap)  RSS feeds  Using AsyncTask.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
Small talk with the UI thread
Echo Networking COMP
Asynchronous Task (AsyncTask) in Android
Reactive Android Development
JavaScript and Ajax (Internet Background)
Web Programming Developing Web Applications including Servlets, and Web Services using NetBeans 6.5 with GlassFish.V3.
Section 13 - Integrating with Third Party Tools
Chapter 3 Internet Applications and Network Programming
CS240: Advanced Programming Concepts
MCA – 405 Elective –I (A) Java Programming & Technology
AJAX and REST.
#01 Client/Server Computing
WEB API.
JavaScript Introduction
CSE 154 Lecture 22: AJAX.
Process-to-Process Delivery:
CS371m - Mobile Computing Responsiveness.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
CS323 Android Topics Network Basics for an Android App
Internet Applications and Network Programming
Web Socket Server (using node.js)
JavaScript & jQuery AJAX.
Android Topics Asynchronous Callsbacks
Android Developer Fundamentals V2
Lecture 5: Functions and Parameters
Android Developer Fundamentals V2
Threads, Handlers, and AsyncTasks
Chapter 42 Web Services.
Message Passing Systems Version 2
Android Threads Dimitar Ivanov Mobile Applications with Android
#01 Client/Server Computing
Message Passing Systems
Presentation transcript:

USING ANDROID WITH THE INTERNET

Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results Parsing HTTP results

Slide 3 Network Prerequisites (1) The element must be included in the AndroidManifest.xml resource so as to allow the application to connect to the network Permissions are used to ask the operating system to access any privileged resource

Slide 4 Network Prerequisites (2) The tag causes the application to request to use an Android resource that must be authorized The tag must be an immediate child of

Slide 5 Protocols (Introduction) Android supports several different network protocols. TCP / IP (through the Socket class) SMTP (through the GMailSender class) HTTP And others In this exercise, you will work with HTTP

Slide 6 Checking for Network Access The network might be unavailable for many reasons We may have WiFi service or just cell service Note that service might be metered so we need to limit bandwidth We will only get to the basics in this course related to bandwidth management and power consumption best practices

Slide 7 The ConnectivityManager The ConnectivityManager answers questions about the state of the network It can be used to notify applications when the connectivity status changes Fail over to other networks when connectivity is lost Allow applications to select and query networks

Slide 8 The ConnectivityManager The getSystemService( Context.CONNECTIVITY_SERVICE) method gets information about the service stored properties of the ConnectivityManager

Slide 9 The NetworkInfo Class (1) The ConnectivityMannager.getActiveNetworkInfo() method returns an NetworkInfo instance Call the isConnected method to determine whether there is a network connection See kInfo.html kInfo.html

Slide 10 The NetworkInfo Class (2) Example If there is no default network, NetworkInfo is null Always call isConnected to determine whether there is a viable connection

Slide 11 Introduction to Multithreading A device must: Respond to user requests (your interaction) Listen for and respond to various notifications (from other applications) The Android OS services application requests based on processes and threads In a nutshell, the OS services applications with work to do, based on a queue

Slide 12 Introduction to Multithreading Service requests can take a while to complete Some requests might not complete at all A thread is a concurrent unit of execution There is a thread for the UI (foreground) We can create other threads (background) There are other threads already running (background and other applications) This gets complicated so I’ll provide just enough here for you to create a thread and run it

Slide 13 The AsyncTask Class (Introduction) The android.os.AsyncTask allows background operations to be performed on a background thread and data returned to the UI thread It is a helper class that wraps the Thread class It hides the details of managing a Thread Use it to perform short-lived background tasks

Slide 14 The AsyncTask Class (Creating) To use the AsyncTask create a subclass with three generic parameters The first contains the data passed to the thread (params…) The second, if used, contains an integer used to report the thread’s progress (progress…) The third, if used, contains the data type of the thread’s return value (result) If a parameter is void, then it will not be used

Slide 15 The AsyncTask Class (Example) Extend AsyncTask A String is passed to the task There is no progress reporting (the 2 nd argument is Void ) A String is returned from the task When the background task is executed, it goes through four steps

Slide 16 The AsyncTask Class (Implementing – Introduction) We implement methods that execute Before the thread starts ( onPreExecute ) To start the thread and pass data to the thread ( doInBackground ) To report thread progress ( onProgressUpdate ) To return data from the thread ( onPostExecute ) These methods are required by the abstract class (interface)

Slide 17 The AsyncTask Class (Implementing – Step 1) onPreExecute is invoked on the UI thread before the task begins It is here that you setup the task Initialize a progress meter, for example Implementing this step is optional

Slide 18 The AsyncTask Class (Implementing – Step 2) doInBackground is the worker It executes immediately after onPreExecute finishes It is here that you perform the background computation Or in our case network access The parameters are passed to this procedure The results are returned in this step Call publishProgress(Progress…) to publish progress results (optional)

Slide 19 The AsyncTask Class (Implementing – Step 2) String … denotes a variable length array of type String Se could have used String[] urls So urls contains the parameters

Slide 20 The AsyncTask Class (Implementing – Step 3) onProgressUpdate is invoked on the UI thread It gets called as a result of the publishProgress call Use it to update a progress meter or log Implementing this method is optional It’s really only useful when we can estimate progress What if I don’t know how big a page is when I request it

Slide 21 The AsyncTask Class (Implementing – Step 4) onPostExecute is invoked on the UI thread The parameter contains the result of the asynchronous method call

Slide 22 STATUS You now know how to set up the asynchronous infrastructure Net we will see how to send a HTTP request with a URL and process the result

Slide 23 HTTP (Introduction) In our case the transfer protocol is HTTP We connect the client device to a server and get data We then process that data somehow We might render a Web page We might parse and process XML Or any other message

Slide 24 HTTP (Introduction) Two HTTP clients HttpClient HttpURLConnection Both support HTTPS and IPV6 Use HttpURLConnection for post Lollypop devices

Slide 25 Android URL and HTTP Related Classes Note that there are both Java and Android URL classes There are also URI classes

Slide 26 The URL Class The java.net.URL class represents a url Convert strings to URLs Convert URLs to strings net/URL.html net/URL.html

Slide 27 The URL Class

Slide 28 Opening a Connection (1) The URL.openConnectcion() method establishes a connection to a resource Over this connection, you make the request and get the response We will use HTTP here but other protocols are supported

Slide 29 Opening a Connection (2) The setReadTimeout() mutator defines the time to wait for data The setConnectTimeout() mutator the time to wait before establishing a connection The setRequestMethod() defines whether the request will be a GET or a POST The setDoInput() mutator, if true allows receiving of data

Slide 30 Opening a Connection (3) Calling the connect() method opens the connection getResponseCode() gets the HTTP response code from the server -1 if there is no response code. Such as 404 not found? getInputStream() gets the input stream from which you can read data Works just like an open file

Slide 31 Opening A Connection (Example)

Slide 32 Reading the Input

Slide 33 Processing XML Create a DocumentBuilder via the DocumentBuilderFactory Call the parse method on an input stream to read and parse the document Using the Document object, navigate and work with the in-memory XML document Its really the same object as the JavaScript document object

Slide 34 Classes We Need DocumentBuilderFactory DocumentBuilder Document NodeList Node InputSource StringReader

Slide 35 The DocumentBuilderFactory Class It lets applications get a parser that produces XML object trees from XML documents This is pretty much the same DOM with which you are familiar (JavaScript) As the name implies, we use a factory class and newInstance method to create the DocumenntBuilder

Slide 36 The DocumentBuilderFactory Class Example to create the factory DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

Slide 37 The DocumentBuilder Class It’s the DocumentBuilder that has the APIs used to parse the XML into a Document

Slide 38 The Document Class

Slide 39 The NodeList Class

Slide 40 The Node Class