Develop a Smartphone app and calling WebService

xiaoxiao2021-03-06  75

Orange SPV is the first mobile phone running Smartphone 2002 to enter the market, now we can start developing a Smartphone application. This article we have established a Smartphone client that can use the ASP.NET Web service to provide geographic information. This article assumes that the reader has the basic knowledge of web services, ASP.NET, and Win32 programming.

The .NET Framework Components makes us have a set of classes to help use the web service, from the analysis of the WSDL document to the automatic establishment proxy class to rely on program. This makes most programming of the Web service trivial, but the simple framework component (Compact Framework) is not used for Smartphone, so we need other options.

Typical COM can use the SOAP toolkit, which cannot be used for Smartphone, but we will use it to help track calls.

We can rely on Web service programming still do not use these technologies. In order to implement it, we need to view the format and transfer of the web service. Use two standards in .NET 1.0 Web services: SOAP and WSDL.

Web service foundation

If no SOAP-based API helps use a web service, we need to process the SOAP message and the format specified by the WSDL document. Note that one of the benefits of using WSDL is a summary information in type elements. This information specifies the format of the SOAP body, launching the WSDL aware client (for example, .NET) to define the SOAP package that can be string and converted.

For SmartPhone, we must construct your SOAP message to make sure they can be accepted by the web service. These messages are sent to the server via HTTP POST, and our client waits for the SOAP response. This requires analysis, check the SOAP error, and return the extracted value. It seems that this requires a lot of work, but in practice it is similar to the low-rise API provided by the SOAP toolkit.

Manually constructed SOAP messages from pure WSDL is a complex transaction (such as map.wsdl display). This requires the correct operating element to find the input message, map the correct type. If the web service is ASP.NET, a simple way is to view the generated ASMX page, select WebMethod, and view the SOAP request example (Figure 1).

Figure 1 WEB service documentation from ASP.NET

If you cannot access this information, another mechanism is used to support WSDL client calling Web services and track SOAP packages. The SOAP Toolkit provides a tool TRACE Utility (MSSOAPT3.exe), which is useful for debugging Web services.

Figure 2 Trace Utility, from SOAP Toolkit 3.0

Smartphone Web Services

It is easy to see how to construct SOAP messages in the request. In this example, I built a SOAPWRITER class to provide a low-level feature that writes SOAP messages.

#include "soap.h"

SOAPWRITER * PSOAP = New soapwriter ();

PSoAP-> Startenvelop ();

PSoAP-> StartBody ();

PSoAP-> StartElement (l "getlatlong", l "http: // mapmobile");

PSoAP-> WriteElementstring (L "AddressLine", L "new bond street");

PSoAP-> WriteElementstring (l "city", l "bath");

PSoAP-> StartElement (l "postcode");

PSoAP-> endelement (l "postcode"); PSoAP-> WriteElementstring (L "Country", L "UK");

PSoAP-> endelement (l "getlatlong");

PSoAP-> endbody ();

PSoAP-> EndENVelope ();

PSoAP-> FinalizesoAP ();

In order to send a SOAP request to the web service, I will use the Wininet API. Wininet is generally used in HTTP and FTP communication. It provides a high-level API for Internet access without using Winsock programming. Fortunately, the web service uses the SOAP call to use HTTP calls in our example.

In order to make the sending SOAP request easier, I created the SOAPConnector class, which allowed us to send SOAP requests and load response information. Once connected to the web service, the client may make multiple calls, using the access program to retrieve SOAP response information.

#include "soap.h"

SOAPWRITER * PSOAP = New soapwriter ();

/ / Establish SOAP request here

SOAPCONNECTOR * PCON = New soapConnector ();

PCON-> INIT ();

// Connect to the server and web service

PCON-> Connect (l "http: // chungw02: 8080 / mapmobile / map.asmx");

// Call together with SOAP messages and soapaction

PCON-> Invoke (PSoAP, L "http://mapmobile / getlatlong");

// Extract response information

INT Ilen = 0;

PCON-> GetsoAplength (& Ilen);

TCHAR * PRESPONSE = New Tchar [Ilen 1];

PCON-> GetSoap (& president);

The XML file received by the client is then loaded into the DOM analyzer and returns the extracted value. In this example, I built a packaging class exposed to the IXmldomDocument interface in MSXML, which requires COM.

#include "soap.h"

// Read SOAP Response Information

PSoAPReader = new soapreader ();

PSoAPReader-> init ();

// Load SOAP response information by passing the SOAPConnector or XML string

PSoAPReader-> loadingXML (PCON);

Once it is loaded, you can access the DOM, select the node value or run the XPath query via m_pdom.

MSXML :: ixmldomnode * pnode = null;

