2014-06-19 39 views
0

嘿每一個我使用此代碼來從Facebook獲取個人資料,但它不適合我... ,我按照這個教程http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/我不知道這個問題是什麼,請幫助我。在Android中獲取Facebook的個人資料

public void getProfileInformation() { 
      mAsyncRunner.request("me", new RequestListener() { 
       @Override 
       public void onComplete(String response, Object state) { 

        Log.d("Profile", response); 
        String json = response; 
        try { 
         txt2.setText("testing 124"); 
         JSONObject profile = new JSONObject(json); 
         // getting name of the user 
         final String name = profile.getString("name"); 
         // getting email of the user 
         final String email = profile.getString("email"); 
         txt2.setText("Name: " +name+"Email: " +email); 
         runOnUiThread(new Runnable() { 

          @Override 
          public void run() { 
           Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show(); 

          } 

         }); 

        } catch (JSONException e) { 
         txt2.setText(e.toString()); 
         e.printStackTrace(); 
        } 
        logoutFromFacebook(); 
       } 

       @Override 
       public void onIOException(IOException e, Object state) { 
       } 

       @Override 
       public void onFileNotFoundException(FileNotFoundException e, 
         Object state) { 
       } 

       @Override 
       public void onMalformedURLException(MalformedURLException e, 
         Object state) { 
       } 

       @Override 
       public void onFacebookError(FacebookError e, Object state) { 
       } 
      }); 
     } 

請幫我我應該怎麼辦..

這些錯誤:

06-19 06:35:07.180: E/chromium(1759): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
06-19 06:35:07.180: E/chromium(1759): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
06-19 06:35:07.200: E/chromium(1759): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
06-19 06:35:07.200: E/chromium(1759): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
06-19 06:35:07.230: E/chromium(1759): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed 
06-19 06:35:12.470: E/chromium(1759): [ERROR:simple_backend_impl.cc(186)] File structure does not match the disk cache backend. 
06-19 06:35:12.470: E/chromium(1759): [ERROR:simple_backend_impl.cc(402)] Simple Cache Backend: wrong file structure on disk: /data/data/com.facebook.androidhive/app_webview/Cache 
06-19 06:36:38.000: E/chromium(1893): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
06-19 06:36:38.000: E/chromium(1893): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
06-19 06:36:38.010: E/chromium(1893): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
06-19 06:36:38.010: E/chromium(1893): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
06-19 06:36:38.010: E/chromium(1893): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed 
06-19 07:36:10.530: E/AndroidRuntime(2428): FATAL EXCEPTION: Thread-94 
06-19 07:36:10.530: E/AndroidRuntime(2428): Process: com.facebook.androidhive, PID: 2428 
06-19 07:36:10.530: E/AndroidRuntime(2428): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
06-19 07:36:10.530: E/AndroidRuntime(2428):  at android.os.Handler.<init>(Handler.java:200) 
06-19 07:36:10.530: E/AndroidRuntime(2428):  at android.os.Handler.<init>(Handler.java:114) 
06-19 07:36:10.530: E/AndroidRuntime(2428):  at android.widget.Toast$TN.<init>(Toast.java:327) 
06-19 07:36:10.530: E/AndroidRuntime(2428):  at android.widget.Toast.<init>(Toast.java:92) 
06-19 07:36:10.530: E/AndroidRuntime(2428):  at android.widget.Toast.makeText(Toast.java:241) 
06-19 07:36:10.530: E/AndroidRuntime(2428):  at com.facebook.androidhive.AndroidFacebookConnectActivity$6.onComplete(AndroidFacebookConnectActivity.java:200) 
06-19 07:36:10.530: E/AndroidRuntime(2428):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254) 
+0

使用Facebook圖形API – Kishan

+0

什麼是你所得到的錯誤或異常。 –

+0

如果我點擊按鈕,所以應用程序已經崩潰。 –

回答

0

像您必須使用Facebook的圖形API的評論中提到的人,特別是你必須使用GraphUser與呼叫從Request.GraphUserCallback() { ... }

使用下面的代碼獲得Facebook的個人資料信息。確保您的APP_ID包括在元數據中的AndroidManifest.xml文件

代碼:

private void onSessionStateChange(Session session, SessionState state, 
     Exception exception) { 
    if (state.isOpened()) { 
      Log.i(TAG, "Logged in..."); 

      /* 
      * When callback comes from facebook request user data from 
      * facebook and show the results. 
      */ 
      Request.newMeRequest(session, new Request.GraphUserCallback() { 

       @Override 
       public void onCompleted(GraphUser user, Response response) { 
        if (user != null) { 
         //Enter Code to retrieve information 


        } 

       } 
      }).executeAsync(); // If .executeAsync() is not called then the 
           // above code does not execute 
           // Async() executes in a parallel thread. 
    } else if (state.isClosed()) { 
     Log.i(TAG, "Logged out..."); 
    } 

} 

裏面public void onCompleted(GraphUser user, Response respoonse)你可以爲用戶信息請求,就像這樣:

user.getId(); 
user.getName(); 
user.getBirthday(); 

您可以將這個變量中的信息,或通過它傳遞給另一個活動:

Log.w("myConsultant", 
       user.getId() + " " + user.getName() + " " 
             + user.getInnerJSONObject() + " " 
             + user.getLocation()); 
       i = new Intent(this, NewActivity.class); 
       i.putExtra("Fb_name", user.getName()); 
       i.putExtra("Fb_location", user.getLocation() 
           .getCity()); 
       i.putExtra("Fb_id", user.getId()); 

       Log.i(TAG, " FB USer info fetched"); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(i); 

你可以調用onSessionStateChangeonCreate()是像這樣創建活動時,檢索用戶的信息:

Session session = Session.getActiveSession(); 
    if (session != null && (session.isOpened() || session.isClosed())) { 
     onSessionStateChange(session, session.getState(), null); 
    } 
+0

你的代碼不工作會話永遠爲空 –

+0

這可能是你可能沒有正確保存會話嗎? – AndyRoid

+0

將剩餘的代碼發佈到一個粘貼bin URL中,以便我可以看到發生了什麼 – AndyRoid

相關問題