Android中調用webservice的工具類

來源:互聯網
上載者:User

最近學習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調用獲得的對象轉換成對象

View Code

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;        }    

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.