由于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的部分代码:
package cn.edu.stu.VillageMarket.utils; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException; /**
*
* @author Navy_iFanr
*
*/
public class LinkerUtils {
private String URL;
private String NAMESPACE = "http://192.168.49.100:8088/";
private String SOAP_ACTION;
private String METHOD_NAME;
private SoapSerializationEnvelope envelope;
private SoapObject rpc;
private HttpTransportSE ht;
// private MyAndroidHttpTransport ht;
private List<HashMap<String, String>> listUserInfo;
private List<HashMap<String, String>> listPublish;
private List<HashMap<String, String>> listComment; public LinkerUtils() {
"http://womenapp.stu.edu.cn/api/cjjs/WebService.asmx";
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
ht = new HttpTransportSE(URL, 80000);
ht.debug = true;
} /*
* 用户注册
*/
public Object setUserRegister(String name, String password, String province, String city, String town,
String country, int state) throws IOException, XmlPullParserException {
METHOD_NAME = "insertNewUser";
// SOAP_ACTION = "http://192.168.13.199:8088/insertNewUser";
// SOAP_ACTION = "http://mt.stu.edu.cn:8088/insertNewUser";
SOAP_ACTION = "http://192.168.49.100:8088/insertNewUser";
rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty("uName", name);
rpc.addProperty("uPassword", password);
rpc.addProperty("uProvince", province);
rpc.addProperty("uCity", city);
rpc.addProperty("uTown", town);
rpc.addProperty("uCountry", country);
rpc.addProperty("uState", state);
envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
ht.call(SOAP_ACTION, envelope);
return (Object) envelope.getResponse(); // Boolean&boolean,唯一只能使用Boolean上的就是从列表或者哈希表获取值时
} /*
* 获取用户状态信息
*/
public List<HashMap<String, String>> getUserInfo() throws IOException, XmlPullParserException {
METHOD_NAME = "selectUserState";
// SOAP_ACTION = "http://192.168.13.199:8088/selectUserState";
// SOAP_ACTION = "http://mt.stu.edu.cn:8088/selectUserState";
SOAP_ACTION = "http://192.168.49.100:8088/selectUserState";
rpc = new SoapObject(NAMESPACE, METHOD_NAME); envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
ht.call(SOAP_ACTION, envelope);
SoapObject userInfo = (SoapObject) envelope.bodyIn;
userInfo = (SoapObject) envelope.getResponse(); listUserInfo = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < ((SoapObject) userInfo).getPropertyCount(); i += 8) {
HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("uId", userInfo.getProperty(i).toString());
hashMap.put("uName", userInfo.getProperty(i + 1).toString());
hashMap.put("uPassword", userInfo.getProperty(i + 2).toString());
hashMap.put("uProvince", userInfo.getProperty(i + 3).toString());
hashMap.put("uCity", userInfo.getProperty(i + 4).toString());
hashMap.put("uTown", userInfo.getProperty(i + 5).toString());
hashMap.put("uCountry", userInfo.getProperty(i + 6).toString());
hashMap.put("uState", userInfo.getProperty(i + 7).toString());
listUserInfo.add(hashMap);
System.out.println("detail.getProperty(" + i + ")" + ((SoapObject) userInfo).getProperty(i));
}
return listUserInfo;
} }
还有到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网络能够访问。
问题虽然不是很难,但一折腾,也学到很多东西。