当前位置: 代码迷 >> Web前端 >> WebService处理.net中Dataset有关问题解决方法(转)
  详细解决方案

WebService处理.net中Dataset有关问题解决方法(转)

热度:726   发布时间:2012-10-26 10:30:59.0
WebService处理.net中Dataset问题解决办法(转)

前段日子看到了一个提供WEB服务的网站-www.webxml.com.cn.它提供各种适用的Web服务。如天气预报、火车时刻表。

?

?

?

?

?

?

?

?

?

于是就通过它提供的接口,实验了一下,看看它提供的那些接口有什么效果。但是因为是免费用户,所以每次访问的次数有限制,所以只是测试一下。

但是你需要从WSDL文档中生成客户端支持代码,可以使用axis的wsdl2java:

%AXIS2_HOME%\bin\wsdl2java -uri?http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl?-p client -s -o stub

其中-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。-p参数指定了生成的Java类的包名,-o参数指定了生成的一系列文件保存的根目录。

在执行完上面的命令后,读者就会发现在当前目录下多了个stub目录,在."stub"src"client目录可以找到一个SimpleServiceStub.java文件,该文件复杂调用WebService

可以在程序中直接使用这个类。

?

我用了好几个服务,火车,天气,城市代码等。

先贴我的pojo,pojo需要和它提供的入口的属性一致,代码如下:

?Train.java

?

Java代码?
  1. package?com.jushi.pojo;??
  2. ??
  3. public?class?Train?{??
  4. ??
  5. ????private?String?trainStation;??
  6. ????private?String?arriveTime;??
  7. ????private?String?startTime;??
  8. ????private?String?km;??
  9. ??????
  10. ????private?String?firstStation;??
  11. ????private?String?lastStation;??
  12. ????private?String?trainCode;??
  13. ????private?String?startStation;??
  14. ????private?String?arriveStation;??
  15. ????private?String?useDate;??
  16. ??????
  17. ????public?String?getTrainStation()?{??
  18. ????????return?trainStation;??
  19. ????}??
  20. ????public?void?setTrainStation(String?trainStation)?{??
  21. ????????this.trainStation?=?trainStation;??
  22. ????}??
  23. ????public?String?getArriveTime()?{??
  24. ????????return?arriveTime;??
  25. ????}??
  26. ????public?void?setArriveTime(String?arriveTime)?{??
  27. ????????this.arriveTime?=?arriveTime;??
  28. ????}??
  29. ????public?String?getStartTime()?{??
  30. ????????return?startTime;??
  31. ????}??
  32. ????public?void?setStartTime(String?startTime)?{??
  33. ????????this.startTime?=?startTime;??
  34. ????}??
  35. ????public?String?getKm()?{??
  36. ????????return?km;??
  37. ????}??
  38. ????public?void?setKm(String?km)?{??
  39. ????????this.km?=?km;??
  40. ????}??
  41. ????public?String?getFirstStation()?{??
  42. ????????return?firstStation;??
  43. ????}??
  44. ????public?void?setFirstStation(String?firstStation)?{??
  45. ????????this.firstStation?=?firstStation;??
  46. ????}??
  47. ????public?String?getLastStation()?{??
  48. ????????return?lastStation;??
  49. ????}??
  50. ????public?void?setLastStation(String?lastStation)?{??
  51. ????????this.lastStation?=?lastStation;??
  52. ????}??
  53. ????public?String?getTrainCode()?{??
  54. ????????return?trainCode;??
  55. ????}??
  56. ????public?void?setTrainCode(String?trainCode)?{??
  57. ????????this.trainCode?=?trainCode;??
  58. ????}??
  59. ????public?String?getStartStation()?{??
  60. ????????return?startStation;??
  61. ????}??
  62. ????public?void?setStartStation(String?startStation)?{??
  63. ????????this.startStation?=?startStation;??
  64. ????}??
  65. ????public?String?getArriveStation()?{??
  66. ????????return?arriveStation;??
  67. ????}??
  68. ????public?void?setArriveStation(String?arriveStation)?{??
  69. ????????this.arriveStation?=?arriveStation;??
  70. ????}??
  71. ????public?String?getUseDate()?{??
  72. ????????return?useDate;??
  73. ????}??
  74. ????public?void?setUseDate(String?useDate)?{??
  75. ????????this.useDate?=?useDate;??
  76. ????}??
  77. ??????
  78. }??