Msxml :: ixmldomnodelist * pnodelist = null;

MSXML :: ixmldomnode * ptextNode = null;

Tchar * lpnodevalue = NULL;

Tchar * xpath = new tchar [50];

_TCSCPY (XPATH, L "/ soap: envelope / soap: body / node");

Variant vNodeVal;

HRESULT HR;

// Use the previously established PSoAPReader

Try {

// Select node

HR = PSoAPReader -> m_pdom-> selectsinglenode (xpath, & pnode);

IF (Failed (HR))

__leave;

IF (pnode == null) __ leave;

// Get a child node

HR = pnode-> get_childnodes;

IF (Failed (HR))

__leave;

/ / Get the next node

HR = pnodelist-> get_item (0, & ptextNode);

IF (Failed (HR))

__leave;

// Get text node value

Variantinit (& vNodeVal);

HR = ptextNode-> get_nodevalue (& vnodeval);

IF (Failed (HR)) {

VariantClear (& vNodeVal);

__leave;

}

/ / Specify the value to LPNODEVALUE

LPNODEVALUE = tchar [sysStringlen (vNodeVal.bstrval) 1];

_TCSCPY (LPNodeValue, vNodeVal.Bstrval);

VariantClear (& vNodeVal);

// Handling transactions using LPNODEVALUE

}

__finally {

IF (PNode! = NULL)

Pnode-> Release ();

IF (pnodelist! = null)

Pnodelist-> release ();

IF (PTEXTNode! = NULL)

PTEXTNODE-> Release ();

IF (LPNodeValue! = NULL)

DELETE [] lpnodevalue;

}

As a choice, I provide an auxiliary method SelectSingletExtNode to extract text nodes. It assigns TCHAR small space to accommodate the value of the text node.

// Note these auxiliary methods SelectSingletExtNode assignment

Tchar * latitude = NULL;

TCHAR * longitude = null;

// Use the previously established PSoAPReader

PSoAPReader-> SelectsingletExtNode (L "/ soap: envelope / soap: body / getlatlongResponse

/ latitude ", & latitude);

PSoAPReader-> SelectsingletExtNode (L "/ soap: envelope / soap: body / getlatlongResponse

/ longitude ", & longItude);

// Use return value to handle transactions

// Clear

IF (Latitude! = NULL)

delete [] latitude;

IF (longitude! = NULL)

DELETE [] longitude;

The upcoming web service returns a SOAP message containing text node data, so the SelectSingleTextNode method provides a useful mechanism for extracting data from the web service call. If you often need to extract data, such as attribute data, and evaluation node collections, the SOAPReader class can expand the other auxiliary method.

This is the function of SoapWriter, SOAPConnector, and SoapReader, call, and read and respond to the Web service. In order to implement the client with these classes, we need to understand the format of the SOAP message. Here, you will see the WEB service and the Smartphone client.

MapMobile and Mappoint .NET Web Services

The routine contains a client that can retrieve the map information from the web service and displayed on the Smartphone. Geographic Search and Map Data are provided by mappoint .net, it is a commercial Web service. Although the SmartPhone end user does not have a mappoint .NET account, I built a wrapper web service called MapMobile to rely on mappoint .net for calls and authentication. For Smartphone 2002, there is no summary (Digest) authentication provider, so if I want to talk directly to the Mappoint .NET. NET, you need to manually implement the certification header. Other reasons for packaging is to include the ability to change geodic data providers, without affecting end users, such as upgrading from mappoint .NET 2.0 to 3.0. Figure 3. MapMobile structure

Our packaging service exposes two simple web service methods to enable the client to locate and display the map. The first call returns the longitude and latitude of the address, and the second call returns the byte array containing the matching GIF image.

[WebMethod]

[SoapHeader ("_ phoneenumber", direction = soapheaderdirection.in)]

Public void getlatlong (String AddressLine, String City, String Postcode, String

Country, Out Double Latitude, Out Double Longitude

[WebMethod]

[SoapHeader ("_ phoneenumber", direction = soapheaderdirection.in)]

Public Byte [] getMap (double latitude, double longitude, double zoom, int width,

INT Height, String Tag)

In order to prevent unlimited calls of the MapMobile web service, all calls contain a SOAP header, which contains the mobile phone number of the Smartphone. At the server side, the detection head information is a valid number. If it fails, the SOAP fault will return to the client. This authentication mechanism only serves as an example of use, no deception of effective numbers. Due to a lot of security options, they rely on your web service, you can choose not authentication, use digital signatures, or use the new global XML Web Services (Global XML Web Services Architecture, GXA).

Smartmap client

The client application provides an input screen for entering the address. The MapMobile Web service is then called and retrieves the map stored in the file system. This allows users to view the latest map downloaded.

Figure 4. SmartMap User Interface

Our client application contains a boot screen (main window body class) and three dialogs (input screen, web service call status, and map display). At first start the screen display, follow the display of the Find Location dialog. The device phone number is also extracted and saved for SOAP headers. These calls do not work in the emulator, so in our example, if the call fails, the number is set to 1234567890 for testing.

Tchar * g_phonenumber = new tchar [40];

SMS_ADDRESS PADDR;

/ / Get a phone number

SMSGETPHONENUMBER (& PADDR);

_TCSCPY (g_phonenumber, paddr.ptsaddress);

When the user selects the Search menu option, the application constructs the correct SOAP call for the MapMobile Web service. This includes a SOAP header containing the device phone number. SOAPWRITER * PSOAP = New soapwriter ();

SW-> Startenvelope ();

// Context

SW-> StartHeader ();

SW-> StartHeadeRelement (L "MapMobileHeader", l "http: // mapmobile");

SW-> WriteHeadeRelementstring (l "phoneenumber", g_phonenumber;

SW-> endheadelelement (L "MapMobileHeader");

SW-> endheader ();

// SOAP body

SW-> StartBody ();

// Construct SOAP body here

SW-> endbody ();

SW-> EndENvelope ();

SW-> finalizesoap ();

After this is completed, the SOAPConnector will be loaded into the SOAPReader for extracting 64-bit encoded data.

TCHAR * MAP64;

// Allocate memory to the target variable

hrsearch =

PSoAPReader-> SelectsingletExtNode (L "/ soap: envelope / soap: body / getmapResponse / getmap

Result ", & Map64);

We need to constantly check HRESULT. If the SOAPReader fails, check whether the response information contains the SOAP failure.

Tchar & SOAPFAULT;

hrsearch =

PSoAPReader-> SelectsingletExtNode (L "/ soap: envelope / soap: body / soap: fault

/ faultstring ", & SoapFault);

if (Succeeded (Hrsearch))

MessageBox (HDLG, SOAPFAULT, L "soap error", mb_ok | mb_iconwarning);

Else

MessageBox (HDLG, L "Call Failed", L "Error", MB_ok | MB_ICONWARNING);

Figure 5. SOAP fault back client

If all calls are successful, the 64-bit string will decode into a character array and save it as a smartmap.gif. We hope that the file is also saved in the My Documents folder on the phone. However, the file structure of the curing memory can be changed by OEM, so call the SHGETSPECIALFOLDERPATH API to retrieve this.

Char szmapfile [MAX_PATH];

ShgetspecialFolderPath (HWND, SZMAPFILE, CSIDL_PERSONAL, FALSE);

_TCSCAT (SZMAPFILE, L "// smartmap.gif");

// Szmapfile on Orange SPV is included in / IPSM / My Document / SmartMap.gif

// On the simulator: / my document / smartmap.gif

Finally, the MAP dialog is established, which contains an image control (IDC_IMAGE).

Figure 6. Select other data

When the dialog is loaded, the saved map image will be read from the file system and the STM_setImage passes to the dialog handle. This image is loaded with the IMGDecmp.dll image, and part of the CE 3.0 platform is available.

Hbitmap g_hbm = null;

ReadPictureFile (HDLG, SzmapFile, & g_hbm); SenddlgiteMmessage (HDLG, IDC_IMAGE, STM_SETIMAGE, Image_bitmap, (LPARAM) G_HBM);

Figure 7. Select other data

At this time, the SmartMap client has almost completed. If possible, the user needs to view the latest map. In order to implement function, a pop-up menu is used, in the Option button of the Find Location dialog. If the file exists, a WM_INITMENUPOPUP message is captured, and the pop-up menu is activated or disabled.

// Temperature call before menu

Case WM_INITMENUPOPUP:

{

/ / View whether the file exists and whether the latest map menu is activated

DWORD DWFILE = GetFileAttributes (SZMAPFile);

Bool benabled = (dwfile == 0xfffffffff? False: true);

EnableMenuItem ((HMENU) WPARAM, ID_EXIT_LASTMAP, MF_BYCOMMAND |

(BENABLED? MF_ENABED: MF_GRAYED));

Break;

}

If the user can select this menu, then the MAP dialog will be established again, it automatically loads the image from the file system.

to sum up

I have reviewed the contents of Web services, tools, and technology to help see the SOAP package. Using the routines provided, we see a way to use a web service in the example mappoint .NET in SMARTPHONE 2002. By establishing a WEB Service-Aware client, we have gained the benefits of a new generation of applications on mobile devices that have a hybrid connection environment, connect, and disconnect, total and store data, and provide new experience. .

转载请注明原文地址:https://www.9cbs.com/read-92353.html

New Post(0)