码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发调用webservice方式之一

时间:2020-07-17 19:43:22      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:gets   init   method   deb   timeout   manage   style   ssim   http   

添加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方式之一

标签:gets   init   method   deb   timeout   manage   style   ssim   http   

原文地址:https://www.cnblogs.com/xnya/p/13332041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!