当前位置 : 主页 > 手机开发 > android >

Android-----使用SoapObject获取服务器数据

来源:互联网 收集:自由互联 发布时间:2021-06-11
新建两个工具类ConnectWeb.java 和 ConnectMethod.java 进行对服务器进行数据交互 ConnectWeb.java代码如下: public class ConnectWeb { private static final String nameSpace = " http://gdhs.com/ " ; private String ip = nul

新建两个工具类ConnectWeb.java 和 ConnectMethod.java 进行对服务器进行数据交互

ConnectWeb.java代码如下:

public class ConnectWeb {

    private static final String nameSpace = "http://gdhs.com/";
    private String ip = null;
    private int timeout = 3000;
    private int numbercs = 0;
    SoapObject rpc = null;
    String methodName = null;

    public ConnectWeb(String methodName , String ip , int timeout , int numbercs){
        this.methodName = methodName;
        this.ip = " http://" + ip + "/" + "CYService" + ".asmx";
        this.timeout = timeout;
        this.numbercs = numbercs;
        rpc = new SoapObject(nameSpace,this.methodName);
    }

    public void addProperty(String name , String value){
        rpc.addProperty(name,value);
    }

    public SoapObject connectToService()throws Exception{
        try {
            HttpTransportSE hts = new HttpTransportSE(ip,timeout);
            hts.debug = true;
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.bodyOut = rpc;
            envelope.dotNet = true;
            envelope.setOutputSoapObject(rpc);
            hts.call(nameSpace+this.methodName,envelope);
            SoapObject result = (SoapObject)envelope.bodyIn;
            Log.i("AAA","ConnectWeb从数据库拿到的数据result==========>"+result);
            return result;
        }catch (Exception e){
            Log.i("AAA","ConnectWeb从数据库拿数据出错==========>"+e.getMessage());
            return null;
        }
    }

}

 ConnectMethod.java 代码如下:

public class ConnectMethod {

    public static String connectWebService(String ip , String methodName , String jkxlh , String jkid ,
                                           String xmlDoc , String ResultGs , int timeout , int cscs){
        try {
            ConnectWeb web = new ConnectWeb(methodName , ip , timeout , cscs);
            web.addProperty("xtlb" , "01");
            web.addProperty("jkxlh" , jkxlh);
            web.addProperty("jkid" , jkid);
            web.addProperty("xmlDoc" , xmlDoc);
            SoapObject result = web.connectToService();
            String dataOne = result.getProperty(ResultGs).toString();
            String dataTwo = URLDecoder.decode(dataOne , "UTF-8");
            return dataTwo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

调服务器接口获取数据:

 

 String result = ConnectMethod.connectWebService(ip , methodName , jkxlh , "01C07", xmlDoc ,queryResult , timeoutFive, timeoutThree); 

 

返回数据由服务器方法决定。

网友评论