Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),

Similar presentations


Presentation on theme: "Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),"— Presentation transcript:

1 Networking Nasrullah

2 Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()), or from an in-memory byte array (ByteArrayInputStream).FileInputStreamgetInputStream() ByteArrayInputStream Use InputStreamReader to adapt a byte stream like this one into a character stream.InputStreamReader Most clients should wrap their input stream with BufferedInputStream. Callers that do only bulk reads may omit buffering. BufferedInputStream

3 InputStream::read reads a single byte and returns it as an int InputStreamReader::read reads a single char (respecting the encoding) and returns this as an int If you want to read binary data use InputStream If you want to read strings from a binary stream, use InputStreamReader. One of its constructors allows you to specify a character set.

4 Whole story of Streaming in java An inputStreamReader is a bridge from bytesStream to character stream ; it reads bytes and decodes them into characters.it usually is wrapped inside a BufferReader.

5 Web services We have to work on two types of web services A.REST B.SOAP The web services can response in one of the following way XML SOAP JSON

6 Parsing xml Android offers three types of XML parsers DOM Parser Pull Parser SAX Parser

7 Parsing with DOM parser

8 Web services http://services.aonaware.com/DictService/Dic tService.asmx?op=Define

9 What is mean by Parsing Parsing usually applies to text - the act of reading text and converting it into a more useful in- memory format, "understanding" what it means to some extent. So for example, an XML parser will take the sequence of characters (or bytes) and convert them into elements, attributes etc. In some cases (particularly compilers) there's a separation between lexical analysis and syntactic analysis, so the real "understanding" part of the parser works on a sequence of tokens (identifiers, operators etc) rather than on the raw characters.lexical analysis

10 Document Class The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data. Since elements, text nodes, comments, processing instructions, etc. cannot exist outside the context of a Document, the Document interface also contains the factory methods needed to create these objects. The Node objects created have a ownerDocument attribute which associates them with the Document within whose context they were created.

11 DocumentBuilderFactory class Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents

12 Processes and Thread

13 Threads If you are familiar with the “Thread” term, it's easy to understand. Android applications have a main thread where all is processed. It's like having “OneTask” that does everything. If we had only this UI Thread to work with, you would not be able to do some things like, storing 10,000 data rows in the application at the same time you are using it. The application would stick, until the process “store 10,000 data rows” was finished. But, this is not the case! In Android you can have multiple threads running at the same time in one application. The UI Thread, allows you to move through the screen, while, for example, a background task is receiving data from a server and storing it in the database

14 Async Tasks As we are aware that any User interaction on any Android application should and must response with in 5 seconds, failure to which results in ANR Dialog. Yes "Application Not Responding" Dialog with 'Wait' and 'Force Close' inputs. Asynchronous operations can also be performed using Threads in accordance with Handler and messages to update UI about the action progress.

15

16 problem If you perform a long lasting operation, e.g. loading a file or accessing data from the Internet, the user interface of your Android Application will block until the corresonding code has finished.

17 Multithreading Multithreaded GUI (graphical user interface)- based programs remain responsive to users while performing other tasks, such as repaginating or printing a document. Threaded programs typically finish faster than their nonthreaded counterparts. This is especially true of threads running on a multiprocessor machine, where each thread has its own processor

18 Hints Do not call the methods onPreExecute, doInBackground and onPostEx ecute manually. This is automatically done by the system. You cannot call an AsyncTask inside another AsyncTask or Thread. The call of the method execute must be done in the UI Thread. The method onPostExecute is executed in the UI Thread (here you can call another AsyncTask!). The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.

19 doInBackGround() public void makeLemonade(String[] args) {and public void makeLemonade(String... args) {the code inside the method would be the same, but when it was called, they would be called differently. The first would need to be called like this: makeLemonade(new String[]{"lemon1", "lemon2", "lemon3"});while the second one's method signature could have 0 to (an assumed)infinite number of arguments, but they would all need to be String arguments. All of the following calls would work: makeLemonade("lemon1"); makeLemonade("lemon4", "lemon7", "lemon11", "lemon12");

20 IntentService Class It is important to note that once your service has finished executing a task, it should be stopped as soon as possible so that it does not unnecessarily hold up valuable resources. That’s why you use the stopSelf() method to stop the service when a task has been completed. Unfortunately, a lot of developers often forgot to terminate the service when it is done performing its task. To easily create a service that runs a task asynchronously and terminates itself when it is done, you can use the IntentService class

21 IntentService class (continued--) IntentService class is a base class for service that handles asynchronously requests on demand it is started just like a normal service and it executes its tasks within a worker thread and terminates itself when task is completed


Download ppt "Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),"

Similar presentations


Ads by Google