Java Firebase 身份验证 FirebaseNetworkException:发生网络错误(例如超时、连接中断或主机无法访问)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40052162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:40:04  来源:igfitidea点击:

Firebase Authentication FirebaseNetworkException: A network error (such as timeout, interrupted connection or unreachable host) has occurred

javaandroidfirebasefirebase-authentication

提问by Landen

I'm creating an authentication workflow for my android app. I'm allowing users to sign in with username/password and various OAuth providers. I'm validating emails and password so that, I know the information I'm passing to Firebase is valid. I'm using com.google.firebase:firebase-auth:9.6.1

我正在为我的 android 应用程序创建一个身份验证工作流程。我允许用户使用用户名/密码和各种 OAuth 提供商登录。我正在验证电子邮件和密码,以便我知道我传递给 Firebase 的信息是有效的。我正在使用com.google.firebase:firebase-auth:9.6.1

When I execute the following code, I get a callback that says the operation not successful with an error.

当我执行以下代码时,我收到一个回调,提示操作不成功并出现错误。

mFirebaseAuth.signInWithEmailAndPassword(username,password).addOnCompleteListener(this);

Callback function or completion listener tells me

回调函数或完成侦听器告诉我

com.google.firebase.FirebaseNetworkException: A network error (such as timeout, interrupted connection or unreachable host) has occurred.

The username I'm passing doesn't exist yet. So, I would assume to see some kind of error stating the user doesn't exist. Am I passing something incorrectly or am I assuming incorrectly? I can also see that in the Firebase documentation, the iOS library has a various error codes common to all API methods section where as the Android section doesn't show this. One of these exceptions is FIRAuthErrorCodeUserNotFound. So, does that functionality even exists in the Android library?

我传递的用户名尚不存在。所以,我假设会看到某种错误,说明用户不存在。我是否错误地传递了某些东西,还是我假设错误?我还可以在 Firebase 文档中看到,iOS 库具有所有 API 方法部分通用的各种错误代码,而 Android 部分没有显示这一点。这些例外之一是FIRAuthErrorCodeUserNotFound。那么,该功能是否存在于 Android 库中?

采纳答案by Tej

It can also happen when google play services are not running. Try to launch play store and check if it is working. If not reboot of device issue.And also compare the google play services using in the project and google play services in the device are same if not update google play services.

当 google play 服务未运行时也可能发生这种情况。尝试启动 Play 商店并检查它是否正常工作。如果没有重启设备问题。如果不更新谷歌播放服务,还比较项目中使用的谷歌播放服务和设备中的谷歌播放服务是否相同。

This is just a minor but possible case where it gives the exception.

这只是一个次要但可能的情况,它给出了例外。

回答by Chetan

I was facing the same issue. what solved my problem by clear extra space in API_KEY, so my suggestion is to check your GoogleService-Info.plist for

我面临着同样的问题。通过清除 API_KEY 中的额外空间解决了我的问题,所以我的建议是检查您的 GoogleService-Info.plist

  1. API_KEY is proper (without extra spaces)
  2. GOOGLE_APP_ID
  3. CLIENT_ID
  1. API_KEY 是正确的(没有多余的空格)
  2. GOOGLE_APP_ID
  3. CLIENT_ID

I think this might help you

我想这可能对你有帮助

回答by Dan Alboteanu

changing from <form></form>to <div></div>solved this problem:

更改<form></form><div></div>解决了这个问题:

"A network error (such as timeout, interrupted connection or unreachable host) has occurred in a form element in the HTML. Small bug."

“HTML 中的表单元素中出现网络错误(例如超时、连接中断或主机无法访问)。小错误。”

回答by Huy - Logarit

<a (click)="login()" class="nav-link">Login</a>

Do not put hrefattribute into tag a. It is help to solved my case

不要将href属性放入标签中a。这有助于解决我的案子

回答by V.K.Agarwal

i have faced this problem after searching a lot i got the solution, i my case it was happening due to the restriction of background data

我在搜索了很多之后遇到了这个问题我得到了解决方案,我的情况是由于背景数据的限制而发生的

回答by ZuzEL

Check your rewrites in firebase.json, make sure you don't rewrite auth provider url /__/

检查您的重写firebase.json,确保您不重写身份验证提供程序 url/__/

{
  "database": {
    "rules": "database.rules.json"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "!/__/**",
        "destination": "/index.html"
      },
      {
        "source": "**/!(*.js|*.html|*.css|*.json|*.svg|*.png|*.jpg|*.jpeg)",
        "destination": "/index.html"
      }
    ]
  }
}

Also it could be service worker problem. See https://github.com/Polymer/polymer-cli/issues/290

也可能是服务人员问题。见https://github.com/Polymer/polymer-cli/issues/290

回答by Konstantin Nikitin

If you do this check in form onSumbit handler, you need to preventDefault before sending a request.

如果您以 onSumbit 处理程序的形式进行此检查,则需要在发送请求之前阻止默认。

This is a snippet (React) that works:

这是一个有效的片段(React):

class LoginComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            email: '',
            password: '',
        };
        this.login = this.login.bind(this);
        this.handleLoginChange = this.handleLoginChange.bind(this);
        this.handlePasswordChange = this.handlePasswordChange.bind(this);
    }

    handleLoginChange(event) {
        this.setState({
            email: event.target.value,
            password: this.state.password,
        });
    }

    handlePasswordChange(event) {
        this.setState({
            email: this.state.email,
            password: event.target.value,
        });
    }

    login(event) {
        event.preventDefault();
        firebase.auth()
            .signInWithEmailAndPassword(this.state.email, this.state.password)
            .then(function(user) {
                      window.alert('OK' + user);
                  },
                  function(error) {
                      window.alert('ERR' + error);
                  });
    }

    render() {
        return (
            <form onSubmit={this.login}>
                <TextField hintText="login" value={this.state.email} onChange={this.handleLoginChange} /><br/>
                <TextField hintText="password" type="password" value={this.state.password} onChange={this.handlePasswordChange} /><br/>
                <RaisedButton type="submit" label="Login" primary={true} />
            </form>
        )
    }
}

回答by Kangudie Muanza - Killmonger

In your AndroidManifest.xml add, it works for me

在您的 AndroidManifest.xml 添加中,它对我有用

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

回答by luishsivla

I resolve the problem fixing the date of my cell phone were one year late, when I fixed the date, everything worked well again and the error disappear, I hopping this help you

我解决了修复手机日期晚一年的问题,当我修复日期时,一切又正常了,错误消失了,我希望这对你有帮助

回答by Sathish

In my case the issue is due to mismatch versions of dependencies, then I have changed dependencies like below

在我的情况下,问题是由于依赖项的版本不匹配,然后我更改了依赖项,如下所示

implementation 'com.google.firebase:firebase-auth:+'
implementation 'com.google.firebase:firebase-core:+'
implementation 'com.google.android.gms:play-services-auth:+'
implementation 'com.firebaseui:firebase-ui-auth:+'

then its started working perfectly.

然后它开始完美地工作。