由于app后续开发的需要,移植了两次webservice和数据库,遇到了不少问题,也花费了很多时间,实践告诉我要学会寻找问题的根源,这样才能在开发中节省时间,尽快解决问题!好,废话不多说,转入正题……
首先该应用时采用第三方类库ksoap2来实现Android与C#版的webservice之间的连接的,它是一个SOAP Web service客户端开发包,网上无论是C#版的webservice还是J2EE版的,一般都是通过ksoap2(准确地说是ksoap2 android)来实现两者间的连接的,至于具体怎样连接就不多说了,给个Android的webservice相关开发的开发汇总(感谢eoe的朋友):http://www.eoeandroid.com/thread-169417-1-1.html
然后说下第一次移植时遇到的一个非常坑爹的问题,移植到新的服务器后,测试一直发现在解析返回的数据,总是显示
SoapObject userInfo = (SoapObject) envelope.bodyIn;
userInfo = (SoapObject) envelope.getResponse();
的返回类型错误,不能强制转换SoapObject类型(具体什么错忘记了),然后又查找相关的错误,一直改,最后才发现是命名空间与webservice的不对应,而导致之后的系列错误。
ksoap2连接webservice的部分代码:
1 package cn.edu.stu.VillageMarket.utils;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7
8 import org.ksoap2.SoapEnvelope;
9 import org.ksoap2.serialization.SoapObject;
10 import org.ksoap2.serialization.SoapSerializationEnvelope;
11 import org.ksoap2.transport.HttpTransportSE;
12 import org.xmlpull.v1.XmlPullParserException;
13
14 /**
15 *
16 * @author Navy_iFanr
17 *
18 */
19 public class LinkerUtils {
20 private String URL;
21 private String NAMESPACE = "http://192.168.49.100:8088/";
22 private String SOAP_ACTION;
23 private String METHOD_NAME;
24 private SoapSerializationEnvelope envelope;
25 private SoapObject rpc;
26 private HttpTransportSE ht;
27 // private MyAndroidHttpTransport ht;
28 private List<HashMap<String, String>> listUserInfo;
29 private List<HashMap<String, String>> listPublish;
30 private List<HashMap<String, String>> listComment;
31
32 public LinkerUtils() {
33 "http://womenapp.stu.edu.cn/api/cjjs/WebService.asmx";
34 envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
35 envelope.dotNet = true;
36 ht = new HttpTransportSE(URL, 80000);
37 ht.debug = true;
38 }
39
40 /*
41 * 用户注册
42 */
43 public Object setUserRegister(String name, String password, String province, String city, String town,
44 String country, int state) throws IOException, XmlPullParserException {
45 METHOD_NAME = "insertNewUser";
46 // SOAP_ACTION = "http://192.168.13.199:8088/insertNewUser";
47 // SOAP_ACTION = "http://mt.stu.edu.cn:8088/insertNewUser";
48 SOAP_ACTION = "http://192.168.49.100:8088/insertNewUser";
49 rpc = new SoapObject(NAMESPACE, METHOD_NAME);
50 rpc.addProperty("uName", name);
51 rpc.addProperty("uPassword", password);
52 rpc.addProperty("uProvince", province);
53 rpc.addProperty("uCity", city);
54 rpc.addProperty("uTown", town);
55 rpc.addProperty("uCountry", country);
56 rpc.addProperty("uState", state);
57 envelope.bodyOut = rpc;
58 envelope.setOutputSoapObject(rpc);
59 ht.call(SOAP_ACTION, envelope);
60 return (Object) envelope.getResponse(); // Boolean&boolean,唯一只能使用Boolean上的就是从列表或者哈希表获取值时
61 }
62
63
64 /*
65 * 获取用户状态信息
66 */
67 public List<HashMap<String, String>> getUserInfo() throws IOException, XmlPullParserException {
68 METHOD_NAME = "selectUserState";
69 // SOAP_ACTION = "http://192.168.13.199:8088/selectUserState";
70 // SOAP_ACTION = "http://mt.stu.edu.cn:8088/selectUserState";
71 SOAP_ACTION = "http://192.168.49.100:8088/selectUserState";
72 rpc = new SoapObject(NAMESPACE, METHOD_NAME);
73
74 envelope.bodyOut = rpc;
75 envelope.setOutputSoapObject(rpc);
76 ht.call(SOAP_ACTION, envelope);
77 SoapObject userInfo = (SoapObject) envelope.bodyIn;
78 userInfo = (SoapObject) envelope.getResponse();
79
80 listUserInfo = new ArrayList<HashMap<String, String>>();
81 for (int i = 0; i < ((SoapObject) userInfo).getPropertyCount(); i += 8) {
82 HashMap<String, String> hashMap = new HashMap<String, String>();
83
84 hashMap.put("uId", userInfo.getProperty(i).toString());
85 hashMap.put("uName", userInfo.getProperty(i + 1).toString());
86 hashMap.put("uPassword", userInfo.getProperty(i + 2).toString());
87 hashMap.put("uProvince", userInfo.getProperty(i + 3).toString());
88 hashMap.put("uCity", userInfo.getProperty(i + 4).toString());
89 hashMap.put("uTown", userInfo.getProperty(i + 5).toString());
90 hashMap.put("uCountry", userInfo.getProperty(i + 6).toString());
91 hashMap.put("uState", userInfo.getProperty(i + 7).toString());
92 listUserInfo.add(hashMap);
93 System.out.println("detail.getProperty(" + i + ")" + ((SoapObject) userInfo).getProperty(i));
94 }
95 return listUserInfo;
96 }
97
98
99 }
还有到webservice连接服务器的数据库的问题,主要是连接不上的问题:(provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server),遇到这问题详细的解决办法,这个网址都有了,就不多说了,解决SQL Server管理器无法连接远程数据库的问题:http://www.cnblogs.com/jaxu/archive/2010/11/23/1884411.html,如果还是解决不了,那就是服务器本身的问题了,侦听端口没设置好,或其他。
最后,就是webservice的发布问题,在发布前,最好在本机用VS调试好,确定没错后再发布,不然发布后就调试不了了,这样如果之后出错,也可以保证程序本身没错。发布时,配置好iis,如果是新安装的,记得一定要打开允许asp脚本的访问等。
还有,关于与webservice相关的应用,如果wifi能访问,3G网络访问不了,可能是你的服务器发布在内网(你的wifi也处于内网),所以要将发布webservice的端口映射到外网,才能使3G网络能够访问。
问题虽然不是很难,但一折腾,也学到很多东西。