转--Android中调用webservice的工具类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了转--Android中调用webservice的工具类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3494字,纯文字阅读大概需要5分钟。
内容图文
![转--Android中调用webservice的工具类](/upload/InfoBanner/zyjiaocheng/1138/ecae91b8881b4c23a1ab463d7c5bd4ad.jpg)
最近学习WebService,感觉利用这个借口开发网站的Android客户端方便及了,用到一个工具类,这里铭记一下。
public static final String WebServiceNamespace =""//地址 public static final String WebAddress = ""//地址
调用Webservice
public static Object callWebservice(String WebServiceUrl,String method,String[] params,Object[] values) { Object result = null; SoapObject rpc = new SoapObject(WebServiceTool.WebServiceNamespace,method); if(params!=null) { for(int i=0;i<params.length;i++) rpc.addProperty(params[i], values[i]); } SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc); HttpTransportSE ht = new HttpTransportSE(WebServiceUrl); ht.debug = true; String SOAP_ACTION = WebServiceTool.WebServiceNamespace + method; try { ht.call(SOAP_ACTION, envelope); result = envelope.getResponse(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return result; }
将WebService调用获得的对象转换成对象
![技术分享](/upload/getfiles/default/2022/11/14/20221114112246945.jpg)
public static Object toObject(Object obj,Class<?> cls) { if(obj==null) return null; if(obj instanceof String) return obj; Object result = null ; if(!(obj instanceof SoapObject)) return null; try { result = cls.newInstance() ; SoapObject so = (SoapObject)obj; System.out.println(so.getNamespace()); for(int i=0;i<so.getPropertyCount();i++) { PropertyInfo pinfo = new PropertyInfo(); so.getPropertyInfo(i, pinfo); System.out.println(pinfo.name); Object value = so.getProperty(i); if(value==null) continue; Object returnValue = value; Field field = null; try { field = cls.getField(pinfo.name); } catch(NoSuchFieldException e) { continue; } String name = field.getType().getName(); System.out.println(name); if(name.equals("int")) returnValue = Integer.valueOf(returnValue.toString()); else if(name.equals("short")) returnValue = Short.valueOf(value.toString()); else if(name.equals("long")) returnValue = Long.valueOf(value.toString()); else if(name.equals("byte")) returnValue = Byte.valueOf(value.toString()); else if(name.equals("float")) returnValue = Float.valueOf(value.toString()); else if(name.equals("double")) returnValue = Double.valueOf(value.toString()); else if(name.equals("BigInteger")) returnValue = new BigInteger(value.toString()); else if(name.equals("boolean")) returnValue = Boolean.valueOf(value.toString()); else if(name.equals("char")) returnValue = value.toString().charAt(0); else if(name.equals("java.util.Date")) returnValue = Date.parse(value.toString()); else if(name.equals("java.lang.String")) returnValue = value.toString(); cls.getField(pinfo.name).set(result,returnValue); } } catch (Exception e) { e.printStackTrace(); } return result; }
将WebService调用获得的对象转换成对象数组
public static Object[] toObjects(Object obj,Class<?> cls) { if(obj==null) return null; if(!(obj instanceof SoapObject)) return null; SoapObject so = (SoapObject)obj; int count = so.getPropertyCount(); Object[] objs = new Object[count]; for(int i=0;i<count;i++) { objs[i] = toObject(so.getProperty(i),cls); } return objs; }
public static ArrayList<Object> toObjectList(Object obj,Class<?> cls) { if(obj==null) return null; if(!(obj instanceof SoapObject)) return null; SoapObject so = (SoapObject)obj; int count = so.getPropertyCount(); ArrayList<Object> objs = new ArrayList<Object>(); for(int i=0;i<count;i++) { objs.add(toObject(so.getProperty(i),cls)); } return objs; }
原文:http://www.cnblogs.com/awkflf11/p/4370530.html
内容总结
以上是互联网集市为您收集整理的转--Android中调用webservice的工具类全部内容,希望文章能够帮你解决转--Android中调用webservice的工具类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。