แม้ว่าไซม่อนครอสคำตอบนั้นได้รับการยอมรับและถูกต้อง แต่ฉันคิดว่าฉันอยากยกตัวอย่างเล็กน้อย (Android) ถึงสิ่งที่จะต้องทำ ฉันจะให้มันเป็นเรื่องทั่วไปเท่าที่จะทำได้ ส่วนตัวแล้วฉันเก็บของไว้ในฐานข้อมูลทำให้การโหลดราบรื่น แต่ต้องใช้ CursorAdapter และ ContentProvider ซึ่งอยู่นอกขอบเขตเล็กน้อย
ฉันมาที่นี่ด้วยตัวเองแล้วคิดว่าตอนนี้อะไร!
ปัญหา
เช่นเดียวกับผู้ใช้3594351ฉันสังเกตเห็นว่าข้อมูลเพื่อนว่างเปล่า ฉันพบสิ่งนี้โดยใช้ FriendPickerFragment ทำงานเมื่อสามเดือนที่แล้วไม่ทำงานอีกต่อไป แม้แต่ตัวอย่างของ Facebook ก็พัง ดังนั้นปัญหาของฉันคือ 'ฉันจะสร้าง FriendPickerFragment ด้วยมือได้อย่างไร?
อะไรไม่ทำงาน
ตัวเลือก # 1 จากSimon Crossยังไม่แข็งแรงพอที่จะเชิญเพื่อนมาที่แอป Simon Crossยังแนะนำไดอะล็อกการร้องขอ แต่จะอนุญาตได้เพียงห้าครั้งเท่านั้น กล่องโต้ตอบคำขอยังแสดงให้เห็นว่ามีเพื่อนคนเดียวกันในระหว่างการเข้าสู่ระบบ Facebook ไม่มีประโยชน์.
ทำงานอย่างไร (สรุป)
ตัวเลือก # 2 กับงานบางอย่าง คุณต้องทำให้แน่ใจว่าคุณปฏิบัติตามกฎใหม่ของ Facebook: 1. ) คุณเป็นเกม 2. ) คุณมีแอพ Canvas (การแสดงตนของเว็บ) 3. ) แอปของคุณลงทะเบียนกับ Facebook มันจะทำทั้งหมดบนเว็บไซต์ของนักพัฒนา Facebook ภายใต้การตั้งค่า
ในการเลียนแบบเครื่องมือเลือกเพื่อนด้วยมือในแอพของฉันฉันได้ทำสิ่งต่อไปนี้:
- สร้างกิจกรรมแท็บที่แสดงสองชิ้น แต่ละส่วนจะแสดงรายการ หนึ่งส่วนสำหรับเพื่อนที่พร้อมใช้งาน ( / ฉัน / เพื่อน ) และอีกอันสำหรับเพื่อนที่น่าเชื่อถือ ( / ฉัน / invitable_friends ) ใช้รหัสส่วนเดียวกันเพื่อแสดงทั้งสองแท็บ
- สร้าง AsyncTask ที่จะรับข้อมูลเพื่อนจาก Facebook เมื่อโหลดข้อมูลแล้วให้โยนไปยังอะแดปเตอร์ซึ่งจะแสดงค่าไปที่หน้าจอ
รายละเอียด
AsynchTask
private class DownloadFacebookFriendsTask extends AsyncTask<FacebookFriend.Type, Boolean, Boolean> {
    private final String TAG = DownloadFacebookFriendsTask.class.getSimpleName();
    GraphObject graphObject;
    ArrayList<FacebookFriend> myList = new ArrayList<FacebookFriend>();
    @Override
    protected Boolean doInBackground(FacebookFriend.Type... pickType) {
        //
        // Determine Type
        //
        String facebookRequest;
        if (pickType[0] == FacebookFriend.Type.AVAILABLE) {
            facebookRequest = "/me/friends";
        } else {
            facebookRequest = "/me/invitable_friends";
        }
        //
        // Launch Facebook request and WAIT.
        //
        new Request(
            Session.getActiveSession(),
            facebookRequest,
            null,
            HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                    FacebookRequestError error = response.getError();
                    if (error != null && response != null) {
                        Log.e(TAG, error.toString());
                    } else {
                        graphObject = response.getGraphObject();
                    }
                }
            }
        ).executeAndWait();
        //
        // Process Facebook response
        //
        //
        if (graphObject == null) {
            return false;
        }
        int numberOfRecords = 0;
        JSONArray dataArray = (JSONArray) graphObject.getProperty("data");
        if (dataArray.length() > 0) {
            // Ensure the user has at least one friend ...
            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject jsonObject = dataArray.optJSONObject(i);
                FacebookFriend facebookFriend = new FacebookFriend(jsonObject, pickType[0]);
                if (facebookFriend.isValid()) {
                    numberOfRecords++;
                    myList.add(facebookFriend);
                }
            }
        }
        // Make sure there are records to process
        if (numberOfRecords > 0){
            return true;
        } else {
            return false;
        }
    }
    @Override
    protected void onProgressUpdate(Boolean... booleans) {
        // No need to update this, wait until the whole thread finishes.
    }
    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            /*
            User the array "myList" to create the adapter which will control showing items in the list.
             */
        } else {
            Log.i(TAG, "Facebook Thread unable to Get/Parse friend data. Type = " + pickType);
        }
    }
}
ชั้น FacebookFriend ที่ฉันสร้างขึ้น
public class FacebookFriend {
    String facebookId;
    String name;
    String pictureUrl;
    boolean invitable;
    boolean available;
    boolean isValid;
    public enum Type {AVAILABLE, INVITABLE};
    public FacebookFriend(JSONObject jsonObject, Type type) {
        //
        //Parse the Facebook Data from the JSON object.
        //
        try {
            if (type == Type.INVITABLE) {
                //parse /me/invitable_friend
                this.facebookId =  jsonObject.getString("id");
                this.name = jsonObject.getString("name");
                // Handle the picture data.
                JSONObject pictureJsonObject = jsonObject.getJSONObject("picture").getJSONObject("data");
                boolean isSilhouette = pictureJsonObject.getBoolean("is_silhouette");
                if (!isSilhouette) {
                    this.pictureUrl = pictureJsonObject.getString("url");
                } else {
                    this.pictureUrl = "";
                }
                this.invitable = true;
            } else {
                // Parse /me/friends
                this.facebookId =  jsonObject.getString("id");
                this.name = jsonObject.getString("name");
                this.available = true;
                this.pictureUrl = "";
            }
            isValid = true;
        } catch (JSONException e) {
            Log.w("#", "Warnings - unable to process Facebook JSON: " + e.getLocalizedMessage());
        }
    }
}