Android Programming




   Lesson 10

Web services
 NGUYEN The Linh
Android Programming


Contents


      1    Overview

      2    Working with SOAP services

      3    Working with XML

      4    Using KSoap2 Library




                         2
Android Programming


Web services




               Overview




                  3
Android Programming


Overview

 What is a web service?
   A Web service is a method of communication between two
    electronic devices over the World Wide Web.

    The most important factor to the success and popularity of web
     services is the fact that its backbone is XML.




                                 4
Android Programming


Overview

 What is a web service?(cont.)




                          5
Android Programming


Overview

 What is a web service?(cont.)
                   Request -> POST

                        XML

                                       Web
                                     services


                       Response

                         XML
                           6
Android Programming


Web services




        Working with SOAP services




                    7
Android Programming


Working with SOAP services

 What is SOAP?
   SOAP stands for Simple Object Access Protocol

    SOAP is a communication protocol

    SOAP is for communication between applications

    SOAP is a format for sending messages

    SOAP communicates via Internet
                               8
Android Programming


Working with SOAP services

 What is SOAP? (cont.)
   SOAP is platform independent

    SOAP is language independent

    SOAP is based on XML

    SOAP is simple and extensible

    SOAP is a W3C recommendation
                               9
Android Programming


Working with SOAP services

 Why is SOAP Needed?
   It is important for application development to allow Internet
    communication between programs.

     HTTP is supported by all Internet browsers and servers.

     SOAP provides a way to communicate between applications
      running on different operating systems, with different technologies
      and programming languages.


                                  10
Android Programming


Working with SOAP services

 SOAP Building Blocks




                         11
Android Programming


Working with SOAP services

 SOAP Building Blocks (cont.)
    An Envelope element that identifies the XML document as a
     SOAP message.

    A Header element that contains header information.

    A Body element that contains call and response information.




                                12
Android Programming


Working with SOAP services




                   13
Android Programming


Working with SOAP services




                   14
Android Programming


Web services




               Working with XML




                      15
Android Programming


Working with XML

 What is XML?
   XML stands for EXtensible Markup Language
   XML is a markup language much like HTML

    XML was designed to carry data, not to display data
    XML tags are not predefined. You must define your own tags
    XML is designed to be self-descriptive

    XML is a W3C Recommendation


                               16
Android Programming


Working with XML

 What is XML Parser?
   A parser is a piece of program that takes a physical representation
    of some data and converts it into an in-memory form for the
    program as a whole to use.

    Parsers are used everywhere in software.

    An XML Parser is a parser that is designed to read XML and
     create a way for programs to use XML.


                                 17
Android Programming


Working with XML

 The main types of parsers?
    The main types of parsers are known by some names:
       • SAX
       • DOM
       • Pull




                               18
Android Programming


Working with XML

 What is SAX parser?
   A SAX (Simple API for XML) parser does not create any internal
    structure. Instead, it takes the occurrences of components of an
    input document as events, and tells the client what it reads as it
    reads through the input document.

    A SAX parser serves the client application always only with
     pieces of the document at any given time.




                                 19
Android Programming


Working with XML

 What is SAX parser? (cont.)
   A SAX parser, however, is much more space efficient in case of a
    big input document (because it creates no internal structure).
    What’s more, it runs faster and is easier to learn than DOM
    parser because its API is really simple.

    But from the functionality point of view, it provides a fewer
     functions, which means that the users themselves have to take
     care of more, such as creating their own data structures.



                                20
Android Programming


Working with XML




                   21
Android Programming


Working with XML




                   22
Android Programming


Working with XML

 What is DOM parser?
   A DOM (Document Object Model) parser creates a tree structure
    in memory from an input document and then waits for requests
    from client.

    A DOM parser always serves the client application with the entire
     document no matter how much is actually needed by the client.




                                23
Android Programming


