java - 登录失败。请检查您的网络连接,然后重试

标签 java android google-play google-play-services google-play-games

我正在尝试使用 Google Play 游戏服务制作简单的游戏,但未能登录 Google Play 游戏。

我得到这个错误:

登录失败。请检查您的网络连接并重试。

我有 MainActivity 和三个 fragment 网(MainFragment、GameFragment 和 ResultFragment)。

MainFragment 是主菜单的 fragment ,其中有按钮可以点击开始游戏。

授权?

我已在 Google Play 开发者控制台中使用 SHA-1 链接并授权我的游戏。

当我使用 Android Studio 时,我的包名称类似于:aplikacijezaandroid.thebuttonchallenge,我在 Google Play 开发者控制台上的链接应用中添加了两个应用版本。

所以我有 com.aplikacijezaandroid.thebuttonchallenge 和 aplikacijezaandorid.thebuttonchallenge

应用 ID?

我将应用程序 ID 和排行榜 ID 添加到 strings.xml 中,并将元标记添加到 Android Manifest。

我在AndroidManifest.xml中添加了上网权限

测试?

我使用物理设备从 Android Studio 测试和调试应用程序,并且在 Google Play 开发者控制台中添加了我自己的 gmail 作为测试用户。

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="aplikacijezaandroid.thebuttonchallenge" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id"/>
   <meta-data android:name="com.google.android.gms.version" 

  android:value="@integer/google_play_services_version"/>
  </application>

这是 MainActivity 类:

 public class MainActivity extends Activity implements MainMenuFragment.Listener,   
 GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, 
 GameFragment.Listener, ResultFragment.Listener {

//Fragments
MainMenuFragment mMainFragment;
GameFragment mGameFragment;
ResultFragment mResultFragment;

// Client used to interact with Google APIs
private GoogleApiClient mGoogleApiClient;

// Are we currently resolving a connection failure?
private boolean mResolvingConnectionFailure = false;

// Has the user clicked the sign-in button?
private boolean mSignInClicked = false;

// Automatically start the sign-in flow when the Activity starts
private boolean mAutoStartSignInFlow = true;

// request codes we use when invoking an external activity
private static final int RC_RESOLVE = 5000;
private static final int RC_UNUSED = 5001;
private static final int RC_SIGN_IN = 9001;

//Debug
private String TAG = "IGRA";

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

    // Create the Google API Client with access to Plus and Games
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();

    //Fragments
    mMainFragment = new MainMenuFragment();
    mGameFragment = new GameFragment();
    mResultFragment = new ResultFragment();

    // listen to fragment events
    mMainFragment.setListener(this);
    mGameFragment.setListener(this);
    mResultFragment.setListener(this);

    //Treba dodati listenere


    // add initial fragment (welcome fragment)
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction().add(R.id.container, mMainFragment).commit();
    }
}

// Switch UI to the given fragment
void switchToFragment(Fragment newFrag) {
    getFragmentManager().beginTransaction().replace(R.id.container, newFrag)
            .commit();
}

private boolean isSignedIn() {
    return (mGoogleApiClient != null && mGoogleApiClient.isConnected());
}

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "onStart(): connecting");
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop(): disconnecting");
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onStartGameRequested() {
    startGame();
}

@Override
public void onShowAchievementsRequested() {

}

@Override
public void onShowLeaderboardsRequested() {

}

void startGame(){
    switchToFragment(mGameFragment);
}

public void onEnteredScore(int finalScore){

    mResultFragment.setFinalScore(finalScore);

    // push those accomplishments to the cloud, if signed in
    pushAccomplishments(finalScore);

    // switch to the exciting "you won" screen
    switchToFragment(mResultFragment);
}

private void pushAccomplishments(int finalScore) {

    if (!isSignedIn()) {
        // can't push to the cloud, so save locally
       // mOutbox.saveLocal(this);
        Log.d(TAG, "can't push to the cloud, so save locally");
        return;
    }
    Games.Leaderboards.submitScore(mGoogleApiClient,   getString(R.string.number_guesses_leaderboard),
            finalScore);
}


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

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "onConnected(): connected to Google APIs");
    // Show sign-out button on main menu
    //mMainFragment.setShowSignInButton(false);

    // Show "you are signed in" message on win screen, with no sign in button.
    //mWinFragment.setShowSignInButton(false);

    // Set the greeting appropriately on main menu
    Player p = Games.Players.getCurrentPlayer(mGoogleApiClient);
    String displayName;
    if (p == null) {
        Log.w(TAG, "mGamesClient.getCurrentPlayer() is NULL!");
        displayName = "???";
    } else {
        displayName = p.getDisplayName();
    }
    mMainFragment.setGreeting("Hello, " + displayName);


    // if we have accomplishments to push, push them
    /*if (!mOutbox.isEmpty()) {
        pushAccomplishments();
        Toast.makeText(this, getString(R.string.your_progress_will_be_uploaded),
                Toast.LENGTH_LONG).show();
    }*/
}

@Override
public void onWinScreenDismissed() {
    switchToFragment(mMainFragment);
}

@Override
public void onWinScreenSignInClicked() {

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == RC_SIGN_IN) {
        mSignInClicked = false;
        mResolvingConnectionFailure = false;
        if (resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        } else {
            BaseGameUtils.showActivityResultError(this, requestCode, resultCode,
                    R.string.signin_failure, R.string.signin_other_error);
        }
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "onConnectionSuspended(): attempting to connect");
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "onConnectionFailed(): attempting to resolve");
    if (mResolvingConnectionFailure) {
        Log.d(TAG, "onConnectionFailed(): already resolving");
        return;
    }

    if (mSignInClicked || mAutoStartSignInFlow) {
        mAutoStartSignInFlow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;
        if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, connectionResult,
                RC_SIGN_IN, getString(R.string.signin_other_error))) {
            mResolvingConnectionFailure = false;
        }
    }

    // Sign-in failed, so show sign-in button on main menu
    mMainFragment.setGreeting(getString(R.string.signed_out_greeting));
    //mMainMenuFragment.setShowSignInButton(true);
   // mWinFragment.setShowSignInButton(true);
}

最佳答案

我已经解决了这个问题,所以我会发布答案。

我已将应用程序 ID 和排行榜 ID 从 strings.xml 移动到 values 文件夹中的 ids.xml。

我已经删除了所有客户端 ID,并再次为调试 keystore 和发布 keystore 添加了客户端 ID。

关于java - 登录失败。请检查您的网络连接,然后重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26804929/

相关文章:

java - 如何为 nattable 中的某些列设置自定义样式?

java - 执行set命令时不显示环境变量

android insmod init_module 失败(所需 key 不可用)

android - Android 中应用内购买的 Beta 测试

google-play - 谷歌游戏控制台 : cannot download apk from artifact library

java命令行

java - 从循环中获取值(value)

带有 itemTouchHelper 的 android 拖放导致 IndexOutOfBoundsException

android - 如何删除 Android 中图像周围的空白区域?

android - 有没有办法更改 Google Play 应用程序商店中应用程序的名称?