?

?

City.java

?

Java代码?
  1. package?com.jushi.pojo;??
  2. ??
  3. public?class?City?{??
  4. ??????
  5. ????private?String?cityId;??
  6. ????private?String?cityName;??
  7. ????public?String?getCityId()?{??
  8. ????????return?cityId;??
  9. ????}??
  10. ????public?void?setCityId(String?cityId)?{??
  11. ????????this.cityId?=?cityId;??
  12. ????}??
  13. ????public?String?getCityName()?{??
  14. ????????return?cityName;??
  15. ????}??
  16. ????public?void?setCityName(String?cityName)?{??
  17. ????????this.cityName?=?cityName;??
  18. ????}??
  19. }??

??

?

接下来就是业务了。我们需要将它返回的数据集处理成我们需要的结果,所以用到了jdom。

?代码如下:

TrainInfo.java

?

Java代码?
  1. package?com.jushi.service;??
  2. ??
  3. import?java.io.File;??
  4. import?java.io.FileInputStream;??
  5. import?java.io.FileWriter;??
  6. import?java.io.IOException;??
  7. import?java.util.ArrayList;??
  8. import?java.util.List;??
  9. ??
  10. import?org.apache.axis.message.MessageElement;??
  11. import?org.jdom.Document;??
  12. import?org.jdom.Element;??
  13. import?org.jdom.input.SAXBuilder;??
  14. ??
  15. import?cn.com.WebXml.GetDetailInfoByTrainCodeResponseGetDetailInfoByTrainCodeResult;??
  16. import?cn.com.WebXml.GetStationAndTimeByStationNameResponseGetStationAndTimeByStationNameResult;??
  17. import?cn.com.WebXml.TrainTimeWebServiceLocator;??
  18. import?cn.com.WebXml.TrainTimeWebServiceSoap;??
  19. ??
  20. import?com.jushi.pojo.Train;??
  21. ??
  22. public?class?TrainInfo?{??
  23. ??
  24. ????//通过车次得到列车信息???
  25. ????public?ArrayList?getInfo(String?traincode){??
  26. ????????ArrayList?trainlist?=?null;??
  27. ????????try{??
  28. ????????????java.net.URLEncoder.encode("参数","UTF-8");??
  29. ????????????TrainTimeWebServiceLocator?service?=?new?TrainTimeWebServiceLocator();??
  30. ????????????TrainTimeWebServiceSoap?client?=?service.getTrainTimeWebServiceSoap();??
  31. ????????????GetDetailInfoByTrainCodeResponseGetDetailInfoByTrainCodeResult?traininfo?=??
  32. ????????????????client.getDetailInfoByTrainCode(traincode,?"");//后面的是我新注册的userID:?eb01f699583d44d4bdee0ed47c0397d5??
  33. ????????????MessageElement[]?msg?=?traininfo.get_any();??
  34. ????????????List?elementHead?=?msg[0].getChildren();//消息头,DataSet对象??
  35. ????????????List?elementBody?=?msg[1].getChildren();//消息体信息,DataSet对象??
  36. ????????????if(elementBody.size()<=0){??
  37. ????????????????return?null;??
  38. ????????????}??
  39. ????????????String?str?=?elementBody.get(0).toString();//消息体的字符串形式??
  40. ????????????saveXMLString(str,"train.xml");??
  41. ????????????trainlist?=?loadXML("train.xml");??
  42. ??????????????
  43. ????????}catch(Exception?e){??
  44. ????????????e.printStackTrace();??
  45. ????????}??
  46. ????????return?trainlist;??
  47. ????}??
  48. ??????
  49. ????public?static?void?saveXMLString(String?xmlString,String?filename)?throws?IOException{??
  50. ????????????File?file?=?new?File(filename);??
  51. ????????????if(file.exists()){??
  52. ????????????????file.delete();??
  53. ????????????}?????????
  54. ????????????file.createNewFile();??
  55. ????????????if(file.canWrite()){??
  56. ????????????????FileWriter?out?=?new?FileWriter(file);??
  57. ????????????????out.write(xmlString);??
  58. border-left: #d1d7dc 1px solid; padding-bottom: 0px; line-height: 18px; background-color: #fafafa; margin: 0px 0px 0px 38px; padding-
  相关解决方案