Android开发调用webservice方式之一

  • A+
所属分类:Android开发

添加ksoap2-android-assembly-3.6.3-jar-with-dependencies.jar包
创建KsoapHelper类

  1. public class KsoapHelper
  2. {
  3.     public static int timeOut = 30000;
  4.     public static String webServiceUrl;
  5.     public static String nameSpace;
  6.     public static int soapVersion = 120;
  7.     private KsoapHelper()
  8.     {}
  9.     public static void initKsoapHelper(String _webServiceUrl, String _nameSpace)
  10.     {
  11.         initKsoapHelper(timeOut, _webServiceUrl, _nameSpace, soapVersion);
  12.     }
  13.     public static void initKsoapHelper(String _webServiceUrl, String _nameSpace, int _version)
  14.     {
  15.         initKsoapHelper(timeOut, _webServiceUrl, _nameSpace, _version);
  16.     }
  17.     public static void initKsoapHelper(int _timeOut, String _webServiceUrl, String _nameSpace, int _version)
  18.     {
  19.         nameSpace = _nameSpace;
  20.         webServiceUrl = _webServiceUrl;
  21.         timeOut = _timeOut;
  22.         soapVersion = _version;
  23.     }
  24.     public static SoapObject GetSoapObject(String methodName)
  25.     {
  26.         SoapObject object = new SoapObject(nameSpace, methodName);
  27.         return object;
  28.     }
  29.     public static SoapObject GetSoapObject(String nameSpace, String methodName)
  30.     {
  31.         SoapObject object = new SoapObject(nameSpace, methodName);
  32.         return object;
  33.     }
  34.     public static Object GetResult(SoapObject object, boolean isSimple) throws IOException, XmlPullParserException
  35.     {
  36.         SoapSerializationEnvelope sSEnvelope = new SoapSerializationEnvelope(120);
  37.         sSEnvelope.bodyOut = object;
  38.         sSEnvelope.dotNet = true;
  39.         HttpTransportSE httpTransSE = new HttpTransportSE(webServiceUrl, timeOut);
  40.         httpTransSE.debug = true;
  41.         httpTransSE.call((String) null, sSEnvelope);
  42.         SoapObject result;
  43.         if(isSimple)
  44.         {
  45.             result = null;
  46.             Object simpleResult = sSEnvelope.getResponse();
  47.             return simpleResult.toString();
  48.         }
  49.         else
  50.         {
  51.             result = null;
  52.             result = (SoapObject) sSEnvelope.getResponse();
  53.             SoapObject childs = (SoapObject) result.getProperty(1);
  54.             return(SoapObject) childs.getProperty(0);
  55.         }
  56.     }
  57. }

 

 

编写MainActivity类,橙色部分根据自己情况更改

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener
  2. {
  3.     private String WEBSERVICE_URL;
  4.     private String NAMESPACE;@
  5.     Override protected void onCreate(Bundle savedInstanceState)
  6.     {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.activity_main);
  9.         initView();
  10.     } /** * 初始化 */
  11.     private void initView()
  12.     {
  13.         WEBSERVICE_URL = "http://192.168.200.185:8090/UserInfoManagement.asmx?wsdl";
  14.         NAMESPACE = "http://tempuri.org/";
  15.     }@
  16.     Override public void onClick(View view)
  17.     {
  18.         switch(view.getId())
  19.         {
  20.             case R.id.btn_enter:
  21.                 Login();
  22.                 break;
  23.         }
  24.     }
  25.     public void Login()
  26.     {
  27.         KsoapHelper.initKsoapHelper(WEBSERVICE_URL, NAMESPACE);
  28.         SoapObject request = KsoapHelper.GetSoapObject("UserCheck");
  29.         request.addProperty("UserID", et_name.getText().toString());
  30.         request.addProperty("Password", et_pwd.getText().toString());
  31.         try
  32.         {
  33.             String sCheckuser = (String) KsoapHelper.GetResult(request, true);
  34.         }
  35.         catch(Exception
  36.             var)
  37.         {
  38.             return;
  39.         }
  40.     }
  41. }

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: