简体   繁体   中英

How can I pass data returned from Facebook when Request.callback onCompleted() method is called, to my fragment in Android?

I'm currently trying to integrate Facebook into my Android application. I've had no problems getting the app to connect and authenticate. But I'm having a little trouble understanding how I can handle data once the onCompleted(Response response) callback method is executed. The method below works:

private void onSessionStateChange(Session session, SessionState state,
        Exception exception) {

    if (state.isOpened()) {

        Log.i(TAG, "Logged in...");

        new Request(session, "/me", null, HttpMethod.GET,
                new Request.Callback() {

                    @Override
                    public void onCompleted(Response response) {

                        GraphObject graphObject = response.getGraphObject();
                        JSONObject data = graphObject.getInnerJSONObject();

                        Log.i(TAG, "My DETAILS: ");

                        try {
                            Log.i(TAG, "ID: " + data.getLong("id"));
                            Log.i(TAG, "Name: " + data.getString("name"));
                            Log.i(TAG, "Email: " + data.getString("email"));
                            Log.i(TAG,
                                    "Gender: " + data.getString("gender"));

                        } catch (JSONException e) {

                            e.printStackTrace();
                        }

                    }
                }).executeAsync();

    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

When I run my application, Facebook authenticates, the data is retrieved and successfully outputs to the Logcat window. However, I'm at a loss to understand how I can pass the JSONObject back to my fragment for further processing.
Most examples I've looked at online simply set the JSONObject content to views in the fragment, or even less helpful simply say /* Handle response here */ or something similar. I have another similar method where I want to get a profile image url and download the image, but I can't get the url back to my fragment for further processing. Should I do something like develop a runnable class that accepts a JSONObject as a parameter and start a separate thread from the onCompleted() method to process it the way I want?

My current goal is to get a list of the users friends who use my app and save their profile pictures for use within the app. Am I going about this the wrong way?

SO if I understand you properly, you are getting all data, you are able to parse the JSON but you are not able to pass the data to your other fragment? Why dont you write to a file, which can be accessible from anywhere?

Why do you want to "DOWNLOAD" the images, that will increase your processing time. Just use this URL: https://graph.facebook.com/"+uid.trim()+"/picture?type=normal Where uid is your users id. Use this in Conjunction with Universal Image Loader to asynchronously load your images in image view. You save your time - you save a headache of manually caching files or saving them on the SD.

But bro, the problem here is that Facebook will stop support to the API you are using by April of 2015. Start porting your app to use the latest facebook API; which however is not so useful in getting users information. Cheers and keep on coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM