gSOAP正在Windows Mobile平台上的操做总结.docx
- 文档编号:23775834
- 上传时间:2023-05-20
- 格式:DOCX
- 页数:11
- 大小:69.40KB
gSOAP正在Windows Mobile平台上的操做总结.docx
《gSOAP正在Windows Mobile平台上的操做总结.docx》由会员分享,可在线阅读,更多相关《gSOAP正在Windows Mobile平台上的操做总结.docx(11页珍藏版)》请在冰豆网上搜索。
gSOAP正在WindowsMobile平台上的操做总结
gSOAP在WindowsMobile平台上的使用总结
gSOAP是很好的东西,它弥补了C++库对Webservice支持的不足,让C++的开发者能够轻松使用Webservice,不过说轻松其实也不轻松,到目前为止,我没有用过什么开源的库是一到手就能很顺利地使用的,总是经过了这个那个的折腾,最后才能用,虽然很多问题也都是只差那么一丁点儿,但就是那么一丁点儿却让人焦头烂额。
WindowsMobile没落了……我不止一次提起这话,我甚至怀疑我现在开发的WindowsMobile程序是不是最后一个获得较多用户的WindowsMobile程序,也许弄完了这个之后,也没什么人再会涉足这个领域了。
ASMX接口定义文件
OK,废话不说了,言归正传,Webservice最最最典型的应用是什么?
——更新天气,你看看Webservice的入门文章,都是拿天气更新作为范例,而我做的这个正好也是一个天气更新,接口是我定义的,具体就不贴出来了,总而言之我们要从WSDL这个接口文件出发,假设你已经有了这个WSDL文件了,文件名为“SSPWeatherService.asmx”。
(不懂WSDL的话建议先了解下Webservice)
获取一份gSOAP并安装
接着要去获取一份gSOAP的代码,地址是,我下载的版本是2.8.3,这是2011年6月更新的,在这前我下载了2.8.2,这两个版本用起来还有些微小的差别,哪个更好?
当然是新的更好了。
下载完之后当然是解压缩,我是把它解压缩到“D:
\gsoap”这个路径下。
然后给系统环境变量“path”增加这么一个路径:
“D:
\gsoap\gsoap\bin\win32\”,这完全是为了一会儿方便调用到“wsdl2h.exe”和“soapcpp2.exe”,否则你还得输入exe的完整路径。
根据asmx生成相关文件
将刚才那个WSDL文件“SSPWeatherService.asmx”放到你的工作目录下,比如“D:
\work\SSPWeatherUpdate_WS”,然后使用命令行工具,如下执行:
其中涉及到两个命令:
>wsdl2hSSPWeatherService.asmx-oSSPWeatherService.h
>soapcpp2SSPWeatherService.h-ID:
\gsoap\gsoap\import-C-x-i
第一个命令是根据WSDL文件生成相应的头文件,用-o参数指定生成的头文件的名称。
第二个命令死根据刚生成的头文件来生成别的头文件和cpp文件。
-I后面是gSOAP的import目录的路径,这个是必须的,-C表示只生成客户端代码,这正是我们需要的,-x可以少生成一些垃圾,-i表示生成C++封装代码,用C++封装好的代码比纯C代码好用多了。
对生成内容的简单说明
接下来你查看目录中的文件可能是这样:
soapC.cpp
soapH.h
soapSSPWeatherServiceSoapProxy.cpp
soapSSPWeatherServiceSoapProxy.h
soapStub.h
SSPWeatherService.asmx
SSPWeatherService.h
SSPWeatherServiceSoap.nsmap
也就是说,除了asmx和第一步生成的h文件之外,之后生成的文件有这些:
soapC.cpp
soapH.h
soapSSPWeatherServiceSoapProxy.cpp
soapSSPWeatherServiceSoapProxy.h
soapStub.h
SSPWeatherServiceSoap.nsmap
可能你还会碰到下面这几个文件,这跟你原本的asmx的接口定义有关系:
soapSSPWeatherServiceSoap12Proxy.cpp
soapSSPWeatherServiceSoap12Proxy.h
SSPWeatherServiceSoap12.nsmap
“12”表示soap的1.2版本,你比较一下,发现这几个文件跟上面提到的几个文件的内容是几乎一致的,除了里面的名称大多都加上了“12”,对我来说这几个文件是不需要的,所以删除掉了。
创建一个工程
接下去当然是用VisualStudio创建一个工程来使用刚才生成的这些文件了。
我创建的Project名称为“SSPWeatherUpdate_WS”,目录也是刚才的那个目录,为了简单起见,创建一个console类型的程序用来测试就行了。
然后把刚才生成的这些文件添加到这个Project中去:
soapC.cpp
soapH.h
soapSSPWeatherServiceSoapProxy.cpp
soapSSPWeatherServiceSoapProxy.h
soapStub.h
SSPWeatherService.h
也许你注意到了,asmx和nsmap文件是不需要添加的。
然后是很关键的一部,把D:
\gsoap\gsoap目录下的stdsoap2.h和stdsoap2.cpp复制到刚创建的工程目录并添加到工程中去。
完了之后Project里应该有这些东西:
编译以及可能的问题
编译一下看看,能不能通过。
可能出现了一大堆的错误,可能你会看到这样的出错提示:
“1>.\soapC.cpp(16):
warningC4627:
'#include"soapH.h"':
skippedwhenlookingforprecompiledheaderuse”
这是因为工程设置了使用“预编译头”,我们不要使用预编译头,工程属性设置如下图:
设置后rebuild,看看还有没有什么问题?
在我这里出现了这样的错误提示:
1>.\soapC.cpp(850):
errorC3861:
'soap_outdateTime':
identifiernotfound
1>.\soapC.cpp(855):
errorC3861:
'soap_indateTime':
identifiernotfound
表面上看是漏掉某个头文件,或者某个编译选项不正确引起,但其实,这正是让我郁闷了好久,努力了好久,最后才发现无解的问题,其中波折就不想在这里赘述了,如果你认为自己技术水平不错,可以直接摆平这个问题的话不妨尝试一下看,但如果时间不是很多的话我劝你就算了,直接采纳我这个结论:
gSOAP在WM环境下不支持WSDL中的datetime类型!
我不知道这算不算bug,可能准确说“支持不佳”,如果前面的asmx是你定义的话,你就改一改,把其中的datetime类型改为string,然后自己在程序中再作转换,如果asmx不是你定义的话,那就很不幸了,我也没辙了,修改gSOAP的代码是很痛苦的工作,我费了很大力气最后都没解决,如果你有能力解决,不妨跟我分享一下。
使用Webservice
我直接贴上我的完整代码,希望能够抛砖引玉。
#include "stdafx.h"
#include
#include "soapSSPWeatherServiceSoapProxy.h"
#include "SSPWeatherServiceSoap.nsmap"
using namespace std;
BOOL UTF8ToTChar(const char* pUTF8Str, TCHAR* &pTChar)
{
//First, convert it to UNICODE
INT len = MultiByteToWideChar(CP_UTF8, 0, pUTF8Str, -1, NULL, 0);
WCHAR *pWC = new WCHAR[len];
MultiByteToWideChar(CP_UTF8, 0, pUTF8Str, -1, pWC, len);
pWC[len-1] = '\0';
//Second, convert UNICODE to TCHAR
#ifdef UNICODE
pTChar = pWC;
#else
len = WideCharToMultiByte(CP_ACP, 0, pWC, -1, NULL, 0, NULL, NULL);
pTChar = new TCHAR[len];
WideCharToMultiByte(CP_ACP, 0, pWC, -1, pTChar, len, NULL, NULL);
pTChar[len-1] = '\0';
delete[] pWC;
#endif
return TRUE;
}
BOOL TCharToUTF8(const TCHAR* pTChar, char* &pUTF8Str)
{
INT len;
#ifdef UNICODE
const WCHAR* pWC = pTChar;
#else
len = MultiByteToWideChar(CP_ACP, 0, pTChar, -1, NULL, 0);
WCHAR *pWC = new WCHAR[len];
MultiByteToWideChar(CP_ACP, 0, pTChar, -1, pWC, len);
pWC[len-1] = '\0';
#endif
len = WideCharToMultiByte(CP_UTF8, 0, pTChar, -1, NULL, 0, NULL, NULL);
pUTF8Str = new char[len];
WideCharToMultiByte(CP_UTF8, 0, pTChar, -1, pUTF8Str, len, NULL, NULL);
pUTF8Str[len-1] = '\0';
#ifdef UNICODE
//
#else
delete[] pWC;
#endif
return TRUE;
}
void ReleaseChar(char* &pChar)
{
if(pChar!
=NULL)
{
delete[] pChar;
pChar = NULL;
}
}
void ReleaseTChar(TCHAR* &pTChar)
{
if(pTChar!
=NULL)
{
delete[] pTChar;
pTChar = NULL;
}
}
#define OUTPUT_BUFF_LEN 512
void DbgStrOut(const TCHAR *fmt,
)
{
TCHAR szOutStr[OUTPUT_BUFF_LEN];
va_list ap;
va_start(ap, fmt);
StringCbVPrintf(szOutStr, OUTPUT_BUFF_LEN, fmt, ap);
va_end(ap);
OutputDebugString(szOutStr);
}
int _tmain(int argc, _TCHAR* argv[])
{
SSPWeatherServiceSoapProxy gs(SOAP_C_UTFSTRING);
gs.soap_endpoint = "
_ns1__FindCityByString input;
_ns1__FindCityByStringResponse output;
CHAR* pszUTF8;
TCHAR* pszCityCode;
TCHAR* pszCityName;
TCharToUTF8(L"闵行", pszUTF8);
input.CityToFind = pszUTF8;
ReleaseChar(pszUTF8);
if(SOAP_OK==gs.FindCityByString(&input, &output))
{
std:
:
vector
:
iterator it = output.CityInfo.begin();
while (it!
=output.CityInfo.end())
{
ns1__CityInfo *pCityInfo = (*it);
UTF8ToTChar(pCityInfo->CityCode.c_str(), pszCityCode);
UTF8ToTChar(pCityInfo->CityName.c_str(), pszCityName);
DbgStrOut(L"%s %s\n", pszCityCode, pszCityName);
ReleaseTChar(pszCityCode);
ReleaseTChar(pszCityName);
++it;
}
if(output.MatchCityNumber>=1)
{
_ns1__GetWeatherByCityCode input2;
_ns1__GetWeatherByCityCodeResponse output2;
input2.CityCode = output.CityInfo[0]->CityCode;
if(SOAP_OK==gs.GetWeatherByCityCode(&input2, &output2))
{
TCHAR* pszUpdateTime;
float fRtTemperature;
TCHAR* pszWindDirection;
TCHAR* pszWindForce;
UTF8ToTChar(output2.WeatherCast->UpdateTime.c_str(), pszUpdateTime);
fRtTemperature = output2.WeatherCast->RtTemperature;
UTF8ToTChar(output2.WeatherCast->RtWindDirection.c_str(), pszWindDirection);
UTF8ToTChar(output2.WeatherCast->RtWindForce.c_str(), pszWindForce);
DbgStrOut(L"Update Time :
%s\n", pszUpdateTime);
DbgStrOut(L"Temperature :
%.1f\n", fRtTemperature);
DbgStrOut(L"Wind Direction :
%s\n", pszWindDirection);
DbgStrOut(L"Wind Force :
%s\n", pszWindForce);
ReleaseTChar(pszUpdateTime);
ReleaseTChar(pszWindDirection);
ReleaseTChar(pszWindForce);
ns1__ArrayOfOneDayWeather *pOneDay = output2.WeatherCast->DayWeather;
std:
:
vector
:
iterator itDW = pOneDay->OneDayWeather.begin();
//std:
:
vector
:
iterator it = output.CityInfo.begin();
while (itDW!
=pOneDay->OneDayWeather.end())
{
DbgStrOut(L"##########\n");
ns1__OneDayWeather *pDayWeather = *itDW;
DbgStrOut(L"\tDay temp :
%.1f\n", pDayWeather->DtTemperature);
DbgStrOut(L"\tDay weather :
%d\n", pDayWeather->DtWeatherID);
TCHAR* pszDayWindDirection;
TCHAR* pszDayWindForce;
UTF8ToTChar(pDayWeather->DtWindDirection.c_str(), pszDayWindDirection);
UTF8ToTChar(pDayWeather->DtWindForce.c_str(), pszDayWindForce);
DbgStrOut(L"\tDay Wind Direction :
%s\n ", pszDayWindDirection);
DbgStrOut(L"\tDay Wind Force :
%s\n", pszDayWindForce);
ReleaseTChar(pszDayWindDirection);
ReleaseTChar(pszDayWindForce);
DbgStrOut(L"\tNight temp :
%.1f\n", pDayWeather->NtTemperature);
DbgStrOut(L"\tNight weather :
%d\n", pDayWeather->NtWeatherID);
TCHAR* pszNightWindDirection;
TCHAR* pszNightWindForce;
UTF8ToTChar(pDayWeather->NtWindDirection.c_str(), pszNightWindDirection);
UTF8ToTChar(pDayWeather->NtWindForce.c_str(), pszNightWindForce);
DbgStrOut(L"\tNight Wind Direction :
%s\n ", pszNightWindDirection);
DbgStrOut(L"\tNight Wind Force :
%s\n", pszNightWindForce);
ReleaseTChar(pszNightWindDirection);
ReleaseTChar(pszNightWindForce);
++itDW;
}
}
}
}
return 0;
}
代码说明
使用Webservice的过程中,我通过查找“闵行”来找到我的城市,再根据城市ID来获取天气,其实代码并不多,多在字符编码转换和Debug输出这部分,因为我们的XML使用UTF-8编码,而我们的软件界面通常使用Unicode编码,所以得转换,英文的情况下不转换是没什么问题的,但汉字一定得转,否则就是乱码了。
另外特别注意这个地方:
SSPWeatherServiceSoapProxygs(SOAP_C_UTFSTRING);
gs.soap_endpoint="
SOAP_C_UTFSTRING,这个是用来指定UTF-8编码的,一定不能少,下面这个soap_endpoint参数则用来指明这个Webservice的服务地址。
可能的问题以及解决方案
1,超时
程序发布之后有人反映不能使用,WindowsMobile如果用电脑直连的话就没任何问题,但如果用GPRS上网的话还真的可能出现失败的情况,我认为这是因为GPRS速度太慢(用起来感觉还不如以前56K猫拨号上网)导致超时的缘故。
可以通过下面办法来解决:
pGS.accept_timeout=30;
pGS.connect_timeout=30;
pGS.recv_timeout=30;
pGS.send_timeout=30;
pGS.linger_time=30;
2,无法用WAP方式更新
许多人用手机上网的时候都喜欢用WAP方式,就是那种用代理服务器的方式,移动的接入点叫cmwap,联通的叫uniwap,代理的地址是“10.0.0.172”。
由于连接用了代理,gSOAP也得显示指定一个代理,方法如下:
pGS->proxy_host=“10.0.0.172”;
pGS->proxy_port=80;
可能不一定是“10.0.0.172”,端口也可能不是80,这两个信息可以通过连接管理器相关的API来获得,具体就不展开了,提示一下,用这个:
ConnMgrProviderMessage。
我不知道还有什么疑难杂症,这篇文章也无法囊括所有内容,大家只能见招拆招了。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- gSOAP正在Windows Mobile平台上的操做总结 gSOAP 正在 Windows Mobile 平台 总结