Android 开发记录 webservice调用:ksoap2框架

1. 引入项目包:ksoap2-android-assembly-2.5.7.jar

 

2. java程序:

 

 

    protected ResultDO callWebservice(String webserviceUrl, SoapObject soapObject){
        //创建HttpTransportSE对象,通过HttpTransportSE类的构造方法可以指定WebService的url 
        HttpTransportSE transport = new HttpTransportSE(webserviceUrl, TIMEOUT);// 这里url为webservice地址
        
        //指定WebService的命名空间和函数名
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = transport;
        envelope.setOutputSoapObject(soapObject);

        //使用call方法调用WebService方法
        SoapObject sb = null;
        try {
            transport.call(null, envelope);
            sb = (SoapObject) envelope.bodyIn;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultDO(IConsts.RESULT_CODE_TIMEOUT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDO(IConsts.RESULT_CODE_CALL_ERROR);
        }

//        Object sb= envelope.getResponse();
        String jsonString = null;
        if(sb.getPropertyCount() > 0){
            jsonString = (String)sb.getProperty(0).toString();// 获取从服务器端返回的json字符串
        }else{
            return new ResultDO(IConsts.RESULT_CODE_RESOLVE_ERROR);
        }
        return createFromJSONString(jsonString);
    }
    public ResultDO getData(){
        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD);// namespace 和 method参数
        soapObject.addProperty(param, param);
        super.callWebservice(ENDPOINT, soapObject);// endpoint 为webservice调用地址,以?wsdl结尾
        
        return null;
    }
 

 

你可能感兴趣的:(webservice)