开发者

getting facebook events

I am trying to get a list of facebook events with the name events host and a start time. I am using the official facebook sdk for android.

But for some reason whenever I click get event in the application it will load then retrieve no data.

Any help at all would be brilliant I have been stuck on this for weeks

My code:

public static final String APP_ID = "";

private static final String[] PERMISSIONS =
    new String[]{ "offline_access", "read_stream", 
            "publish_stream","create_event","user_events","friends_events",
            "publish_checkins", "friends_checkins","read_friendlists" };

private TextView mText;
private Handler mHandler = new Handler();
private ProgressDialog mSpinner;
private final ArrayList<Events> events = new ArrayList<Events>();
private EventsArrayAdapter eventsArrayAdapter;
private ListView listView;
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;

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

    // Make sure the app client_app has been set
    if (APP_ID == null) {
        Util.showAlert(this,
        "Warning", "Facebook Applicaton ID must be set...");
    }

    // Initialize the content view
    setContentView(R.layout.main);
    // Get the status text line resource
    mText = (TextView) workdammit.this.findViewById(R.id.txt);

    // Setup the ListView Adapter that is loaded when selecting "get events"
    listView = (ListView) findViewById(R.id.eventsview);
    eventsArrayAdapter = new EventsArrayAdapter(this, R.layout.rowlayout, events);
    listView.setAdapter(eventsArrayAdapter);

    // Define a spinner used when loading the events over the network
    mSpinner = new ProgressDialog(listView.getContext());
    mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mSpinner.setMessage("Loading...");

    // Initialize the Facebook session
    mFacebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);

}

//////////////////////////////////////////////////////////////////////

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  Log.d("FB Sample App", "onActivityResult(): " + requestCode);
  mFacebook.authorizeCallback(requestCode, resultCode, data);
}

//////////////////////////////////////////////////////////////////////
// Get events request listener
//////////////////////////////////////////////////////////////////////

/**
 * eventsRequestListener implements a request lister/callback
 *  for "get events" requests
 */
public class EventsRequestListener implements
        com.facebook.android.AsyncFacebookRunner.RequestListener {

    /**
     * Called when the request to get events has been completed.
     * Retrieve and parse and display the JSON stream.
     */
    public void onComplete(final String response) {
        mSpinner.dismiss();
        try {
            // process the response here: executed in background thread
            Log.d("Facebook-Example-events Request", "response.length(): " + response.length());
            Log.d("Facebook-Example-events 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-events Request", "d.length(): " + l);

            for (int i=0; i<l; i++) {
                JSONObject o = d.getJSONObject(i);
                String n = o.getString("name");
                String h = o.getString("host");
                String L = o.getString("location");
                Events f = new Events();
                f.host = h;
                f.name = n;
                f.location =L;
                events.add(f);
            }

            // Only the original owner thread can touch its views
            workdammit.this.runOnUiThread(new Runnable() {开发者_运维技巧
                public void run() {
                    eventsArrayAdapter = new EventsArrayAdapter(
                            workdammit.this, R.layout.rowlayout, events);
                    listView.setAdapter(eventsArrayAdapter);
                    eventsArrayAdapter.notifyDataSetChanged();
                }
            });
        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        }
    }



    @Override
    public void onComplete(String response, Object state) {
        mSpinner.dismiss();

    }

    @Override
    public void onIOException(IOException e, Object state) {
        mSpinner.dismiss();

    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        mSpinner.dismiss();

    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        mSpinner.dismiss();

    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
        mSpinner.dismiss();

    }
}

//////////////////////////////////////////////////////////////////////
// Wall Post request listener
//////////////////////////////////////////////////////////////////////

/**
 * WallPostRequestListener implements a request lister/callback
 *  for "wall post requests"
 */
