Example Android webservice authentication using user's login

           Some time ago made a two Android aspects of the project, and now want to find time for some knowledge points which were summarized under, can be considered for further study.

           As the development of mobile client and server are generally required to deal with, so the logged-on user authentication in general applications have missed. I will do so before the project in the use of this verification webservice

Write out alone. Server-side applications using our phones are Asp.net development, and thus also the webservice development with C # development, published on IIS.

           Calling WebService does not provide libraries in the Android SDK, therefore, we need to use third-party SDK to call WebService. PC version of the WebService library is very rich, but they are too large for Android. WebService client for mobile phones are some of the SDK, more commonly used is KSOAP2.

KSOAP2 Address: http://code.google.com/p/ksoap2-android/

We used in the project are: ksoap2-android-assembly-2.4-jar-with-dependencies.jar.

          After the project references KSOAP2, we have introduced several packages of the following:

org.ksoap2.SoapEnvelope Import;
Import org.ksoap2.serialization.SoapObject;
Import org.ksoap2.serialization.SoapSerializationEnvelope;
Import org.ksoap2.transport.HttpTransportSE;

then we will call the write method of verifying a user logs in, and call the webservice method, the code is as follows:

String GetUserWS public (String methodName, String [] parameterList) {
        // create SoapObject object and specify the namespace and call WebService method name
        SoapObject request = new SoapObject (Config.NAMESPACE, methodName);
        function // call if there is parameter, where you can set parameters need to pass Note: the first parameter using a plurality of parameters and so arg0 arg1, arg2 ...
        IF (parameterList = null!) {
            // for (int I = 0; I <parameterList.length ; I ++) {
            request.addProperty ( "Key", parameterList [0]);
            request.addProperty ( "the userName", parameterList [. 1]);
            request.addProperty ( "passWord,", parameterList [2]);

            //}
        }

        // call WebService method of generating a SOAP request message, and specify the SOAP version
        = New new envelope SoapSerializationEnvelope SoapSerializationEnvelope (
                SoapEnvelope.VER11);
        // envelope.setOutputSoapObject (Request);
        // equivalent to an upper side to a lower side of the phrase envelope SoapObject object is assigned an object
        envelope.bodyOut = Request;
        // current development it should not be .net WS WS call the Java
        envelope.dotNet = to true;

        / *
         * Do not use here ht = new new AndroidHttpTransport AndroidHttpTransport (the URL of);
         * this is a class to expire
         * create HttpTransportSE object. WebService WSDL document can be specified by the class constructor HttpTransportSE the URL of
         * /

         //这里的SOAP_GETUSERINFOACTION = "http://172.16.xx.xxx:3366/Service/EWineService.asmx?op=Mobile_GetUserInfo";
        HttpTransportSE ht = new HttpTransportSE(Config.SOAP_GETUSERINFOACTION);

        try {
            // 请求WS
            ht.call(Config.SOAP_ACTION, envelope);
            if (envelope.getResponse() != null) {
                // 获得WS函数返回值信息
                // System.out.println(envelope.getResponse().toString());
                Log.d("wine", "GetUserWS Result:"
                        + envelope.getResponse().toString());
                return envelope.getResponse().toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            Log.d("wine", "GetUserWS Error:" + e.getMessage());
        }
        return null;
    }


Specific call code is as follows:

                Eastern Point // OK button executed
                String [] = parameterList new new String [. 3];
                parameterList [0] = LOGINKEY;
                parameterList [. 1] = TxtUser.getText () toString ();.
                ParameterList [2] = txtPassword. getText () toString ().;

                // Note Config.METHOD_GETUSERINFO is the name of the method webservice specific calls, such as: METHOD_GETUSERINFO = "Mobile_GetUserInfo";
                // call webService
                String strRemoteInfo = GetUserWS (Config.METHOD_GETUSERINFO,
                        parameterList);





Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2012/04/08/2482122.html

Guess you like

Origin blog.csdn.net/weixin_33832340/article/details/93306913