2016-09-19 32 views
0

Android Google plus、その後に向かうヌルポインタ例外 google + friendリストを取得するためのコードです。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

まさに私は、コードのnull値を取得しています

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

関連する問題