简体   繁体   中英

Android Login & Registration using Web service or ASP.NET denying a passed strings with KSoap2

I'm creating an Android application that will communicates between android and ASP.NET by using SOAP web service, and I'm using KSoap2 to do it. And I am making is to register an account, which takes some user information, along with a " registration code " and then i use connect web service as used to connect to web-service to ASP .

My ASP.NET IIS is working properly.

Ho to know that values are passing on web-services. That possible to send value on .net and insert on sql server on android.

registration code

public class Register extends Activity {
EditText edit_username;
EditText edit_displayName;
EditText edit_email;
EditText edit_password;
EditText edit_confirmPassword;
Button btn_register, btn_login;
Button btn_reset;
String user;
String disp, pass, email, conpass;
boolean RegisterStatus;
static boolean errored = false;
ProgressBar webservicePG;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    edit_username = (EditText) findViewById(R.id.edit_username);
    edit_displayName = (EditText) findViewById(R.id.edit_name);
    edit_email = (EditText) findViewById(R.id.edit_email);
    edit_password = (EditText) findViewById(R.id.edit_password);
    edit_confirmPassword = (EditText) findViewById(R.id.edit_confirmPassword);
    btn_register = (Button) findViewById(R.id.btn_register);
    btn_login = (Button) findViewById(R.id.reg_back_btn);
    btn_reset = (Button) findViewById(R.id.btn_reset);

    btn_reset.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            edit_username.setText("");
            edit_displayName.setText("");
            edit_email.setText("");
            edit_password.setText("");
            edit_confirmPassword.setText("");
        }
    });
    btn_login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(getApplicationContext(), Login.class);
            startActivity(i);
        }
    });
    btn_register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (edit_username.getText().length() != 0
                    && edit_username.getText().toString() != "") {
                if (edit_displayName.getText().length() != 0
                        && edit_displayName.getText().toString() != "") {

                    if (edit_email.getText().length() != 0
                            && edit_email.getText().toString() != "") {

                        if (edit_password.getText().length() != 0
                                && edit_password.getText().toString() != "") {

                            if (edit_confirmPassword.getText().length() != 0
                                    && edit_confirmPassword.getText()
                                            .toString() != "") {

                                user = edit_username.getText().toString();
                                disp = edit_displayName.getText()
                                        .toString();
                                pass = edit_password.getText().toString();
                                email = edit_email.getText().toString();
                                conpass = edit_confirmPassword.getText()
                                        .toString();
                                // statusTV.setText("");
                                // Create instance for AsyncCallWS
                                AsyncCallWS task = new AsyncCallWS();
                                // Call execute
                                task.execute();
                            }
                            // If Password text control is empty
                            else {
                                // statusTV.setText("Please enter Password");
                                Toast.makeText(getApplicationContext(),
                                        "Please enter Conform Password",
                                        Toast.LENGTH_LONG).show();
                                edit_confirmPassword
                                        .setError("Cannot Be Empty");
                            }
                            // If Username text control is empty

                        } else {
                            // statusTV.setText("Please enter Username");
                            Toast.makeText(getApplicationContext(),
                                    "Please enter Password",
                                    Toast.LENGTH_LONG).show();
                            edit_password.setError("Cannot Be Empty");
                        }
                    } else {
                        // statusTV.setText("Please enter Username");
                        Toast.makeText(getApplicationContext(),
                                "Please enter Email Address",
                                Toast.LENGTH_LONG).show();
                        edit_email.setError("Cannot Be Empty");
                    }
                } else {
                    // statusTV.setText("Please enter Username");
                    Toast.makeText(getApplicationContext(),
                            "Please enter Dispalayname", Toast.LENGTH_LONG)
                            .show();
                    edit_displayName.setError("Cannot Be Empty");
                }
            } else {
                // statusTV.setText("Please enter Username");
                Toast.makeText(getApplicationContext(),
                        "Please enter Username", Toast.LENGTH_LONG).show();
                edit_username.setError("Cannot Be Empty");
            }

        }
    });
}

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        // Call Web Method
        RegisterStatus = WebService.invokeLoginWS(user, disp, email, pass,
                conpass, "AuthenticateUser");
        return null;
    }

    @Override
    // Once WebService returns response
    protected void onPostExecute(Void result) {
        // Make Progress Bar invisible
        webservicePG.setVisibility(View.INVISIBLE);
        Intent intObj = new Intent(Register.this, Login.class);
        // Error status is false
        if (!errored) {
            // Based on Boolean value returned from WebService
            if (RegisterStatus) {
                // Navigate to Home Screen
                startActivity(intObj);
            } else {
                // Set Error message
                Toast.makeText(getApplicationContext(),
                        "Register Failed, try again", Toast.LENGTH_LONG)
                        .show();
            }
            // Error status is true
        } else {
            // /statusTV.setText("Error occured in invoking webservice");
            Toast.makeText(getApplicationContext(),
                    "Error occured in invoking webservice",
                    Toast.LENGTH_LONG).show();
        }
        // Re-initialize Error Status to False
        errored = false;
    }

    @Override
    // Make Progress Bar visible
    protected void onPreExecute() {
        webservicePG.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

  }
 }

