2014-06-17 13 views
6

Burada şu kodu kullanarak google + integartion.i am yapıyorum ama şu hatayı yaşıyorum: onConnectionFailed: ConnectionResult.getErrorCode() = 4.So lütfen lütfen bana yardım edin ve bana bu kodu yanlış yaptığımı söyle ve bu için bana çözüm sağlayın.Onlar size çok minnettar olacaktır.Çok çok aradım ama hiçbir şey bulamadım.Çok hızlı Google örneğini kullanıyorum.Ayrıca başka bir sorun da var Bu kodda doğum günü vb gibi kişisel bilgiler edemiyorum.onConnectionFailed: ConnectionResult.getErrorCode() = 4

public class GooglePlus extends FragmentActivity implements 
      ConnectionCallbacks, OnConnectionFailedListener, View.OnClickListener { 
     String fb_userId, fb_username; 
     SharedPreferences pref; 
     SharedPreferences.Editor editor; 
     private static final int STATE_DEFAULT = 0; 
     private static final int STATE_SIGN_IN = 1; 
     private static final int STATE_IN_PROGRESS = 2; 

     private static final int RC_SIGN_IN = 0; 

     private static final int DIALOG_PLAY_SERVICES_ERROR = 0; 

     private static final String SAVED_PROGRESS = "sign_in_progress"; 

     // GoogleApiClient wraps our service connection to Google Play services and 
     // provides access to the users sign in state and Google's APIs. 
     private GoogleApiClient mGoogleApiClient; 

     // We use mSignInProgress to track whether user has clicked sign in. 
     // mSignInProgress can be one of three values: 
     // 
     // STATE_DEFAULT: The default state of the application before the user 
     // has clicked 'sign in', or after they have clicked 
     // 'sign out'. In this state we will not attempt to 
     // resolve sign in errors and so will display our 
     // Activity in a signed out state. 
     // STATE_SIGN_IN: This state indicates that the user has clicked 'sign 
     // in', so resolve successive errors preventing sign in 
     // until the user has successfully authorized an account 
     // for our app. 
     // STATE_IN_PROGRESS: This state indicates that we have started an intent to 
     // resolve an error, and so we should not start further 
     // intents until the current intent completes. 
     private int mSignInProgress; 

     // Used to store the PendingIntent most recently returned by Google Play 
     // services until the user clicks 'sign in'. 
     private PendingIntent mSignInIntent; 

     // Used to store the error code most recently returned by Google Play 
     // services 
     // until the user clicks 'sign in'. 
     private int mSignInError; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_google_plus); 
      pref = PreferenceManager.getDefaultSharedPreferences(this); 
      if (savedInstanceState != null) { 
       mSignInProgress = savedInstanceState.getInt(SAVED_PROGRESS, 
         STATE_DEFAULT); 
      } 

      mGoogleApiClient = buildGoogleApiClient(); 

      Handler handle = new Handler(); 
      handle.postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          // resolveSignInError(); 

         } 
        }); 

       } 
      }, 1500); 

     } 

     private GoogleApiClient buildGoogleApiClient() { 
      // When we build the GoogleApiClient we specify where connected and 
      // connection failed callbacks should be returned, which Google APIs our 
      // app uses and which OAuth 2.0 scopes our app requests. 
      return new GoogleApiClient.Builder(this).addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this).addApi(Plus.API, null) 
        .addScope(Plus.SCOPE_PLUS_LOGIN).build(); 
     } 

     @Override 
     protected void onStart() { 
      super.onStart(); 
      mGoogleApiClient.connect(); 
     } 

     @Override 
     protected void onStop() { 
      super.onStop(); 

      if (mGoogleApiClient.isConnected()) { 
       mGoogleApiClient.disconnect(); 
      } 
     } 

     @Override 
     protected void onSaveInstanceState(Bundle outState) { 
      super.onSaveInstanceState(outState); 
      outState.putInt(SAVED_PROGRESS, mSignInProgress); 
     } 

     @Override 
     public void onConnectionFailed(ConnectionResult result) { 
      // Refer to the javadoc for ConnectionResult to see what error codes 
      // might 
      // be returned in onConnectionFailed. 
      Log.i("", "onConnectionFailed: ConnectionResult.getErrorCode() = " 
        + result.getErrorCode()); 

      if (mSignInProgress != STATE_IN_PROGRESS) { 
       // We do not have an intent in progress so we should store the 
       // latest 
       // error resolution intent for use when the sign in button is 
       // clicked. 
       mSignInIntent = result.getResolution(); 
       mSignInError = result.getErrorCode(); 

       if (mSignInProgress == STATE_SIGN_IN) { 
        // STATE_SIGN_IN indicates the user already clicked the sign in 
        // button 
        // so we should continue processing errors until the user is 
        // signed in 
        // or they click cancel. 
        resolveSignInError(); 
       } 
      } 

      // In this sample we consider the user signed out whenever they do not 
      // have 
      // a connection to Google Play services. 

     } 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 

     /* 
     * Starts an appropriate intent or dialog for user interaction to resolve 
     * the current error preventing the user from being signed in. This could be 
     * a dialog allowing the user to select an account, an activity allowing the 
     * user to consent to the permissions being requested by your app, a setting 
     * to enable device networking, etc. 
     */ 
     private void resolveSignInError() { 
      if (mSignInIntent != null) { 
       // We have an intent which will allow our user to sign in or 
       // resolve an error. For example if the user needs to 
       // select an account to sign in with, or if they need to consent 
       // to the permissions your app is requesting. 

       try { 
        // Send the pending intent that we stored on the most recent 
        // OnConnectionFailed callback. This will allow the user to 
        // resolve the error currently preventing our connection to 
        // Google Play services. 
        mSignInProgress = STATE_IN_PROGRESS; 
        startIntentSenderForResult(mSignInIntent.getIntentSender(), 
          RC_SIGN_IN, null, 0, 0, 0); 
       } catch (SendIntentException e) { 
        Log.i("", 
          "Sign in intent could not be sent: " 
            + e.getLocalizedMessage()); 
        // The intent was canceled before it was sent. Attempt to 
        // connect to 
        // get an updated ConnectionResult. 
        mSignInProgress = STATE_SIGN_IN; 
        mGoogleApiClient.connect(); 
       } 
      } else { 
       // Google Play services wasn't able to provide an intent for some 
       // error types, so we show the default Google Play services error 
       // dialog which may still start an intent on our behalf if the 
       // user can resolve the issue. 
       showDialog(DIALOG_PLAY_SERVICES_ERROR); 
      } 
     } 

     @SuppressWarnings("unused") 
     @Override 
     public void onConnected(Bundle arg0) { 
      // Reaching onConnected means we consider the user signed in. 
      Log.i("onConnected", "onConnected"); 

      // Retrieve some profile information to personalize our app for the 
      // user. 
      Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); 

      String personName = currentUser.getDisplayName(); 
      String personPhotoUrl = currentUser.getImage().getUrl(); 
      String personGooglePlusProfile = currentUser.getUrl(); 
      String email = Plus.AccountApi.getAccountName(mGoogleApiClient); 

      Log.i("personName", personName); 
      Log.i("email", email); 
      Log.i("Gender", "" + currentUser.getGender()); 
      Log.i("Birthday", "" + currentUser.getBirthday()); 

      // Indicate that the sign in process is complete. 
      mSignInProgress = STATE_DEFAULT; 

      /* 
      * fb_userId = currentUser.getId(); fb_username = 
      * currentUser.getDisplayName(); editor = pref.edit(); 
      * editor.putString("fb_userId", fb_userId); 
      * editor.putString("fb_username", fb_username); 
      * editor.putString("social_provider", "google +"); 
      * 
      * editor.putString("gender", currentUser.getGender()); 
      * editor.putString("birthday", currentUser.getBirthday()); 
      * 
      * editor.putString("device_name", "android"); 
      * 
      * editor.putString("email", 
      * Plus.AccountApi.getAccountName(mGoogleApiClient)); editor.commit(); 
      */ 

     } 

     @Override 
     public void onConnectionSuspended(int arg0) { 
      // The connection to Google Play services was lost for some reason. 
      // We call connect() to attempt to re-establish the connection or get a 
      // ConnectionResult that we can attempt to resolve. 
      mGoogleApiClient.connect(); 

     } 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      switch (requestCode) { 
      case RC_SIGN_IN: 
       if (resultCode == RESULT_OK) { 
        // If the error resolution was successful we should continue 
        // processing errors. 
        mSignInProgress = STATE_SIGN_IN; 
       } else { 
        // If the error resolution was not successful or the user 
        // canceled, 
        // we should stop processing errors. 
        mSignInProgress = STATE_DEFAULT; 
       } 

       if (!mGoogleApiClient.isConnecting()) { 
        // If Google Play services resolved the issue with a dialog then 
        // onStart is not called so we need to re-attempt connection 
        // here. 
        mGoogleApiClient.connect(); 
       } 
       break; 
      } 
     } 

     @Override 
     protected Dialog onCreateDialog(int id) { 
      switch (id) { 
      case DIALOG_PLAY_SERVICES_ERROR: 
       if (GooglePlayServicesUtil.isUserRecoverableError(mSignInError)) { 
        return GooglePlayServicesUtil.getErrorDialog(mSignInError, 
          this, RC_SIGN_IN, 
          new DialogInterface.OnCancelListener() { 
           @Override 
           public void onCancel(DialogInterface dialog) { 
            Log.e("", 
              "Google Play services resolution cancelled"); 
            mSignInProgress = STATE_DEFAULT; 

           } 
          }); 
       } else { 
        return new AlertDialog.Builder(this) 
          .setMessage(R.string.play_services_error) 
          .setPositiveButton(R.string.close, 
            new DialogInterface.OnClickListener() { 
             @Override 
             public void onClick(DialogInterface dialog, 
               int which) { 
              Log.e("", 
                "Google Play services error could not be " 
                  + "resolved: " 
                  + mSignInError); 
              mSignInProgress = STATE_DEFAULT; 
              // mStatus.setText(R.string.status_signed_out); 
             } 
            }).create(); 
       } 
      default: 
       return super.onCreateDialog(id); 
      } 
     } 
    } 
