微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 无法登录.请检查您的网络连接,然后重试

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

我收到此错误

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

我有MainActivity和三个fragmenets(MainFragment,GameFragment和ResultFragment).

MainFragment是主菜单的片段,使用按钮点击开始游戏.

授权?

我已在Google Play开发者控制台中将我的游戏与SHA-1相关联并授权.

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

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

应用ID?

我将app id和leaderboard id添加到strings.xml中,并将Meta标签添加到Android Manifest.

我在AndroidManifest.xml中添加了Internet权限

测试?

我使用物理设备从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);
}

解决方法:

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

我已将values id和leaderboard id从strings.xml移到values文件夹中的ids.xml.

删除了所有客户端ID,并再次为调试密钥库和发布密钥库添加客户端ID.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