如何在Android应用程序中获取Facebook相册和照片?

时间:2014-01-22 09:08:11

标签: java android facebook facebook-graph-api

我需要在第一个屏幕上显示Facebook专辑列表,并需要在下一个屏幕中显示相应的专辑图像。我使用以下代码进行用户身份验证和数据解析。在此代码中,用户信息和朋友列表显示被罚款(“/我”和“我/朋友”),当我试图获取用户相册和照片时,它显示以下消息,

{Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[]}}, error: null, isFromCache:false}

我可能知道,问题将发生在什么地方和哪里,我正在使用正确的方式获取相册和照片?请分享您的建议。

AlbumGetMainActivity.Java

public class AlbumGetMainActivity extends Activity {
    Button loginBtn;
    ListView albumList;
    private Session.StatusCallback statusCallback = new SessionStatusCallback();
    Session session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_get_main);
        albumList = (ListView) findViewById(R.id.album_list);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        // StrictMode.ThreadPolicy policy = new
        // StrictMode.ThreadPolicy.Builder()
        // .permitAll().build();
        // StrictMode.setThreadPolicy(policy);
        session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(AlbumGetMainActivity.this,
                        null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(AlbumGetMainActivity.this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                Session.OpenRequest openRequest = null;
                openRequest = new Session.OpenRequest(AlbumGetMainActivity.this);
                if (openRequest != null) {
                    openRequest
                            .setDefaultAudience(SessionDefaultAudience.FRIENDS);
                    openRequest.setPermissions(Arrays.asList("user_photos"));
                    openRequest
                            .setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                    session.openForRead(new Session.OpenRequest(
                            AlbumGetMainActivity.this)
                            .setCallback(statusCallback));
                }
            }
        }
        loginBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    Session.setActiveSession(session);
                    if (session.getState().equals(
                            SessionState.CREATED_TOKEN_LOADED)) {
                        Session.OpenRequest openRequest = null;
                        openRequest = new Session.OpenRequest(
                                AlbumGetMainActivity.this);
                        if (openRequest != null) {
                            openRequest
                                    .setDefaultAudience(SessionDefaultAudience.FRIENDS);
                            openRequest.setPermissions(Arrays
                                    .asList("user_photos"));
                            openRequest
                                    .setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                            session.openForRead(new Session.OpenRequest(
                                    AlbumGetMainActivity.this)
                                    .setCallback(statusCallback));
                        }
                    } else {
                        Session.openActiveSession(AlbumGetMainActivity.this,
                                true, statusCallback);
                    }
                    // Log.e("Finished", "FB authenticiation");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            updateView();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    public void updateView() {
        try {
            session = Session.getActiveSession();
            if (session != null && session.isOpened()) {
// Here i gave "/me" or "me/friends" its working fine
                new Request(session, "me/albums", null, HttpMethod.GET,
                        new Request.Callback() {
                            public void onCompleted(Response response) {
                                Log.e("Updated Response call", response + "");
                            }
                        }).executeAsync();
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Helper method
     */
    private static <T extends GraphObject> List<T> typedListFromResponse(
            Response response, Class<T> clazz) {
        GraphMultiResult multiResult = response
                .getGraphObjectAs(GraphMultiResult.class);
        if (multiResult == null) {
            return null;
        }

        GraphObjectList<GraphObject> data = multiResult.getData();
        if (data == null) {
            return null;
        }

        return data.castToListOf(clazz);
    }

    @Override
    public void onStop() {
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.album_get_main, menu);
        return true;
    }

}

0 个答案:

没有答案