在android中检索属于username的电子邮件

时间:2012-12-27 09:39:58

标签: java android mysql web-services soap

我必须开发一个登录表单android应用程序。在这里,我必须输入用户名和密码。如果正确,请获取属于用户名的电子邮件并在textview中显示,如果用户名和密码错误,则显示登录失败消息。

这是我的网络服务代码:

public class Login { 
    public String authentication(String username,String password) {
        String retrievedUserName = "";
        String retrievedPassword = "";
        String retrievedEmail = "";
        String status = "";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xcart-432pro","root","");
            PreparedStatement statement =  con.prepareStatement("SELECT * FROM xcart_customers WHERE login = '"+username+"'");
            ResultSet result = statement.executeQuery();
            while(result.next()){  
                retrievedUserName = result.getString("login");
                retrievedPassword = result.getString("password");
                retrievedEmail = result.getString("email");  
            }
            if(retrievedUserName.equals(username)&&retrievedPassword.equals(password)&&!(retrievedUserName.equals("") && retrievedPassword.equals(""))){
                status = "Success";
            }
            else {
                status = "Login fail!!!";
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return status;
    }
};

这是我的android代码:

public class CustomerLogin extends Activity {
    private static final String SPF_NAME = "vidslogin";
    private static final String USERNAME = "login";
    private static final String PASSWORD = "password";
    private static final String PREFS_NAME = null;

    CheckBox chkRememberMe;
    private String login;
    String mGrandTotal,total,mTitle;

    EditText username,userPassword;
    private final String NAMESPACE = "http://xcart.com";
    private final String URL = "http://10.0.0.75:8085/XcartLogin/services/Login?wsdl";
    private final String SOAP_ACTION = "http://xcart.com/authentication";
    private final String METHOD_NAME = "authentication";
    private String uName;

    /**Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customer_login);
        Bundle b = getIntent().getExtras();
        total = b.getString("GrandTotal");
        mTitle = b.getString("Title");
        TextView grandtotal = (TextView) findViewById(R.id.grand_total);
        grandtotal.setText("Welcome ," + mTitle );
        chkRememberMe = (CheckBox) findViewById(R.id.rempasswordcheckbox);
        username = (EditText) findViewById(R.id.tf_userName);
        userPassword = (EditText) findViewById(R.id.tf_password);
        SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
        username.setText(loginPreferences.getString(USERNAME, ""));
        userPassword.setText(loginPreferences.getString(PASSWORD, ""));
        Button login = (Button) findViewById(R.id.btn_login);
        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                loginAction();
            }
        });
    }

    private void loginAction(){
        boolean isUserValidated = true;
        boolean isPasswordValidated = true;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        EditText username = (EditText) findViewById(R.id.tf_userName);
        String user_Name = username.getText().toString();
        EditText userPassword = (EditText) findViewById(R.id.tf_password);
        String user_Password = userPassword.getText().toString();

        //Pass value for userName variable of the web service
        PropertyInfo unameProp =new PropertyInfo();
        unameProp.setName("username");//Define the variable name in the web service method
        unameProp.setValue(user_Name);//set value for userName variable
        unameProp.setType(String.class);//Define the type of the variable
        request.addProperty(unameProp);//Pass properties to the variable

        //Pass value for Password variable of the web service
        PropertyInfo passwordProp =new PropertyInfo();
        passwordProp.setName("password");
        passwordProp.setValue(user_Password);
        passwordProp.setType(String.class);
        request.addProperty(passwordProp);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try{
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            String status = response.toString();
            TextView result = (TextView) findViewById(R.id.tv_status);
            result.setText(response.toString());

            if(status.equals("Success")) {
                //   ADD  to save  and  read next time
                String strUserName = username.getText().toString().trim();
                String strPassword = userPassword.getText().toString().trim();

                if (null == strUserName || strUserName.length() == 0) {
                    //  showToast("Enter Your Name");
                    username.setError( "username is required!" );
                    isUserValidated = false;
                }
                if (null == strPassword || strPassword.length() == 0) {
                    //      showToast("Enter Your Password");
                    isPasswordValidated = false;
                    userPassword.setError( "password is required!" );
                } 
                if (isUserValidated = true && isPasswordValidated == true) {
                    if (chkRememberMe.isChecked()) {
                        SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
                        loginPreferences.edit().putString(USERNAME, strUserName).putString(PASSWORD, strPassword).commit();
                    } 
                    else {
                        SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
                        loginPreferences.edit().clear().commit();
                    }
                }
                if (isUserValidated && isPasswordValidated) {

                    Intent intent = new Intent(CustomerLogin.this,PayPalIntegrationActivity.class);
                    intent.putExtra("GrandTotal", total);
                    intent.putExtra("Title", mTitle);
                    intent.putExtra("login",username.getText().toString());
                    startActivity(intent);
                }
            }
            else {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_custom_layout,
                        (ViewGroup) findViewById(R.id.toast_layout_root));
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.TOP, 0, 30);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();

            }
        }
        catch(Exception e){

        }
    }
}

如果登录成功,则必须在textview上显示电子邮件。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以使用asy任务执行此操作,

doInBackground中的

进行登录操作。 Asyctask将此操作用于另一项任务。

在onPostExecute

中进行编译时

显示成功消息。 但是你需要使用runOnUiThread,因为你无法从非ui线程获得ui控件。

private class loginTask extends AsyncTask<URL, Integer, int> {
     protected Long doInBackground(URL... urls) {

         // start login process

         return 1;
     }


     protected void onPostExecute(int result) {

           _activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textview.setText("login success");
                }
            });

     }
 }