+0

hey - google oturum zorundasın, bu hata 4. çoğunlukla bir oturum açma hatadır. Android cihazınızda bir gmail kimliği kullanın. Bana – sudhishkr

+0

'u bildirin. İlk kez, hiç oturum açmadığınız için # 4 numaralı hatayı alacağınızı düşünüyorum. Eğer mSignInIntent kullanarak oturum sonra, benim için onConnected() –

cevap

3

Aynı sorun vardı. Google API konsolu >> rıza ekranında çözümlendim ve bir Ürün Adı ekledim. http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

sorunun ben uygulaması için kimlik bilgilerini oluşturulmamış olmasıydı Benim için .addApi(Plus.API, Plus.PlusOptions.builder().build())

+0

İşleri zarar vermesi durumunda, tek sorun size bir uygulama sahibiyseniz yalnızca (API anahtarı ve Müşteri Kimliği için) ayıklama sertifikasını kullanabilirsiniz olmasıdır. Davetli geliştiriciler yalnızca sürüm sertifikalarını kullanabilir. – Darkmike

2

tarafından .addApi(Plus.API, null) değiştirmek gereken ana aktivitede:

Bu öğretici deneyin.

Google Geliştirici Konsolu gitti> Kimlik>

, uygulamam için gelen paket adı ve SHA1 dolu app reran ve sonra işe yaradı Yeni İstemci Kimliği Oluştur! tamamen interaktif modda API kullanılırken

sen MapView kullandığınız durumda
1

, tüm kuralları yani

takip ediyor emin olun", bu sınıfın kullanıcılarının benzer yöntemlerle de tüm aktivite yaşam döngüsü yöntemleri iletmelidir MapView sınıfında. yaşam döngüsü yöntemlerinin örnekleri onCreate(), OnDestroy(), onResume() ve OnPause() içerir. yaşam döngüsü olaylarını iletirken, basit modda API kullanılırken isteğe bağlıdır. Ayrıntılar için, litre moduna bakınız dokümantasyon."

@Override 
public void onResume() { 
    mapView.onResume(); 
    super.onResume(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mapView.onLowMemory(); 
}