Search code examples
javaandroidfacebookfacebook-graph-api

Retrieving facebook graph data for android native app


I'm creating a android game that integrates with facebook to retrieve their photo and first name and other information using the graph, I've found out how to retrieve their photo using the below code which works perfectly but I'm struggling to find working examples of extracting other information like first_name etc...

The code I'm using to retrieve the photo and display it in a ImageView is below.

public void showPhoto(View v) {
    try {
        ImageView MyProfilePicImageView = (ImageView)findViewById(R.id.temp);
        URL MyProfilePicURL = new URL("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="+ access_token );
        Bitmap MyprofPicBitMap = null;

        try {
            MyprofPicBitMap = BitmapFactory.decodeStream(MyProfilePicURL.openConnection().getInputStream());
            MyProfilePicImageView.setImageBitmap(MyprofPicBitMap);
        }
        catch (IOException e) {
            e.printStackTrace();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

I've searched high and low but can't seem to find information on how to retrieve the other information using the graph, other information meaning first_name etc... including here on the facebook developer website (https://developers.facebook.com/docs/reference/api/) but can't find a working example.

Can anybody show me a working example to retrieve the first_name of the user so I can display it in a textview?


Solution

  • using this url"https://graph.facebook.com/me/friends?access_token="+accessToken you can get your friends which contains thier names and ids to get info about any of them just use `"https://graph.facebook.com/"+yourFriendId+"?access_token="+accessToken' this will return json that you can parse and use
    Example

    HttpClient httpclient = new DefaultHttpClient();
    String url = "https://graph.facebook.com/me/friends?access_token=" + URLEncoder.encode(token);
    HttpGet httppost = new HttpGet(url);
    try {
                    HttpResponse response = httpclient.execute(httppost);
    
                    // to get the response string
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(
                                    response.getEntity().getContent(), "UTF-8"));
                    // used to construct the string
                    String res = "";
                    for (String line = null; (line = reader.readLine()) != null;) {
                        res += line + "\n";
    
                    }
    // here we will parse the response
                    JSONObject obj = new JSONObject(new JSONTokener(res));
                    JSONArray data = obj.getJSONArray("data");
    int len = data.length();
    for (int i = 0; i < len; i++) {
                        JSONObject currentResult = data.getJSONObject(i);
                        String name = currentResult.getString("name");
                        String icon = currentResult.getString("id");
    
                        // do what ever you want
    
                    }
    } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }