使用新旧Firebase进行身份验证

时间:2016-05-24 20:41:15

标签: android firebase firebase-realtime-database firebase-authentication

我有问题,我无法通过我的应用程序连接到多个互联网提供商。首先我相信这是Firebase的新版本,但它似乎是另一回事。

如果我在Android Studio中安装新的模拟器并连接到任何互联网连接,它只允许我使用该连接进入我的应用程序。

当我创建一个新项目并对其进行测试时,问题仍然存在。

在内部登录后,活动不会被调用。

firebaseRef.authWithPassword()

给我以下对话框:

  

另一次调用auth

时,激活了激活或待处理的身份验证凭据

现在出现了:

  

连接到身份验证服务器时出现异常:无法解析hos" auth.firebase.com":没有与主机名关联的地址

  • SDK工具 - Google Play服务(第30版)
  • SDK工具 - Google存储库(第26版)
  • Android Studio 2.1.1
  • 模拟器上的Google Play服务9.0.80

访问用户:电子邮件:steeve@hotmail.com密码:12345678

登录活动:

  public class LoginActivity extends AppCompatActivity {

    protected EditText emailEditText;
    protected EditText passwordEditText;
    protected Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


  // example: https://todoapprj.firebaseio.com/users/0736e525-fe72-44f7-a828-c4d1c66d541c

        // Getting the firebase reference url
        final Firebase ref = new Firebase("https://todoapprj.firebaseio.com");

        //setting up the Views
        emailEditText = (EditText) findViewById(R.id.emailField);
        passwordEditText = (EditText) findViewById(R.id.passwordField);
        loginButton = (Button) findViewById(R.id.loginButton);


        // LOGIN BUTTON
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.i("Pressed", "login button");

                // Retrieves user inputs
                String email = emailEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                // trims the input
                email = email.trim();
                password = password.trim();

                // Authentaction Check for login.
                if (email.isEmpty() || password.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                    builder.setMessage(R.string.login_error_message)
                            .setTitle(R.string.login_error_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                } else {
                    final String emailAddress = email;

                    Log.i("Before", "authWithPassword");

                    //Login with email/password combination
                    ref.authWithPassword(email, password, new Firebase.AuthResultHandler() {

                        @Override
                        public void onAuthenticated(AuthData authData) {
                            Log.i("Inside", "authWithPassword");

                            // Authenticated successfully with authData
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("email", emailAddress);
                            ref.child("users").child(authData.getUid()).updateChildren(map);   // VIKTIGT ATT ÄR UPDATECHILDREN ANNARS FÖRSVINNER DET VID UTLOGGNING

                            Intent intent = new Intent(LoginActivity.this, MainRealb.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }


                        // If authenticated failed with error
                        @Override
                        public void onAuthenticationError(FirebaseError firebaseError) {
                            Log.i("Auth", "error");

                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage(firebaseError.getMessage())
                                    .setTitle(R.string.login_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    });
                }
            }
        });
    }

主要活动:

   public class MainRealb extends AppCompatActivity {

    Firebase mainRef = null;
    private String waypointsUrl;
    private String mUserId;
    private Firebase mRef;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainreal);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Används för att fungera offline -  setPersistenceEnabled(true)

        Firebase.setAndroidContext(this);

        mRef = new Firebase("https://todoapprj.firebaseio.com");
        if (mRef.getAuth() == null) {
            loadLoginView();
        }


        try {
            mUserId = mRef.getAuth().getUid();
        } catch (Exception e) {
            loadLoginView();
        }


        waypointsUrl = "https://todoapprj.firebaseio.com" + "/users/" + mUserId + "/waypoints";
        mainRef = new Firebase(waypointsUrl);


        Log.i("test", mainRef + "" + mUserId);

    }


    private void loadLoginView() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout) {
            mRef.unauth();
            loadLoginView();
        }

        return super.onOptionsItemSelected(item);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

摇篮:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    // This excludes those files from the build to avoid potential build errors due to duplicate files.
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }

    defaultConfig {
        applicationId "com.example.rasmusjosefsson.rjcar"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.android.support:recyclerview-v7:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'

    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.google.code.gson:gson:2.6.2'

    compile 'com.google.maps.android:android-maps-utils:0.4.3'
    compile 'com.google.maps:google-maps-services:0.1.12'

    compile 'com.google.android.gms:play-services-ads:8.4.0'
    compile 'com.google.android.gms:play-services-auth:8.4.0'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
    compile 'com.google.android.gms:play-services-location:8.4.0'

    compile 'com.firebase:firebase-client-android:2.5.2'
    compile 'com.firebaseui:firebase-ui:0.3.1'
}

0 个答案:

没有答案