adrian
adrian

Reputation: 4594

retrieve in android application user's friends

I have android application in which for a logged facebook user to retrieve in your android application all his friends in a similiar way with this picture: http://i55.tinypic.com/wv9vzb.png (this is taken from iphone but I want something similar to this).

I've integrated facebook into my application using facebook-sdk.

If someone could point me into the right direction I would be gratefull.Thanks!

UPDATE:

I tried loading the URL with your code :

    Bitmap bitmap=null;
    Log.d("We are:","Loading Picture");
    imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";

    try{
        bitmap=BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
    }catch(Exception e){

        Log.d("And:","Loading Picture FAILED");
        e.printStackTrace();
}

And the logcat looks like this:

java.net.SocketTimeoutException: The operation timed out
     at org.apache.harmony.luni.platform.OSNetworkSystem.receiveStreamImpl(Native Method)
     at org.apache.harmony.luni.platform.OSNetworkSystem.receiveStream(OSNetworkSystem.java:478)
    at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:565)
    at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:61)
   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.readln(HttpURLConnection.java:1178)
  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.readServerResponse(HttpURLConnection.java:1250)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.sendRequest(HttpURLConnection.java:1238)
     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequestInternal(HttpURLConnection.java:1558)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequest(HttpURLConnection.java:1551)
     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1052)
     at java.net.URLConnection$DefaultContentHandler.getContent(URLConnection.java:1053)
    at java.net.URLConnection.getContent(URLConnection.java:166)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.getContent(HttpsURLConnection.java:156)
   at java.net.URL.getContent(URL.java:621)
    at com.facebook.android.Example.getUserPic(Example.java:192)
    at com.facebook.android.Example$2.onClick(Example.java:174)
     at android.view.View.performClick(View.java:2364)
    at android.view.View.onTouchEvent(View.java:4179)
     at android.widget.TextView.onTouchEvent(TextView.java:6540)
     at android.view.View.dispatchTouchEvent(View.java:3709)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

I must say that when I type the URL in the browser it shows me the correct profile picture.

I tried also to load the URL by doing a http post:

 HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost=new HttpPost(imageURL);

        try{

        HttpResponse response = httpclient.execute(httppost);

        if(response!=null)
        {
        bitmap=BitmapFactory.decodeStream(response.getEntity().getContent());

            }
       }
        catch(ClientProtocolException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

And the logcat looks like this:

Uncaught handler: thread main exiting due to uncaught exception
java.lang.IllegalArgumentException: Host name may not be null
   at org.apache.http.HttpHost.<init>(HttpHost.java:83)
   at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
  at com.facebook.android.Example.getUserPic(Example.java:203)
   at com.facebook.android.Example$2.onClick(Example.java:174)
     at android.view.View.performClick(View.java:2364)
    at android.view.View.onTouchEvent(View.java:4179)
     at android.widget.TextView.onTouchEvent(TextView.java:6540)
     at android.view.View.dispatchTouchEvent(View.java:3709)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)

What's wrong?:-S

Upvotes: 1

Views: 956

Answers (1)

Kenny
Kenny

Reputation: 5542

You would need to get the users friends by making a request to the graph api (https://graph.facebook.com/me/friends?) this will return all the data in a JSON-encoded array.

You can then create a list of all the friends and download the pictures using the ids and the url "http://graph.facebook.com/"+userID+"/picture?type=small"

For an example project whichs shows you how to create a list with images downloaded from a url take a look at my answer to this question here.

Hope this helps to get you started!


Here is the code i use to download the users photo, it will return a bitmap that you can then set in an image view by calling imageView.setImageBitmap(bitmap);

/**
 * Function loads the users facebook profile pic
 * 
 * @param userID
 */
public Bitmap getUserPic(String userID) {
    String imageURL;
    Bitmap bitmap = null;
    Log.d(TAG, "Loading Picture");
    imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
    try {
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
    } catch (Exception e) {
        Log.d(TAG, "Loading Picture FAILED");
        e.printStackTrace();
    }
    return bitmap;
}

Upvotes: 3

Related Questions