最近在用Android开发小程序,里面用到了开源的Ksoap2包,可以调用Webservice实现与服务器通信。服务器端的Webservice使用Asp.Net开发。在调用Webservice的时候发现了一个问题,如果Webservice带有参数,那么Android端调用方法的时候参数传递失败,即Android向服务端传递了参数,但是Webservice没有接收到,所有参数值都为空。最后发现主要问题出现在envelope.dotNet 这个参数,要设置为false。只返回简单值的调用代码如下:
/**
*
*/
package com.utils.web;
*
*/
package com.utils.web;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Iterator;
import javax.security.auth.PrivateCredentialPermission;
import android.R.bool;
import android.R.string;
import android.R.string;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
/**
* @author Peng
*
*/
public final class WebServiceOP
{
private String serviceURL;
private String serviceNamespace;
public WebServiceOP()
{
serviceURL="http://www.test.net/webservice/test.asmx";//service地址,带asmx
serviceNamespace="http://www.test.net/";//命名空间,与服务端的一致(注意以'/'结尾)
}
/**
* 带参数的构造函数
* @param url webservice的地址(不包含?WDSL)
* @param namespace webservie方法的命名空间
*/
public WebServiceOP(String url,String namespace)
{
serviceURL =url;
serviceNamespace =namespace;
}
* @author Peng
*
*/
public final class WebServiceOP
{
private String serviceURL;
private String serviceNamespace;
public WebServiceOP()
{
serviceURL="http://www.test.net/webservice/test.asmx";//service地址,带asmx
serviceNamespace="http://www.test.net/";//命名空间,与服务端的一致(注意以'/'结尾)
}
/**
* 带参数的构造函数
* @param url webservice的地址(不包含?WDSL)
* @param namespace webservie方法的命名空间
*/
public WebServiceOP(String url,String namespace)
{
serviceURL =url;
serviceNamespace =namespace;
}
/**
* 调用webservice,得到返回的xml数据
* @param methodName 要调用的方法
* @param params 给方法提供的参数列表,包含在一个HashMap里
* @return 返回的xml字符串
*/
public String ServiceMethod(String methodName,HashMap<String, String> params)
{
String resultStr = "";
//根据命名空间和方法名称建立SoapObject;
SoapObject soapObj = new SoapObject(serviceNamespace, methodName);
//通过addProperty()方法添加方法的参数
Iterator<String> iterator=params.keySet().iterator();
while(iterator.hasNext())
{
String paramName=(String)iterator.next();
String ParamValue=params.get(paramName).toString();
soapObj.addProperty(paramName, ParamValue);
}
//生成soap请求信息
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//VER版本很重要,11指的是按Ksoap1.1版本格式返回结果
envelope.setOutputSoapObject(soapObj);
envelope.dotNet = false;//C#写的webservice必须加上这一句
envelope.setAddAdornments(false);
envelope.implicitTypes=true;
//开始调用服务
HttpTransportSE httpTransportSE = new HttpTransportSE(serviceURL);
try
{
httpTransportSE.call(serviceNamespace+methodName,envelope);
SoapObject obj=(SoapObject)envelope.bodyIn;
if(obj!=null)
{
resultStr =obj.getProperty(0).toString();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return resultStr;
}
}
* 调用webservice,得到返回的xml数据
* @param methodName 要调用的方法
* @param params 给方法提供的参数列表,包含在一个HashMap里
* @return 返回的xml字符串
*/
public String ServiceMethod(String methodName,HashMap<String, String> params)
{
String resultStr = "";
//根据命名空间和方法名称建立SoapObject;
SoapObject soapObj = new SoapObject(serviceNamespace, methodName);
//通过addProperty()方法添加方法的参数
Iterator<String> iterator=params.keySet().iterator();
while(iterator.hasNext())
{
String paramName=(String)iterator.next();
String ParamValue=params.get(paramName).toString();
soapObj.addProperty(paramName, ParamValue);
}
//生成soap请求信息
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//VER版本很重要,11指的是按Ksoap1.1版本格式返回结果
envelope.setOutputSoapObject(soapObj);
envelope.dotNet = false;//C#写的webservice必须加上这一句
envelope.setAddAdornments(false);
envelope.implicitTypes=true;
//开始调用服务
HttpTransportSE httpTransportSE = new HttpTransportSE(serviceURL);
try
{
httpTransportSE.call(serviceNamespace+methodName,envelope);
SoapObject obj=(SoapObject)envelope.bodyIn;
if(obj!=null)
{
resultStr =obj.getProperty(0).toString();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return resultStr;
}
}