Working with XML

 What is DOM parser? (cont.)
   A DOM parser is rich in functionality.

    It creates a DOM tree in memory and allows you to access any
     part of the document repeatedly and allows you to modify the
     DOM tree.

    But it is space inefficient when the document is huge, and it takes
     a little bit longer to learn how to work with it.


                                 24
Android Programming


Working with XML




                   25
Android Programming


Working with XML

 What is Pull parser?
   SAX is a push parser, since it pushes events out to the calling
    application.

    Pull parsers, on the other hand, sit and wait for the application to
     come calling. They ask for the next available event, and the
     application basically loops until it runs out of XML.




                                  26
Android Programming


Working with XML

 What is Pull parser? (cont.)
   Pull parsers are useful in streaming applications, which are areas
    where either the data is too large to fit in memory.

    It is designed to be used with large data sources, and unlike SAX
     which returns every event, the pull parser can choose to skip
     events that it is not interested in.




                                 27
Android Programming


Working with XML




                   28
Android Programming


Working with XML

 What is Pull parser? (cont.)
   The above example will generate the following output:
       •   Start document
       •   Start tag contact
       •   Text Nguyen Van A
       •   End tag contact
       •   End document




                                29
Android Programming


Working with XML

 Example 10.1
    GetLove App
    http://nikmesoft.com/apis/J4FServices/
   index.php




                                30
Android Programming


Web services




           Using KSoap2 Library




                   31
Android Programming


Using KSoap2 Library

 What is KSoap2?
   KSOAP is a SOAP web service client library for constrained
    Java environments such as Applets or J2ME applications (CLDC /
    CDC / MIDP).

    The ksoap2-android project provides a lightweight and efficient
     SOAP client library for the Android platform.

    http://code.google.com/p/ksoap2-android/


                                32
Android Programming


Using KSoap2 Library

 Why is KSoap2 Needed?

  Send
 Request
  (XML)
                       Get
                     Response
                      (XML)

                                  Parsing
                                   XML


                          33
Android Programming


Using KSoap2 Library

 Why is KSoap2 Needed?

  Send
 Request
  (XML)
                       Get
                     Response
                      (XML)

                                  Parsing
                                   XML


                          34
Android Programming


Using KSoap2 Library

 Why is KSoap2 Needed?
                                                Parsing
  Send                                           XML
 Request
                          KSOAP2
  (XML)




              Get
            Response               All In One
             (XML)


                          35
Android Programming


Using KSoap2 Library

 Example 10.2
    Using KSoap2




                       36
Android Programming

