2016-09-19 77 views
0

Android Google plus,之後在Google帳戶上成功登錄,面臨空指針異常在以下代碼中,用於獲取Google加好友列表。Android Google plus

GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
      httpTransport, jsonFactory, clientId, clientSecret, code, redirectUrl).execute(); 

我的整個代碼如下:

public void setUp() throws IOException { 

    HttpTransport httpTransport = new NetHttpTransport(); 
    JacksonFactory jsonFactory = new JacksonFactory(); 

    // Go to the Google API Console, open your application's 
    // credentials page, and copy the client ID and client secret. 
    // Then paste them into the following code. 

    String clientId = "Client_ID_for_Web_application"; 
    String clientSecret = "Client_secredt_for_Web_application"; 

    // Or your redirect URL for web based applications. 
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; 
    String scope = "https://www.googleapis.com/auth/contacts.readonly"; 


    // Step 1: Authorize --> 

    String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId, 
      redirectUrl, 
      Arrays.asList(scope)) 
      .build(); 

    // Point or redirect your user to the authorizationUrl. 

    System.out.println("Go to the following link in your browser:"); 
    System.out.println(authorizationUrl); 

    // Read the authorization code from the standard input stream. 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("What is the authorization code?"); 
    String code = in.readLine(); 
    // End of Step 1 <-- 

    // Step 2: Exchange --> 
    GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
      httpTransport, jsonFactory, clientId, clientSecret, code, redirectUrl).execute(); 
    // End of Step 2 <-- 

    GoogleCredential credential = new GoogleCredential.Builder() 
      .setTransport(httpTransport) 
      .setJsonFactory(jsonFactory) 
      .setClientSecrets(clientId, clientSecret) 
      .build() 
      .setFromTokenResponse(tokenResponse); 

    People peopleService = new People.Builder(httpTransport, jsonFactory, credential) 
      .build(); 

    Log.e("peopel:", peopleService + ""); 

    ListConnectionsResponse response = peopleService.people().connections().list("people/me").execute(); 
    List<Person> connections = response.getConnections(); 

    Log.e("peope2:", connections + ""); 

    Person profile = peopleService.people().get("people/me").execute(); 

    Log.e("peope3:", profile + ""); 
} 

更新

錯誤日誌中以下內容:

FATAL EXCEPTION: main 
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tops.mygapp/com.example.tops.mygapp.MainActivity}: java.lang.NullPointerException 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                     at android.os.Handler.dispatchMessage(Handler.java:99) 
                     at android.os.Looper.loop(Looper.java:137) 
                     at android.app.ActivityThread.main(ActivityThread.java:5041) 
                     at java.lang.reflect.Method.invokeNative(Native Method) 
                     at java.lang.reflect.Method.invoke(Method.java:511) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                     at dalvik.system.NativeStart.main(Native Method) 
                    Caused by: java.lang.NullPointerException 
                     at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:213) 
                     at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127) 
                     at com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setCode(AuthorizationCodeTokenRequest.java:147) 
                     at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.setCode(GoogleAuthorizationCodeTokenRequest.java:147) 
                     at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.setCode(GoogleAuthorizationCodeTokenRequest.java:79) 
                     at com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.<init>(AuthorizationCodeTokenRequest.java:103) 
                     at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.<init>(GoogleAuthorizationCodeTokenRequest.java:111) 
                     at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.<init>(GoogleAuthorizationCodeTokenRequest.java:92) 
                     at com.example.tops.mygapp.MainActivity.setUp(MainActivity.java:272) 
                     at com.example.tops.mygapp.MainActivity.onCreate(MainActivity.java:77) 
                     at android.app.Activity.performCreate(Activity.java:5104) 
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)  
                     at android.app.ActivityThread.access$600(ActivityThread.java:141)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)  
                     at android.os.Handler.dispatchMessage(Handler.java:99)  
                     at android.os.Looper.loop(Looper.java:137)  
                     at android.app.ActivityThread.main(ActivityThread.java:5041)  
                     at java.lang.reflect.Method.invokeNative(Native Method)  
                     at java.lang.reflect.Method.invoke(Method.java:511)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)  
                     at dalvik.system.NativeStart.main(Native Method)  

更新2

正是我感到了空在

BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("What is the authorization code?"); 
String code = in.readLine(); 

我得到的代碼空值。

+1

u能張貼logcat的? – Raghavendra

+0

我使用錯誤日誌更新了問題。 – TopsAndy

+0

請查看http://stackoverflow.com/questions/27522104/nullpointerexception-only-after-i-have-setserviceaccountuser-for-googlecredent類似的問題。 – Raghavendra

回答

0

由於手冊中描述: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtJava?hl=ru

,你必須使用這樣的:

// Get authorization code from user. 
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("What is the authorization code?"); 
String authorizationCode = null; 
try { 
    authorizationCode = in.readLine(); 
} catch (IOException ioe) { 
    ioe.printStackTrace(); 
} 
+0

hello @Vyacheslav,它無助於解決我的問題,我的問題在異常情況下是一樣的,由於null沒有得到任何結果。 – TopsAndy