2011-07-15 40 views
1

我有Android應用程序,其中一個登錄Facebook用戶檢索您的Android應用程序中的所有他的朋友以類似的方式與此圖片: http://i55.tinypic.com/wv9vzb.png(這是取自iPhone,但我想要類似的東西對此)。在Android應用程序中檢索用戶的朋友

我已經使用facebook-sdk將facebook集成到了我的應用程序中。

如果有人能指出我正確的方向,我會gratefull.Thanks!

UPDATE:

我想與您的代碼加載網址:

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) 

我必須說,當我鍵入URL瀏覽器會顯示正確的個人資料圖片。

我想也做了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

+0

我不太清楚看去在日誌裏是什麼導致了錯誤,我從來沒有「權威期望在索引7」的例外!如果你願意,你可以壓縮這個項目,發佈一個鏈接,我會下載它,看看我能不能解決問題是什麼? – Kenny

+0

我嘗試的例子是facebook-sdk的一個.....我應該壓縮一切...還是隻有我嘗試過這些東西的課程?你想運行這個項目嗎? – adrian

+0

http://dl.transfer.ro/simple-transfer_RO-16jul-7b53ea.zip如果你要運行它:首先按請求按鈕,然後請求配置文件圖片!按鈕,謝謝你! – adrian

回答

3

您需要通過向圖api(https://graph.facebook.com/me/friends?)發出請求來獲取用戶朋友,這將返回JSON編碼數組中的所有數據。

然後,您可以創建的所有好友的列表,並下載使用IDS和URL "http://graph.facebook.com/"+userID+"/picture?type=small"

舉一個例子項目偉馳展示瞭如何創建從URL下載的圖片來看看列表中的圖片在我回答這個問題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; 
} 
+0

好吧,我'我已經成功地返回了朋友的姓名和身份證,但是可以請您在照片部分指導一下。如果我提出這個請求「...... /圖片」,它會返回什麼?感謝你! – adrian

+0

我已經添加了我用來下載用戶照片的代碼,希望對你有所幫助。 – Kenny

+0

我正在面對一個想法的URL問題....當我在瀏覽器中輸入它時...但在代碼中,我不知道什麼錯誤,因爲我得到一個醜陋的錯誤。我可以在這裏發佈logcat並看看它嗎?謝謝 – adrian

相關問題