public class WallPostRequestListener implements
        com.facebook.android.AsyncFacebookRunner.RequestListener {

    /**
     * Called when the wall post request has completed
     */
    public void onComplete(final String response) {
        Log.d("Facebook-Example", "Got response: " + response);
    }


    @Override
    public void onComplete(String response, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onIOException(IOException e, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
        // TODO Auto-generated method stub

    }

}

//////////////////////////////////////////////////////////////////////
// Wall post dialog completion listener
//////////////////////////////////////////////////////////////////////

/**
 * WallPostDialogListener implements a dialog lister/callback
 */
public class WallPostDialogListener implements
        com.facebook.android.Facebook.DialogListener {

    /**
     * Called when the dialog has completed successfully
     */
    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            Log.d("FB Sample App", "Dialog Success! post_id=" + postId);
            mAsyncRunner.request(postId, new WallPostRequestListener());
        } else {
            Log.d("FB Sample App", "No wall post made");
        }
    }

    @Override
    public void onCancel() {
        // No special processing if dialog has been canceled
    }

    @Override
    public void onError(DialogError e) {
        // No special processing if dialog has been canceled
    }

    @Override
    public void onFacebookError(FacebookError e) {
        // No special processing if dialog has been canceled
    }
}

/////////////////////////////////////////////////////////
// Login / Logout Listeners
/////////////////////////////////////////////////////////

/**
 * Listener for login dialog completion status
 */
private final class LoginDialogListener implements
        com.facebook.android.Facebook.DialogListener {

    /**
     * Called when the dialog has completed successfully
     */
    public void onComplete(Bundle values) {
        // Process onComplete
        Log.d("FB Sample App", "LoginDialogListener.onComplete()");
        // Dispatch on its own thread
        mHandler.post(new Runnable() {
            public void run() {
                mText.setText("Facebook login successful. Press Menu...");
            }
        });
    }

    /**
     *
     */
    public void onFacebookError(FacebookError error) {
        // Process error
        Log.d("FB Sample App", "LoginDialogListener.onFacebookError()");
    }

    /**
     *
     */
    public void onError(DialogError error) {
        // Process error message
        Log.d("FB Sample App", "LoginDialogListener.onError()");
    }

    /**
     *
     */
    public void onCancel() {
        // Process cancel message
        Log.d("FB Sample App", "LoginDialogListener.onCancel()");

} }

/**
 * Listener for logout status message
 */
private class LogoutRequestListener implements RequestListener {

    /** Called when the request completes w/o error */
    public void onComplete(String response) {

        // Only the original owner thread can touch its views
        workdammit.this.runOnUiThread(new Runnable() {
            public void run() {
                mText.setText("Thanks for using FB Sample App. Bye bye...");
                events.clear();
                eventsArrayAdapter.notifyDataSetChanged();
            }
        });

        // Dispatch on its own thread
        mHandler.post(new Runnable() {
            public void run() {
            }
        });
    }


    @Override
    public void onComplete(String response, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onIOException(IOException e, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
        // TODO Auto-generated method stub

    }

}

///////////////////////////////////////////////////////////////////
// Menu handlers
///////////////////////////////////////////////////////////////////

/**
 * Invoked at the time to create the menu
 * @param menu is the menu to create
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

/**
 * Invoked when preparing to display the menu
 * @param menu is the menu to prepare
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    MenuItem loginItem = menu.findItem(R.id.login);
    MenuItem postItem = menu.findItem(R.id.wallpost);
    MenuItem geteventsItem = menu.findItem(R.id.getevent);
    if (mFacebook.isSessionValid()) {
        loginItem.setTitle("Logout");
        postItem.setEnabled(true);
        geteventsItem.setEnabled(true);
    } else {
        loginItem.setTitle("Login");
        postItem.setEnabled(false);
        geteventsItem.setEnabled(false);
    }
    loginItem.setEnabled(true);
    return super.onPrepareOptionsMenu(menu);
}

/**
 * Invoked when a menu item has been selected
 * @param item is the selected menu items
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        // Login/logout toggle
        case R.id.login:
            // Toggle the button state.
            //  If coming from login transition to logout.
            if (mFacebook.isSessionValid()) {
                AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook);
                asyncRunner.logout(this.getBaseContext(), new LogoutRequestListener());
            } else {
                // Toggle the button state.
                //  If coming from logout transition to login (authorize).
                mFacebook.authorize(this, PERMISSIONS, new LoginDialogListener());
            }
            break;

        // Wall Post
        case R.id.wallpost: // Wall Post
            mFacebook.dialog(workdammit.this, "stream.publish", new WallPostDialogListener());
            break;

        // Get Friend's List
        case R.id.getevents: // Wall Post
            // Get the authenticated user's events
            mSpinner.show();

            mAsyncRunner.request("me/events", new EventsRequestListener());
            break;

        default:
            return false;

    }
    return true;
}

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