我正在尝试从 Android 应用程序调用 asmx Web 服务。今天刚刚开始了一些Android开发。我一直在关注我在网上和这里找到的各种解决方案,这似乎比预期的要困难。我尝试了各种解决方案,使用 KSoap2 似乎是实现这一点的最简单方法,如果我能让它工作的话。
我有以下代码一直有效:
private class CallWebService extends AsyncTask<Void, Void, Void> {
private static final String SOAP_ACTION = "http://tempuri.org/GetUser";
private static final String METHOD_NAME = "GetUser";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://160.10.1.79:59315/Service1.asmx";
TextView tv;
@Override
protected Void doInBackground(Void... params) {
tv=(TextView)findViewById(R.id.txtMessage);
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
tv.setText(result.toString());
}
catch (Exception e) {
tv.setText(e.getMessage());
}
return null;
}
}
它似乎挂在网上androidHttpTransport.call(SOAP_ACTION, envelope);
任何想法为什么?这是正确的方法吗?我应该转向另一个方向吗?