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

你可能感兴趣的:(Android开发调用webservice方式之一)