当前位置:首页 > 芯闻号 > 充电吧
[导读]Android中调用Web Services有很多方法,我们现在使用的是ksoap,它是SOAP web services的客户端包,ksoap现在版本为2.0.它的一个主要优点就是对dotNET

Android中调用Web Services有很多方法,我们现在使用的是ksoap,它是SOAP web services的客户端包,ksoap现在版本为2.0.它的一个主要优点就是对dotNET兼容性比较不错。

首先下载ksoap的包文件,在Eclispe的Package Explorer中右键项目,Build Path>Add Libraries,找到ksoap2-android-assembly-2.4-jar-with-dependencies.jar添加该引用。代码如下:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33public class WSHelper {    finalstatic String WSUrl="http://xxx/WSUrl.asmx";      privatestatic String namespace ="http://tempuri.org/";    /*************************************     * 获取web services内容     * @param url     * @param params     * @return     *************************************/    publicstatic String GetResponse(String method,List                  try{            String url = WSUrl;            SoapObject request =new SoapObject(namespace, method);            for(inti=0,len=params.size();i<len;i++){                request.addProperty(params.get(i).getName(), params.get(i).getValue());            }            SoapSerializationEnvelope envelope =                newSoapSerializationEnvelope(SoapEnvelope.VER11);             envelope.dotNet =true;             envelope.setOutputSoapObject(request);                          AndroidHttpTransport androidHttpTransport =new AndroidHttpTransport(url);            androidHttpTransport.call(namespace + method, envelope);                          SoapPrimitive result = (SoapPrimitive)envelope.getResponse();            returnresult.toString();        }catch (Exception e) {            return"Error:calling the web services error";        }    }}


调用时代码如下:

? 1 2 3 4String method = "MethodName";//方法名称Listnew ArrayListparams.add(newBasicNameValuePair("userId", String.valueOf(1995)));return WSHelper.GetResponse(method,params);


这里参数可以定义多个,返回时是以String类似来返回。

注意由于我们调用Web sevices,就需要程序有访问网络的权限,因此需要在AndroidManifest.xml中manifest节中增加访问网络权限的定义

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