|
|
|
|
|
- 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.
10 min 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: - On the "Project" tab, in the "Project" group, expand "Import" and select "A web service".
- The import wizard starts. Go to the next step.
- 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.
- Go to the next step. The web service is imported.
- Validate the information window. The imported web service is in the "Imported web services" folder of the "Project explorer" pane.
- 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: 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:
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: - Open the "IW_Product" internal window in the editor (double-click on it in the "Project explorer" pane).
- Reduce the width of the "Reorder" control.
- Delete the "Quantity" control.
- Add a Button control:
- On the "Creation" tab, in the "Usual controls" group, click .
- Click in the window next to the "Reorder" control.
- The control is automatically created.
- Modify the characteristics of the control (select "Description" in the context menu). Set "BTN_Stock" as name and "Stock" as caption.
- Open the events associated with the control (select "Code" in the context menu).
- Write the following WLanguage code in the "Click BTN_Stock" event:
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.
- Close the code editor and save the window ( or Ctrl + S).
- Close the "IW_Product" internal window.
Testing the web service We will test the web service: - Open the WIN_Advanced_LIST_Products window (e.g., press CTRL + E).
- Test the window (click in the quick access buttons).
- Double-click on a product: the product form is displayed.
- Click "Stock".
- 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. - Go back to the editor and open the code of the BTN_Stock control.
- Comment out the existing code (select the lines of code and press Ctrl + //).
- Write the following code:
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
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.
- Close the code editor and save the window ( or Ctrl + S).
Testing the REST web service call We will test the web service: - If necessary, open the WIN_Advanced_LIST_Products window (e.g., press CTRL + E).
- Test the window (click in the quick access buttons).
- Double-click on a product: the product form is displayed.
- Click "Stock".
- Validate the information window and close the simulator.
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:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|