2012-07-28 30 views
0

你好我neeed找回用户名,并从Facebook的个人资料的通知,这是我的Facebook连接器高代码:如何在android中检索Facebook用户名和通知?

import com.FeedPass.SessionEvents.AuthListener; 
import com.FeedPass.SessionEvents.LogoutListener; 

public class FacebookConnector { 
private Facebook facebook = null; 
private Context context; 
private String[] permissions; 
private Handler mHandler; 
private Activity activity; 
private SessionListener mSessionListener = new SessionListener(); 
private String _accessToken; 

public FacebookConnector(String appId,Activity activity,Context context,String[] permissions){ 
    this.facebook = new Facebook(appId); 

    SessionStore.restore(facebook, context); 

    SessionEvents.addAuthListener(mSessionListener); 
    SessionEvents.addLogoutListener(mSessionListener); 
    this.context=context; 
    this.permissions=permissions; 
    this.mHandler = new Handler(); 
    this.activity=activity; 
} 

public void login() { 
    if (!facebook.isSessionValid()) { 
     facebook.authorize(this.activity, this.permissions,new LoginDialogListener()); 
    } 
} 

public void logout() { 
    SessionEvents.onLogoutBegin(); 
    AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(this.facebook); 
    asyncRunner.logout(this.context, new LogoutRequestListener()); 
} 

public void postMessageOnWall(String msg) { 
    if (facebook.isSessionValid()) { 
     Bundle parameters = new Bundle(); 
     parameters.putString("message", msg); 
     try { 
      String response = facebook.request("me/feed", parameters,"POST"); 
      System.out.println(response); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     login(); 
    } 
} 

public String getNotifications(){ 
    String result = null; 
    if(facebook.isSessionValid()){ 
     Bundle bundle = new Bundle(); 
     bundle.putString(Facebook.TOKEN, _accessToken); 
     try{ 
      result = facebook.request("me/notifications", bundle, "GET"); 
     } 
     catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 
    else{ 
     login(); 
    } 
    return result; 
} 

private final class LoginDialogListener implements DialogListener { 
    public void onComplete(Bundle values) { 
     SessionEvents.onLoginSuccess(); 
    } 

    public void onFacebookError(FacebookError error) { 
     SessionEvents.onLoginError(error.getMessage()); 
    } 

    public void onError(DialogError error) { 
     SessionEvents.onLoginError(error.getMessage()); 
    } 

    public void onCancel() { 
     SessionEvents.onLoginError("Action Canceled"); 
    } 
} 

public class LogoutRequestListener extends BaseRequestListener { 
    public void onComplete(String response, final Object state) { 
     // callback should be run in the original thread, 
     // not the background thread 
     mHandler.post(new Runnable() { 
      public void run() { 
       SessionEvents.onLogoutFinish(); 
      } 
     }); 
    } 
} 

private class SessionListener implements AuthListener, LogoutListener { 

    public void onAuthSucceed() { 
     SessionStore.save(facebook, context); 
    } 

    public void onAuthFail(String error) { 
    } 

    public void onLogoutBegin() {   
    } 

    public void onLogoutFinish() { 
     SessionStore.clear(context); 
    } 
} 

public Facebook getFacebook() { 
    return this.facebook; 
} 
} 

,你可以看到我有找回用户通知的方法,但我不知道它是工作,任何人都知道如何做到这一点是以一种简单的方式?

回答

1

下面的代码可用于检索的Facebook用户名:

public static String getName() { 
     String response; 
     String name = ""; 
     try { 
      response = ParseFacebookUtils.getFacebook().request("me"); 
      JSONObject json = Util.parseJson(response); 
      name = json.getString("first_name"); 
     } 
     catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     catch (FacebookError e) { 
      e.printStackTrace(); 
     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return name; 
    } 
+0

使用此代码会使我的应用程序崩溃:S有什么建议吗? – 2012-07-31 15:59:32

+0

用户登录后才能调用该方法 – 2012-07-31 17:19:07

+0

这是我在logcat上遇到的错误,仍然没有运气.. '07-31 13:08:24.578:E/AndroidRuntime(4048): \t at com.facebook.android.Util.openUrl(Util.java:215) 07-31 13:08:24.578:E/AndroidRuntime(4048):\t at com.facebook.android.Facebook.request(Facebook.java :751) 07-31 13:08:24.578:E/AndroidRuntime(4048):\t at com.facebook.android.Facebook.request(Facebook.java:688) 07-31 13:08:24.578:E/AndroidRuntime(4048):\t at com.FeedPass.FacebookConnector.getName(FacebookConnector.java:77) 07-31 13:08:24.578:E/AndroidRuntime(4048):\t at' – 2012-07-31 19:21:20

1

你可以从这个

String User_name = null; 
try { 
    User_name = new JSONObject(facebook.request("username")); 

} catch (MalformedURLException e) { 
    e.printStackTrace(); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
0

得到Facebook的用户名这是你如何找回notifications.Even这是一个老问题。我会张贴它,以防有人发现它有帮助

private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
if (state.isOpened()) { 
    InboxMessage.setVisibility(View.VISIBLE); 

    new Request(session,"/me/notifications", null,HttpMethod.GET,new Request.Callback() { 

       public void onCompleted(Response response) { 
        GraphObject object = response.getGraphObject(); 
         if (object != null) { 
          InboxMessage.setText(object.getProperty("data").toString()); 
         } else { 
          InboxMessage.setText("Notifications returns null"); 
         } 
       } 
      } 
     ).executeAsync(); 
} else { 
    InboxMessage.setVisibility(View.INVISIBLE); 
    Log.i(TAG, "Logged out..."); 
} 
} 
相关问题