zoukankan      html  css  js  c++  java
  • Android开发调用webservice方式之一

    添加ksoap2-android-assembly-3.6.3-jar-with-dependencies.jar包
    创建KsoapHelper类 
    public class KsoapHelper {
    public static int timeOut = 30000;
    public static String webServiceUrl;
    public static String nameSpace;
    public static int soapVersion = 120;

    private KsoapHelper() {
    }

    public static void initKsoapHelper(String _webServiceUrl, String _nameSpace) {
    initKsoapHelper(timeOut, _webServiceUrl, _nameSpace, soapVersion);
    }

    public static void initKsoapHelper(String _webServiceUrl, String _nameSpace, int _version) {
    initKsoapHelper(timeOut, _webServiceUrl, _nameSpace, _version);
    }

    public static void initKsoapHelper(int _timeOut, String _webServiceUrl, String _nameSpace, int _version) {
    nameSpace = _nameSpace;
    webServiceUrl = _webServiceUrl;
    timeOut = _timeOut;
    soapVersion = _version;
    }

    public static SoapObject GetSoapObject(String methodName) {
    SoapObject object = new SoapObject(nameSpace, methodName);
    return object;
    }

    public static SoapObject GetSoapObject(String nameSpace, String methodName) {
    SoapObject object = new SoapObject(nameSpace, methodName);
    return object;
    }

    public static Object GetResult(SoapObject object, boolean isSimple) throws IOException, XmlPullParserException {
    SoapSerializationEnvelope sSEnvelope = new SoapSerializationEnvelope(120);
    sSEnvelope.bodyOut = object;
    sSEnvelope.dotNet = true;
    HttpTransportSE httpTransSE = new HttpTransportSE(webServiceUrl, timeOut);
    httpTransSE.debug = true;
    httpTransSE.call((String)null, sSEnvelope);
    SoapObject result;
    if (isSimple) {
    result = null;
    Object simpleResult = sSEnvelope.getResponse();
    return simpleResult.toString();
    } else {
    result = null;
    result = (SoapObject)sSEnvelope.getResponse();
    SoapObject childs = (SoapObject)result.getProperty(1);
    return (SoapObject)childs.getProperty(0);
    }
    }
    }


    编写MainActivity类,橙色部分根据自己情况更改
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private String WEBSERVICE_URL;
    private String NAMESPACE;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    }
    /**
    * 初始化
    */
    private void initView() {
    WEBSERVICE_URL = "http://192.168.200.185:8090/UserInfoManagement.asmx?wsdl";
    NAMESPACE = "http://tempuri.org/";
    }

    @Override
    public void onClick(View view) {
    switch (view.getId()){
    case R.id.btn_enter:
    Login();
    break;
    }
    }
       public void Login(){
    KsoapHelper.initKsoapHelper(WEBSERVICE_URL, NAMESPACE);
    SoapObject request = KsoapHelper.GetSoapObject("UserCheck");
    request.addProperty("UserID", et_name.getText().toString());
    request.addProperty("Password", et_pwd.getText().toString());
    try {
    String sCheckuser=(String) KsoapHelper.GetResult(request, true);
    } catch (Exception var) {
    return ;
    }
    }
    }
  • 相关阅读:
    SpringBoot学习笔记(14)----应用监控-HTTP方式
    SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群
    SpringBoot学习笔记(12)----SpringBoot实现多个 账号轮询发送邮件
    SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
    SpringBoot学习笔记(8)-----SpringBoot文件上传
    SpringBoot学习笔记(7)-----CORS支持解决跨域问题
    设计模式:迭代器模式(Iterator)
    设计模式:适配器模式(Adapter)
    设计模式:状态模式(State)
    设计模式:抽象工厂模式(Abstract Factory)
  • 原文地址:https://www.cnblogs.com/xnya/p/13332041.html
Copyright © 2011-2022 走看看