如何只对一次用户进行身份验证?

时间:2014-07-15 18:47:21

标签: android authenticator

我有一个需要从服务器进行身份验证的应用程序。 每当我尝试从该服务器获取服务时,我应该使用扩展的Authenticator类来设置这个代码。

Authenticator.setDefault(新的UserAuthenticator(“my_mail”,“my_password”));

这是UserAuthenticator类:

import java.net.Authenticator;
import java.net.PasswordAuthentication;

import android.util.Log;
public class UserAuthenticator extends Authenticator
{
    //counter used to iterate login process
    private int redirect;
    private String email;
    private String password;

    /**
     * UserAuthenticator constructor
     */
    public UserAuthenticator(String mail, String pwd)
    {
        this.email = mail;
        this.password = pwd;
        Log.d("email+password", email+password);
    }

    /**
     * <p>Called when password authorization is needed</p>
     *
     */
    @Override
    protected PasswordAuthentication getPasswordAuthentication()
    {

        //to catch login redirect exception
        if(redirect>0){

            return null;
        }else{
            redirect ++;
        }

        /**
         * <p>Return the information (a data holder that is used by
         * Authenticator</p
         */ 
        return new PasswordAuthentication(email,
                password.toCharArray());
    }
    public PasswordAuthentication authenticationResult()
    {
        return getPasswordAuthentication();
    }

}

有没有办法在所有其他活动中实例化此类一次(然后用户保持身份验证)。

我的意思是我想要对用户进行一次身份验证,并且每次我需要请求服务器时都不要重复该类的实例化。

ps:我试图让它成为一个静态内部类,但是如果没有每次都实例化它就无法工作。 也许在Android中有另一种方式而不是Authenticator类,因为我在java Web应用程序中知道这个类。

0 个答案:

没有答案