One of the ways to call web service in Android Development

Time:2020-11-13
Add ksoap2-android-assembly-3.6.3-jar-with- dependencies.jar package
Create ksoahelper class
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);        }    }}
Write mainactivity class, change the orange part according to your own situation
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();} / * * * initialization * / 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 ;        }    }}