2011-11-16 126 views
6

我通过我的android应用程序访问图API时遇到问题,该应用程序检索用户的朋友为JSONObject,提取他们的名字并将其显示在屏幕上。它应该是一个简单而直接的任务,但显然它不是。当我在Android Nexus I上运行我的应用程序时,我登录到Facebook,然后询问我单击“允许”按钮授予权限,然后重定向到空白页面。我期望看到我的朋友的名字,但它不会发生。有人能帮助我吗?无法通过Android应用程序访问Facebook朋友

public class Login extends Activity 
{ 
    Facebook mFacebook = new Facebook("201509899926116"); 
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
    String FILENAME = "AndroidSSO_data"; 
    private SharedPreferences mPrefs; 
    View linearLayout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     linearLayout = findViewById(R.id.main_layout); 

     /* Get existing access_token if any */ 
     mPrefs = getPreferences(MODE_PRIVATE); 
     String access_token = mPrefs.getString("access_token", null); 
     long expires = mPrefs.getLong("access_expires", 0); 

     if(access_token != null) 
     { 
      mFacebook.setAccessToken(access_token); 
     } 
     if(expires != 0) 
     { 
      mFacebook.setAccessExpires(expires); 
     } 

     /* Only call authorize if the access_token has expired. */ 
     if(!mFacebook.isSessionValid()) 
     { 
      mFacebook.authorize(this, new String[] {"user_birthday","email", 
        "user_relationships","user_religion_politics","user_hometown", 
        "user_location","user_relationship_details","user_education_history", 
        "user_likes","user_interests", "user_activities"}, 
        new DialogListener() 
      { 

       @Override 
       public void onComplete(Bundle values) 
       { 
        SharedPreferences.Editor editor = mPrefs.edit(); 
        editor.putString("access_token", mFacebook.getAccessToken()); 
        editor.putLong("access_expires", mFacebook.getAccessExpires()); 
        editor.commit(); 

        /* access the graph API */ 
        Log.d("Facebook-Example-Friends Request", "Started API request"); 
        mAsyncRunner.request("me/friends", new FriendsRequestListener()); 
        Log.d("Facebook-Example-Friends Request", "Finished API request"); 
       } 

       @Override 
       public void onFacebookError(FacebookError error) {} 

       @Override 
       public void onError(DialogError e) {} 

       @Override 
       public void onCancel() {} 
      }); 
     } 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 
     mFacebook.authorizeCallback(requestCode, resultCode, data); 
    } 

    public class FriendsRequestListener implements com.facebook.android.AsyncFacebookRunner.RequestListener 
    { 
     /** 
     * Called when the request to get friends has been completed. 
     * Retrieve and parse and display the JSON stream. 
     */ 
     public void onComplete(final String response) 
     { 
      try 
      { 
       // process the response here: executed in background thread 
       Log.d("Facebook-Example-Friends Request", "response.length(): " + response.length()); 
       Log.d("Facebook-Example-Friends Request", "Response: " + response); 

       final JSONObject json = new JSONObject(response); 
       JSONArray d = json.getJSONArray("data"); 
       int l = (d != null ? d.length() : 0); 
       Log.d("Facebook-Example-Friends Request", "d.length(): " + l); 

       for (int i=0; i<l; i++) 
       { 
        JSONObject o = d.getJSONObject(i); 
        String n = o.getString("name"); 
        String id = o.getString("id"); 

        TextView tv = new TextView(Login.this); 
        tv.setText(n); 
        ((LinearLayout) linearLayout).addView(tv); 
       }    
      } 
      catch (JSONException e) 
      { 
       Log.w("Facebook-Example", "JSON Error in response"); 
      } 
     } 
    } 
} 
+0

当你调试,它实际上击中了好友请求侦听器的onComplete回调方法? –

+0

嘿,如果我的回答有助于解决问题,您应该将其标记为已接受的答案(点击答案旁边的复选标记) –

回答

2

我想我可能已经发现了这个问题..根据关于onComplet的文档;

 /** 
    * Called when a request completes with the given response. 
    * 
    * Executed by a background thread: do not update the UI in this method. 
    */ 

而且你要更新你的UI,所以addViews不着手在UI线程上体现。您应该建立在你的活性的方法,你可以从使用的onComplete调用runOnUIThread ..喜欢的东西这个;

runOnUiThread(new Runnable() { 
      public void run() { 
       inserFacebookNames(facebookContactNames); 
      } 
     }); 

让我知道如果这实际上修复它。我很好奇,如果是这样的原因..

+0

谢谢您的回复。它是固定的! – NewToAndroid

+0

也许你应该把这个答案标记为接受呢?您可以通过点击绿色的复选标记来完成。 – Silox

相关问题