[Android] Web services

  • 1.
    Android Programming Lesson 10 Web services NGUYEN The Linh
  • 2.
    Android Programming Contents 1 Overview 2 Working with SOAP services 3 Working with XML 4 Using KSoap2 Library 2
  • 3.
  • 4.
    Android Programming Overview  Whatis a web service?  A Web service is a method of communication between two electronic devices over the World Wide Web.  The most important factor to the success and popularity of web services is the fact that its backbone is XML. 4
  • 5.
    Android Programming Overview  Whatis a web service?(cont.) 5
  • 6.
    Android Programming Overview  Whatis a web service?(cont.) Request -> POST XML Web services Response XML 6
  • 7.
    Android Programming Web services Working with SOAP services 7
  • 8.
    Android Programming Working withSOAP services  What is SOAP?  SOAP stands for Simple Object Access Protocol  SOAP is a communication protocol  SOAP is for communication between applications  SOAP is a format for sending messages  SOAP communicates via Internet 8
  • 9.
    Android Programming Working withSOAP services  What is SOAP? (cont.)  SOAP is platform independent  SOAP is language independent  SOAP is based on XML  SOAP is simple and extensible  SOAP is a W3C recommendation 9
  • 10.
    Android Programming Working withSOAP services  Why is SOAP Needed?  It is important for application development to allow Internet communication between programs.  HTTP is supported by all Internet browsers and servers.  SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages. 10
  • 11.
    Android Programming Working withSOAP services  SOAP Building Blocks 11
  • 12.
    Android Programming Working withSOAP services  SOAP Building Blocks (cont.)  An Envelope element that identifies the XML document as a SOAP message.  A Header element that contains header information.  A Body element that contains call and response information. 12
  • 13.
  • 14.
  • 15.
  • 16.
    Android Programming Working withXML  What is XML?  XML stands for EXtensible Markup Language  XML is a markup language much like HTML  XML was designed to carry data, not to display data  XML tags are not predefined. You must define your own tags  XML is designed to be self-descriptive  XML is a W3C Recommendation 16
  • 17.
    Android Programming Working withXML  What is XML Parser?  A parser is a piece of program that takes a physical representation of some data and converts it into an in-memory form for the program as a whole to use.  Parsers are used everywhere in software.  An XML Parser is a parser that is designed to read XML and create a way for programs to use XML. 17
  • 18.
    Android Programming Working withXML  The main types of parsers?  The main types of parsers are known by some names: • SAX • DOM • Pull 18
  • 19.
    Android Programming Working withXML  What is SAX parser?  A SAX (Simple API for XML) parser does not create any internal structure. Instead, it takes the occurrences of components of an input document as events, and tells the client what it reads as it reads through the input document.  A SAX parser serves the client application always only with pieces of the document at any given time. 19
  • 20.
    Android Programming Working withXML  What is SAX parser? (cont.)  A SAX parser, however, is much more space efficient in case of a big input document (because it creates no internal structure). What’s more, it runs faster and is easier to learn than DOM parser because its API is really simple.  But from the functionality point of view, it provides a fewer functions, which means that the users themselves have to take care of more, such as creating their own data structures. 20
  • 21.
  • 22.
  • 23.
    Android Programming Working withXML  What is DOM parser?  A DOM (Document Object Model) parser creates a tree structure in memory from an input document and then waits for requests from client.  A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client. 23
  • 24.
    Android Programming Working withXML  What is DOM parser? (cont.)  A DOM parser is rich in functionality.  It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree.  But it is space inefficient when the document is huge, and it takes a little bit longer to learn how to work with it. 24
  • 25.
  • 26.
    Android Programming Working withXML  What is Pull parser?  SAX is a push parser, since it pushes events out to the calling application.  Pull parsers, on the other hand, sit and wait for the application to come calling. They ask for the next available event, and the application basically loops until it runs out of XML. 26
  • 27.
    Android Programming Working withXML  What is Pull parser? (cont.)  Pull parsers are useful in streaming applications, which are areas where either the data is too large to fit in memory.  It is designed to be used with large data sources, and unlike SAX which returns every event, the pull parser can choose to skip events that it is not interested in. 27
  • 28.
  • 29.
    Android Programming Working withXML  What is Pull parser? (cont.)  The above example will generate the following output: • Start document • Start tag contact • Text Nguyen Van A • End tag contact • End document 29
  • 30.
    Android Programming Working withXML  Example 10.1  GetLove App  http://nikmesoft.com/apis/J4FServices/ index.php 30
  • 31.
    Android Programming Web services Using KSoap2 Library 31
  • 32.
    Android Programming Using KSoap2Library  What is KSoap2?  KSOAP is a SOAP web service client library for constrained Java environments such as Applets or J2ME applications (CLDC / CDC / MIDP).  The ksoap2-android project provides a lightweight and efficient SOAP client library for the Android platform.  http://code.google.com/p/ksoap2-android/ 32
  • 33.
    Android Programming Using KSoap2Library  Why is KSoap2 Needed? Send Request (XML) Get Response (XML) Parsing XML 33
  • 34.
    Android Programming Using KSoap2Library  Why is KSoap2 Needed? Send Request (XML) Get Response (XML) Parsing XML 34
  • 35.
    Android Programming Using KSoap2Library  Why is KSoap2 Needed? Parsing Send XML Request KSOAP2 (XML) Get Response All In One (XML) 35
  • 36.
    Android Programming Using KSoap2Library  Example 10.2  Using KSoap2 36
  • 37.