ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WINDEV Mobile Tutorial / Tutorial - Developing an Android and iOS application
  • Lesson 7 - Consuming a web service
  • Overview
  • Practical example
  • Importing a SOAP web service
  • Consuming a SOAP web service
  • Calling the web service
  • Testing the web service
  • Consuming a REST web service
  • Calling the REST web service
  • Testing the REST web service call
  • To sum up

Tutorial - Developing an Android and iOS application

Lesson 7 - Consuming a web service
We will cover the following topics:
  • Overview.
  • Importing and consuming a web service.
  • Consuming a REST web service.
Durée de la leçon 10 min
Overview
In most cases, a web service is defined as an application that can be accessed via standard Internet protocols. More specifically, web services allow several computers connected via Internet to interact.
Web services allow you to run procedures and processes on a remote Web server (.NET, SOAP or J2EE) from a client computer.
With WINDEV Mobile, these web services can be used as client, via the SOAP protocol on HTTP (the standard Internet protocol for transferring HTML pages) and with the SOAPxx, DotNetxx and J2EExx functions.
Regardless of the web server platform (.NET, J2EE, etc.), a web service can be accessed via SOAP.
With WINDEV Mobile, you don't have to be an expert in this field.
A wizard takes care of ("almost") everything!

Practical example

A web service specific to this tutorial allows you to try the different operations that can be performed on a web service.
When integrated to the "WM Product Management" project, this web service is used to query a supplier database to check whether a product is available in stock using its reference.
First, we will import the web service into the "WM Product Management" project, and then use it in the application to check the product availability via a Product form.
Warning
This lesson is based on the WM Product Management example used in Lesson 1. To follow this lesson, you must have completed the steps from the previous lessons.
Importing a SOAP web service
To import a web service into the project:
  1. On the "Project" tab, in the "Project" group, expand "Import" and select "A web service".
  2. The import wizard starts. Go to the next step.
  3. Specify the address into which the web service WSDL description must be imported:
    https://examples.webdev.info/WSTUTORIALV2_WEB/awws/WSTutorialV2.awws?wsdl

    Reminder: this web service is used to query a supplier database to check the availability (stock) of a product using its reference.
    Web service import wizard
  4. Go to the next step. The web service is imported.
    Import completed
  5. Validate the information window. The imported web service is in the "Imported web services" folder of the "Project explorer" pane.
  6. In the "Project explorer" pane, expand the "Imported web services" folder.
Let's take a closer look at the information displayed in the "Project explorer" pane:
'Project explorer' pane
The structure includes:
  • the web service name (WSTutorialV2 in this example),
  • the name of each function (ProductInStock in this example).
To see the web service call syntax, double-click the name of the function in the "Project explorer" pane. The code editor displays the function description, with the prototype for calling the function:
Call to function
Consuming a SOAP web service
In our example "WM Product Management", we will integrate the web service call in the internal window that displays the product details. We will create a "Stock" button to check the availability of the product via the web service.

Calling the web service

To use the web service:
  1. Open the "IW_Product" internal window in the editor (double-click on it in the "Project explorer" pane).
  2. Reduce the width of the "Reorder" control.
  3. Delete the "Quantity" control.
  4. Add a Button control:
    • On the "Creation" tab, in the "Usual controls" group, click Create a Button control.
    • Click in the window next to the "Reorder" control.
    • The control is automatically created.
  5. Modify the characteristics of the control (select "Description" in the context menu). Set "BTN_Stock" as name and "Stock" as caption.
    Position of the 'Stock' button
  6. Open the events associated with the control (select "Code" in the context menu).
  7. Write the following WLanguage code in the "Click BTN_Stock" event:
    // Display SOAP web service response
    InfoBuild("Number of products ""%1"" in stock: %2", SAI_Référence, ...
    	ProductInStock(EDT_Reference))
    Let's analyze this code:
    • The ProductInStock function of the web service is called. This code uses the function prototype that was displayed previously in the code editor.
    • The response is formatted and displayed.
  8. Close the code editor and save the window (Save window or Ctrl + S).
  9. Close the "IW_Product" internal window.

Testing the web service

We will test the web service:
  1. Open the WIN_Advanced_LIST_Products window (e.g., press CTRL + E).
  2. Test the window (click Test window in the quick access buttons).
  3. Double-click on a product: the product form is displayed.
  4. Click "Stock".
    Web service test
  5. Validate the information window and close the simulator.
Consuming a REST web service
In our example, we consumed a SOAP web service. Now we will consume a REST web service. In this case, you don't need to import the web service into the application. Simply call the desired web service procedure.
In our example, we deployed a REST web service that performs the same process as the SOAP web service used previously. This REST web service can be used to get the stock information of the specified product using the following address:
https://examples.webdev.info/product/<Product reference>

Calling the REST web service

Let's see how to use this REST web service in our example.
  1. Go back to the editor and open the code of the BTN_Stock control.
  2. Comment out the existing code (select the lines of code and press Ctrl + //).
  3. Write the following code:
    // Display REST web service response
    h is httpRequest
    h.Method = httpGet
    h.URL = "https://examples.webdev.info/product/" + EDT_Reference
    h.ContentType = "application/json"

    r is httpResponse = RESTSend(h)
    Content is ANSI string

    IF r.StatusCode = 200 THEN // Request successfully completed
    Content = r.Content
    Info(Content)
    ELSE
    Info("An error has occurred " +  ErrorInfo() + r.StatusCode + r.DescriptionStatusCode)
    END
    This code is used to:
    • Declare a variable of type httpRequest. This variable defines the parameters of the query sent to the REST web service. The properties of this variable are:
      • The method used: GET in our case.
      • Webservice call URL: this is the URL we've just seen. In our case, the product reference corresponds to the value in the EDT_Reference control.
      • The type of content of the HTTP message that will be sent to the server. In this case, the message is in JSON format.
    • Send the request to the server and get the response using RESTSend. The response is a variable of type restResponse.
    • Format the response.
  4. Close the code editor and save the window (Save window or Ctrl + S).

Testing the REST web service call

We will test the web service:
  1. If necessary, open the WIN_Advanced_LIST_Products window (e.g., press CTRL + E).
  2. Test the window (click Test window in the quick access buttons).
  3. Double-click on a product: the product form is displayed.
  4. Click "Stock".
  5. Validate the information window and close the simulator.
To sum up
Completed project
Do you want to check the end result of the steps described here? A completed project is available. This project contains the windows and reports created and used in this lesson. To open the corrected project, on the home page, click on "Tutorial", then in the "Tutorial - Complete application with data" area, click on the "Open corrected project" link.
In this lesson, we discovered how to handle SOAP and REST web services.
The mobile application development tutorial ends here.
To learn more about WINDEV Mobile, see the following documentation pages:
We also recommend that you follow the tutorial on how to deploy a mobile application.
Previous LessonTable of contents
Minimum version required
  • Version 2024
Comments
Click [Add] to post a comment

Last update: 11/19/2024

Send a report | Local help