我最近在过一个项目,是android客户端和webservice连接的问题,在webservice写了段代码public String[] select(String id) throws Exception{
String a[]=new String[10]; int i=0;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName= rs";
String user="sa";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql="select * from t where x<20 and y<=20";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
a[i]=rs.getString(1)+rs.getString(2);
i=i+1;
}
return a;
}
android的里面用soap2来获得数据代码如下
String URN = "http://ws.apache.org/axis2";
String address = "http://192.168.1.104:8080/axis2/services/z";
String methodName = "select";
try{
SoapObject rpc = new SoapObject(URN, methodName);
rpc.addProperty("args0","10");//向webservice里面传入参数,实际上没有用到
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut=rpc;
envelope.dotNet = false;
AndroidHttpTransport ht = new AndroidHttpTransport(address);
ht.call("", envelope);
String q= (envelope.getResponse()).toString();
mButton.setText(q);
首先申明这段代码是没有问题的,确实可以获得数据,但是数据少了。实际应该返回2条记录,在浏览器里面测试确实是2条数据,为毛到了android客户端少了一条数据了?
浏览器测试结果:
2 jk 3 gh
手机客户端获得结果
2 jk
请教达人,在线等待
String a[]=new String[10]; int i=0;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName= rs";
String user="sa";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql="select * from t where x<20 and y<=20";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
a[i]=rs.getString(1)+rs.getString(2);
i=i+1;
}
return a;
}
android的里面用soap2来获得数据代码如下
String URN = "http://ws.apache.org/axis2";
String address = "http://192.168.1.104:8080/axis2/services/z";
String methodName = "select";
try{
SoapObject rpc = new SoapObject(URN, methodName);
rpc.addProperty("args0","10");//向webservice里面传入参数,实际上没有用到
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut=rpc;
envelope.dotNet = false;
AndroidHttpTransport ht = new AndroidHttpTransport(address);
ht.call("", envelope);
String q= (envelope.getResponse()).toString();
mButton.setText(q);
首先申明这段代码是没有问题的,确实可以获得数据,但是数据少了。实际应该返回2条记录,在浏览器里面测试确实是2条数据,为毛到了android客户端少了一条数据了?
浏览器测试结果:
2 jk 3 gh
手机客户端获得结果
2 jk
请教达人,在线等待
好像envelope.getResponse()).toString()不能对数组处理吧!
改为"SoapObject res = (SoapObject) envepole.bodyIn;"
"String q=res_s.toString()"