4

我正在构建一个 android 应用程序,我在其中使用 facebook android sdk 登录到 facebook。登录部分进展顺利,但现在我在登录 facebook 后挂起如何在 gridview 中显示相册。当所有相册都显示在网格视图中时,当我单击任何一个相册时,它必须打开每个图像左上角的复选框。因此,当我选中特定图像的复选框时,它必须保存在设备 sd 卡和其他功能中,选中的图像必须在表格布局中获得拼贴,每个表格行都有表格行和背景颜色。我会附上一些文件,这样你就可以知道我想要什么。希望得到您的积极答复。所以请发送相同的完整源代码。我从过去 20 天开始尝试,但我没有成功 :-( 希望很快得到答复。

enter code here公共类专辑扩展活动{私有静态最终字符串APP_ID =“1403104849907306”;私人脸书脸书=新脸书(APP_ID);

// HOLD THE URL TO MAKE THE API CALL TO
private String URL;

// STORE THE PAGING URL
private String pagingURL;

// FLAG FOR CURRENT PAGE
int current_page = 1;

// BOOLEAN TO CHECK IF NEW FEEDS ARE LOADING
Boolean loadingMore = true;

Boolean stopLoadingData = false;

 ArrayList<getAlbums> arrAlbums = null;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);

 class getAlbumsData extends AsyncTask<Void, Void, Void> {

        LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);

        @Override
        protected void onPreExecute() {

            // SHOW THE PROGRESS BAR (SPINNER) WHILE LOADING ALBUMS
            linlaHeaderProgress.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            // CHANGE THE LOADING MORE STATUS TO PREVENT DUPLICATE CALLS FOR
            // MORE DATA WHILE LOADING A BATCH
            loadingMore = true;

            // SET THE INITIAL URL TO GET THE FIRST LOT OF ALBUMS
           URL = "https://graph.facebook.com/" + "100005623513169"
                    + "/photos&access_token="
                    + facebook.getAccessToken() + "?limit=10"; 


            try {

                HttpClient hc = new DefaultHttpClient();
                HttpGet get = new HttpGet(URL);
                HttpResponse rp = hc.execute(get);

                if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    String queryAlbums = EntityUtils.toString(rp.getEntity());

                    JSONObject JOTemp = new JSONObject(queryAlbums);

                    JSONArray JAAlbums = JOTemp.getJSONArray("data");

                    // IN MY CODE, I GET THE NEXT PAGE LINK HERE

                    if (JAAlbums.length() == 0) {
                        stopLoadingData = true;
                        Runnable run = new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),
                                        "No more Albums", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        };
                        Albums.this.runOnUiThread(run);

                    } else {
                        // PAGING JSONOBJECT
                        if (JOTemp.has("paging"))   {
                            JSONObject JOPaging = JOTemp.getJSONObject("paging");

                            if (JOPaging.has("next")) {
                                String initialpagingURL = JOPaging
                                        .getString("next");

                                String[] parts = initialpagingURL.split("limit=10");
                                String getLimit = parts[1];

                                pagingURL = "https://graph.facebook.com/"
                                        + "100005623513169" + "/albums&access_token="
                                        + facebook.getAccessToken()
                                        + "?limit=10" + getLimit;

                            } else {
                                stopLoadingData = true;
                                Runnable run = new Runnable() {

                                    @Override
                                    public void run() {
                                        Toast.makeText(getApplicationContext(),
                                                "No more Albums",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                };
                                Albums.this.runOnUiThread(run);
                            }
                        } else {
                            stopLoadingData = true;
                            Runnable run = new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),
                                            "No more Albums",
                                            Toast.LENGTH_SHORT).show();
                                }
                            };
                            Albums.this.runOnUiThread(run);

                        }

                        getAlbums albums;

                        for (int i = 0; i < JAAlbums.length(); i++) {
                            JSONObject JOAlbums = JAAlbums.getJSONObject(i);

                            if (JOAlbums.has("link")) {

                                albums = new getAlbums();

                                // GET THE ALBUM ID
                                if (JOAlbums.has("id")) {
                                    albums.setAlbumID(JOAlbums.getString("id"));
                                } else {
                                    albums.setAlbumID(null);
                                }

                                // GET THE ALBUM NAME
                                if (JOAlbums.has("name")) {
                                    albums.setAlbumName(JOAlbums
                                            .getString("name"));
                                } else {
                                    albums.setAlbumName(null);
                                }

                                // GET THE ALBUM COVER PHOTO
                                if (JOAlbums.has("cover_photo")) {
                                    albums.setAlbumCover("https://graph.facebook.com/"
                                            + JOAlbums.getString("cover_photo")
                                            + "/picture?type=normal"
                                            + "&access_token="
                                            + facebook.getAccessToken());
                                } else {
                                    albums.setAlbumCover("https://graph.facebook.com/"
                                            + JOAlbums.getString("id")
                                            + "/picture?type=album"
                                            + "&access_token="
                                            + facebook.getAccessToken());
                                }

                                // GET THE ALBUM'S PHOTO COUNT
                                if (JOAlbums.has("count")) {
                                    albums.setAlbumPhotoCount(JOAlbums
                                            .getString("count"));
                                } else {
                                    albums.setAlbumPhotoCount("0");
                                }

                                arrAlbums.add(albums);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            ListView lv = null;
            ListAdapter adapter = null;
            // SET THE ADAPTER TO THE LISTVIEW
            lv.setAdapter(adapter);

            // CHANGE THE LOADING MORE STATUS
            loadingMore = false;

            // HIDE THE PROGRESS BAR (SPINNER) AFTER LOADING ALBUMS
            linlaHeaderProgress.setVisibility(View.GONE);
        }

    }

  class loadMoreAlbums extends AsyncTask<Void, Void, Void> {

      LinearLayout linlaProgressBar = (LinearLayout)findViewById(R.id.linlaProgressBar);

        @Override
        protected void onPreExecute() {
            // SHOW THE BOTTOM PROGRESS BAR (SPINNER) WHILE LOADING MORE ALBUMS
            linlaProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(Void... params) {

            // SET LOADING MORE "TRUE"
            loadingMore = true;

            // INCREMENT CURRENT PAGE
            current_page += 1;

            // Next page request
            URL = pagingURL;
            // Log.e("NEW URL", URL);

            try {

                HttpClient hc = new DefaultHttpClient();
                HttpGet get = new HttpGet(URL);
                HttpResponse rp = hc.execute(get);

                if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    String queryAlbums = EntityUtils.toString(rp.getEntity());
                    // Log.e("RESULT", queryAlbums);

                    JSONObject JOTemp = new JSONObject(queryAlbums);

                    JSONArray JAAlbums = JOTemp.getJSONArray("data");

                    if (JAAlbums.length() == 0) {
                        stopLoadingData = true;

                        Runnable run = new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),
                                        "No more Albums", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        };
                        Albums.this.runOnUiThread(run);

                    } else {
                        // PAGING JSONOBJECT
                        JSONObject JOPaging = JOTemp.getJSONObject("paging");
                        // Log.e("PAGING", JOPaging.toString());

                        if (JOPaging.has("next")) {
                            String initialpagingURL = JOPaging
                                    .getString("next");
                            // Log.e("initialpagingURL", initialpagingURL);

                            String[] parts = initialpagingURL.split("limit=10");
                            String getLimit = parts[1];

                            pagingURL = "https://graph.facebook.com/"
                                    + "100005623513169" + "/albums&access_token="
                                    + facebook.getAccessToken()
                                    + "?limit=10" + getLimit;
                            // Log.e("NEW PAGING URL", pagingURL);

                        } else {
                            stopLoadingData = true;
                            Runnable run = new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),
                                            "No more Albums available",
                                            Toast.LENGTH_SHORT).show();
                                }
                            };
                            Albums.this.runOnUiThread(run);
                        }

                        getAlbums albums;

                        for (int i = 0; i < JAAlbums.length(); i++) {
                            JSONObject JOAlbums = JAAlbums.getJSONObject(i);
                            // Log.e("INDIVIDUAL ALBUMS", JOAlbums.toString());

                            if (JOAlbums.has("link")) {

                                albums = new getAlbums();

                                // GET THE ALBUM ID
                                if (JOAlbums.has("id")) {
                                    albums.setAlbumID(JOAlbums.getString("id"));
                                } else {
                                    albums.setAlbumID(null);
                                }

                                // GET THE ALBUM NAME
                                if (JOAlbums.has("name")) {
                                    albums.setAlbumName(JOAlbums
                                            .getString("name"));
                                } else {
                                    albums.setAlbumName(null);
                                }

                                // GET THE ALBUM COVER PHOTO
                                if (JOAlbums.has("cover_photo")) {
                                    albums.setAlbumCover("https://graph.facebook.com/"
                                            + JOAlbums.getString("cover_photo")
                                            + "/picture?type=album"
                                            + "&access_token="
                                            + facebook
                                                    .getAccessToken());
                                } else {
                                    albums.setAlbumCover("https://graph.facebook.com/"
                                            + JOAlbums.getString("id")
                                            + "/picture?type=album"
                                            + "&access_token="
                                            + facebook
                                                    .getAccessToken());
                                }

                                // GET THE ALBUM'S PHOTO COUNT
                                if (JOAlbums.has("count")) {
                                    albums.setAlbumPhotoCount(JOAlbums
                                            .getString("count"));
                                } else {
                                    albums.setAlbumPhotoCount("0");
                                }

                                arrAlbums.add(albums);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            AdapterView<ListAdapter> lv = null;
            // get listview current position - used to maintain scroll position
            int currentPosition = lv.getFirstVisiblePosition();

            // APPEND NEW DATA TO THE ARRAYLIST AND SET THE ADAPTER TO THE
            // LISTVIEW
            AlbumsAdapter adapter = new AlbumsAdapter(Albums.this, arrAlbums);
            lv.setAdapter(adapter);

        // Setting new scroll position
        ((ListView) lv).setSelectionFromTop(currentPosition + 1, 0);

            // SET LOADINGMORE "FALSE" AFTER ADDING NEW FEEDS TO THE EXISTING
            // LIST
        loadingMore = false;

            // HIDE THE BOTTOM PROGRESS BAR (SPINNER) AFTER LOADING MORE ALBUMS
        linlaProgressBar.setVisibility(View.GONE);
        }

    }
}

}