web service code

public class WebService {
// Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = "http://tempuri.org/";
// Webservice URL - WSDL File location
private static String URL = "http://192.168.3.3/LoginCheck/Service.asmx";// Make
                                                                            // sure
                                                                            // you
                                                                            // changed
                                                                            // IP
                                                                            // address
// SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://tempuri.org/";

public static boolean invokeLoginWS(String userName, String dispalyname,
        String Email, String password, String conpassWord,
        String webMethName) {
    boolean loginStatus = false;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    // Property which holds input parameters
    PropertyInfo unamePI = new PropertyInfo();
    PropertyInfo passPI = new PropertyInfo();
    // Set Username
    unamePI.setName("username");
    // Set Value
    unamePI.setValue(userName);
    // Set dataType
    unamePI.setType(String.class);
    // Add the property to request object
    request.addProperty(unamePI);
    passPI.setName("dispalyname");
    // Set dataType
    passPI.setValue(dispalyname);
    // Set dataType
    passPI.setType(String.class);// Set Password
    passPI.setName("Email");
    // Set dataType
    passPI.setValue(Email);
    // Set dataType
    passPI.setType(String.class);
    // Add the property to request object
    request.addProperty(passPI);

    // Set Password
    passPI.setName("password");
    // Set dataType
    passPI.setValue(password);
    // Set dataType
    passPI.setType(String.class);
    // Add the property to request object
    request.addProperty(passPI);
    // Set dataType
    // Set Password
    passPI.setName("conpassWord");
    passPI.setValue(conpassWord);
    // Set dataType
    passPI.setType(String.class);
    // Add the property to request object
    request.addProperty(passPI);
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION + webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to boolean variable variable
        loginStatus = Boolean.parseBoolean(response.toString());

    } catch (Exception e) {
        // Assign Error Status true in static variable 'errored'
        Login.errored = true;
        e.printStackTrace();
    }
    // Return booleam to calling object
    return loginStatus;
 }
}

The requests sending from android to server can be listed in server log. If you are not getting any error while sending request, it maybe recieved by the sever. Otherwise check the internet connection and make sure to add the internet permission in the manifest, also refer the ksoap lib to java build path

Check your WSDL file first and check your NAMESPACE = "http://tempuri.org/" is correct or not ? check your SOAP_ACTION = "http://tempuri.org/" is correct or not ? In your code,comments are available before the declaration of NAMESPACE and SOAP_ACTION please follow the comments and WSDL file for your NAMESPACE and SOAP_ACTION.

SOAP Action = URI again Namespace + Web method name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM