Messaging and Networking Melissa Rodrigues Topics Covered Networking

  • Slides: 13
Download presentation
Messaging and Networking Melissa Rodrigues

Messaging and Networking Melissa Rodrigues

Topics Covered � Networking � Downloading Binary Data � Downloading Text Files � Accessing

Topics Covered � Networking � Downloading Binary Data � Downloading Text Files � Accessing Web Services � Performing Asynchronous Calls

Networking in Android Networking HTTP Web Services Socket Programming

Networking in Android Networking HTTP Web Services Socket Programming

1. Using HTTP Using the HTTP protocol, tasks such as • Downloading web pages

1. Using HTTP Using the HTTP protocol, tasks such as • Downloading web pages from a web server • Downloading binary data For establishing an HTTP connection, use the Http. URLConnection class.

1. Downloading Binary Data The Download. Image() method takes the URL of the image

1. Downloading Binary Data The Download. Image() method takes the URL of the image to download and then opens the connection to the server using the Open. Http. Connection() method Because the Download. Image() method is synchronous i. e. , it will not return control until the image is downloaded ; calling it directly will freeze the UI of your activity.

1. Downloading Binary Data This is not allowed in Android 3. 0 and later;

1. Downloading Binary Data This is not allowed in Android 3. 0 and later; all synchronous code must be wrapped using an Async. Task class. Using Async. Task enables you to perform background tasks in a separate thread and then return the result in a UI thread. That way, you can perform background operations without needing to handle complex threading issues. The three methods in Async. Task class are do. In. Background(), on. Progress. Update(), and on. Post. Execute().

1. Downloading Text Files The Download. Text() method takes the URL of the text

1. Downloading Text Files The Download. Text() method takes the URL of the text file to download and then returns the string of the text file downloaded. It basically opens an HTTP connection to the server and then uses an Input. Stream. Reader object to read each character from the stream and save it in a String object.

2. Accessing Web Services A web service is a collection of open protocols and

2. Accessing Web Services A web service is a collection of open protocols and standards used for exchanging data between applications or systems. It is a software function provided at a network address over the Web with the service always on.

2. Accessing Web Services � XML (Extensible Markup Language)Web Services � JSON (Java. Script

2. Accessing Web Services � XML (Extensible Markup Language)Web Services � JSON (Java. Script Object Notation)Web Services

2. Accessing Web Services Use the Document, Document. Builder. Factory, and Document. Builder classes

2. Accessing Web Services Use the Document, Document. Builder. Factory, and Document. Builder classes to parse the XML result returned by the web service. Once the web service returns a result in XML, you extract the relevant parts and display its content using the Toast class using the HTTP GET method.

XML documents is a computationally expensive operation for mobile devices XML documents are lengthy.

XML documents is a computationally expensive operation for mobile devices XML documents are lengthy. They use tags to embed information, and the size of an XML document can get very big pretty quickly. A large XML document means that your device has to use more bandwidth to download it, which translates into higher cost. XML documents are more difficult to process. One needs DOM to traverse the tree in order to locate the information you want. In addition, DOM itself has to build the entire document in memory as a tree structure before you can traverse it. This is both memory and CPU intensive.

3. Socket Programming Problem with HTTP is the web server does not maintain a

3. Socket Programming Problem with HTTP is the web server does not maintain a persistent connection with the clients. To solve this problem, we use the next method called as Socket Programming

3. Socket Programming If you want your application to maintain a persistent connection to

3. Socket Programming If you want your application to maintain a persistent connection to the server and be notified by the server whenever changes occur, you need to use a programming technique known as sockets programming. Sockets programming is a technique through which you use to establish a connection between a client and a server. For socket programming, use the Socket class to establish a TCP connection, use the Input. Stream and Output. Stream objects for receiving and sending data, respectively.