4

1 回答 1

3

您应该使用适用于 Android 的 Facebook SDK 3.0+,并利用他们的 Request.executeAsync() 方法。将用户登录到应用程序并检索Session对象后,您可以在 Graph API 上发出请求。这是我请求用户相册的方法:

/**
 * Makes a request for user's photo albums from Facebook Graph API
 * @param session
 */
private void fetchAlbumsFromFB(Session session) {

    // callback after Graph API response with user object
    Request.GraphUserCallback graphUserCallback;
    graphUserCallback = new Request.GraphUserCallback() {
        @Override
        public void onCompleted(GraphUser user, Response response) {
            JSONObject jsonObject = null;
            if (user != null)
                jsonObject = user.getInnerJSONObject();

            ImageAdapter.getInstance().setPhotoAlbums(jsonObject);
        }
    };

    // assign callback to final instance variable in inner class
    final Request.GraphUserCallback finalCallback = graphUserCallback;
    Request.Callback wrapperCallback = new Request.Callback() {
        @Override
        public void onCompleted(Response response) {
            finalCallback.onCompleted(response.getGraphObjectAs(GraphUser.class), response);
        }
    };

    // make a new async request
    Bundle params = new Bundle();
    params.putString("fields", "photos");
    Request request = new Request(session, "me/albums", params, null, wrapperCallback);
    request.executeAsync();
}

然后你可以像这样解析 JSON 响应:

/**
 * Updates the array with cover photos from albums
 *
 * @param photosObject
 */
public void setPhotoAlbums(JSONObject photosObject) {
    try {
        mPhotoAlbumsArray = photosObject.getJSONArray("data");
        coverPhotosArr = new String[mPhotoAlbumsArray.length()];
        for (int i = 0; i < mPhotoAlbumsArray.length(); i++) {
            JSONObject obj1 = mPhotoAlbumsArray.getJSONObject(i);
            JSONObject obj2 = (JSONObject) obj1.get("photos");
            JSONArray obj3 = obj2.getJSONArray("data");
            JSONObject obj4 = obj3.getJSONObject(0);
            String picture = String.valueOf(obj4.get("picture"));
            coverPhotosArr[i] = picture;
        }

        // notify GridView's adapter that underlying data has been changed
        notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
于 2014-01-13T18:56:34.673 回答