在android应用程序用户的朋友中检索

时间:2011-07-15 14:41:47

标签: android facebook

我有一个Android应用程序,其中一个已登录的Facebook用户在您的Android应用程序中以类似的方式检索所有他的朋友: http://i55.tinypic.com/wv9vzb.png(这是从iphone中获取的,但我想要类似的东西)。

我使用facebook-sdk将facebook集成到我的应用程序中。

如果有人能指出我正确的方向,我会感激不尽。谢谢!

更新:

我尝试使用您的代码加载网址:

    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();
}

logcat看起来像这样:

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)

我必须说,当我在浏览器中输入网址时,它会显示正确的个人资料图片。

我还尝试通过执行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();
        }

logcat看起来像这样:

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)

出了什么问题?: - S

1 个答案:

答案 0 :(得分:3)

您需要通过向图形api(https://graph.facebook.com/me/friends?)发出请求来获取用户朋友,这将返回JSON编码数组中的所有数据。

然后,您可以使用ID和网址"http://graph.facebook.com/"+userID+"/picture?type=small"

创建所有朋友的列表并下载图片

对于一个示例项目,它向您展示如何使用从网址下载的图片创建列表,请查看我对此问题的回答here.

希望这有助于您入门!


以下是我用来下载用户照片的代码,它会返回一个位图,然后您可以通过调用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;
}