开发者

Facebook Single-Sign-On for Android Doesn't Run OnComplete()

I'm new to both Android and Facebook and am in the middle of going through the tutorials and have written a simple app to fetch a user's friends and display their profile picures in a GridView. When I run my program using only the WebView dialog to authorize users, everything works fine. However, when the Facebook app is installed and the user is already signed in, the authorize method uses Single-Sign-On, and the OnComplete() method of the dialog listener that was passed in never gets called. Since the authorize method is asynchronous, I don't think I can just put code after the authorize method. How do I remedy this? An AuthListener of some sort? I'm sure there's a simple solution, but the Facebook documentation is pretty sparse and the examples uses much more complicated methods of authorizing users and storing their info. Here's the code for the main Activity Class:

package com.jackson;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

/**
 * DESPERATELY needs Facebook batch requests.
 */
public class GridViewFbFriends extends Activity {

    private static final String APP_ID = "121897711227945";
    private static fin开发者_如何学编程al int FRIEND_LIMIT = 25;

    private Facebook mFacebook;
    private Drawable[] mFriendImages;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mFacebook = new Facebook(APP_ID);

        mFacebook.authorize(this, new DialogListener() {
            @Override
            public void onComplete(Bundle values) {
                getFriendImages();
                setUpUI();
            }

            @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);
    }

    private void setUpUI(){
        //set up the image adapter
        ImageAdapter imageAdapter = new ImageAdapter(this);
        imageAdapter.setDrawableImages(mFriendImages);

        //set up the gridview
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(imageAdapter);
    }

    private void getFriendImages(){

        //get the user's list of friends
        String[] friendIDs = null;
        try{
            JSONObject json = new JSONObject(mFacebook.request("me/friends"));
            JSONArray jsonArray = new JSONArray(json.getString("data"));
            if(FRIEND_LIMIT < jsonArray.length()) friendIDs = new String[FRIEND_LIMIT];
            else friendIDs = new String[jsonArray.length()];
            for(int i=0; i < friendIDs.length; i++)
                friendIDs[i] = jsonArray.getJSONObject(i).getString("id");
        }
        catch(JSONException e){
            Log.d("JSON Error: ", e.getMessage());
            e.printStackTrace();
        }
        catch(IOException e){
            Log.d("Error getting friends: ", e.getMessage());
            e.printStackTrace();
        }
        catch (NullPointerException e) {
            Log.d("Friend IDs Null: ", e.getMessage());
            e.printStackTrace();
        }

        //get each friend's picture
        try {
            mFriendImages = new Drawable[friendIDs.length];
            for(int i=0; i<mFriendImages.length; i++){
                URL url = new URL("http://graph.facebook.com/"+friendIDs[i]+"/picture");
                mFriendImages[i] = Drawable.createFromStream(url.openStream(), null);
            }
        } catch (MalformedURLException e) {
            Log.d("Error getting friends: ", e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("Error getting friends: ", e.getMessage());
            e.printStackTrace();
        }
          catch (NullPointerException e) {
            Log.d("Friend IDs null: ", e.getMessage());
            e.printStackTrace();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