Sample code for 30+ languages & platforms
Java

Iterate Pages in Feed

See more Facebook Examples

Demonstrates how to read the next page in the user's Facebook feed, and iterates through all of the pages in the Facebook feed.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.OAuth2;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Url;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This example assumes a previously obtained an access token
        OAuth2 oauth2 = new OAuth2();
        oauth2.setAccessToken("FACEBOOK-ACCESS-TOKEN");

        Rest rest = new Rest();

        // Connect to Facebook and send the following GET request:
        String responseJson;
        try {
            rest.connect("graph.facebook.com", 443, true, true);

            // Provide the authentication credentials (i.e. the access key)
            rest.setAuthOAuth2(oauth2);

            // Gets the 1st page in the user's feed.
            responseJson = rest.fullRequestNoBody("GET", "/v2.7/me/feed");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json = new JsonObject();
        json.setEmitCompact(false);
        json.load(responseJson);

        // See Parsing the Facebook User Feed for code showing how to parse the JSON feed content.
        //

        Url nextUrl = new Url();

        // Get the URL for the next page in the feed.
        String nextUrlStr = json.stringOf("paging.next");
        while (json.getLastMethodSuccess()) {
            System.out.println("Next page URL: " + nextUrlStr);

            nextUrl.parseUrl(nextUrlStr);

            // Prepare for getting the next page in the feed.
            // We can continue using the same REST object.
            // If already connected, we'll continue using the existing connection.
            // Otherwise, a new connection will automatically be made if needed.
            rest.clearAllQueryParams();
            rest.addQueryParams(nextUrl.getQuery());

            try {
                responseJson = rest.fullRequestNoBody("GET", "/v2.7/me/feed");
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            json.load(responseJson);
            // See Parsing the Facebook User Feed for code showing how to parse the JSON feed content.

            // Get the URL for the next page.
            nextUrlStr = json.stringOf("paging.next");
        }

        System.out.println("No more pages in the feed.");
    }
}