2011-11-30 37 views
1

當我做一個個人資料圖片的請求,在這種情況下,我在我的HttpResponseHandler中收到某種編碼的字符串。下面的代碼是對我的個人資料圖片的請求。Facebook的個人資料圖片請求,即「http://graph.facebook.com/me/picture」

private static AsyncHttpClient client = new AsyncHttpClient(); 

client.get("http://graph.facebook.com/1206433/picture", fbPictureHandler); 

下面的代碼是我的處理程序來檢索響應。我以字符串的形式得到響應,但我不確定如何處理這個響應對象。我試圖轉換爲一個字節數組,並寫入「file.jpg」這沒有奏效。我的主要問題是我該如何處理這個響應對象?

private static AsyncHttpResponseHandler fbPictureHandler = new AsyncHttpResponseHandler()   
{ 
@Override 
public void onStart() { 
Log.d(TAG,"started picture handler"); 
} 
@Override 
public void onSuccess(String response) { 
     //Not sure what to do here, have been unable to do anything with this Byte //array 
    byte[] imageBackground = response.getBytes(); 

    } 
    @Override 
    public void onFailure(Throwable error) { 
     Log.d(TAG, "unable to retrieve picture"); 
     error.printStackTrace(); 
    } 
    @Override 
    public void onFinish() { 
     Log.d(TAG,"Finished picture handler"); 
    } 
}; 

這是響應對象

11-29 19:42:12.640: D/Yatter Facebook(3551): ÿØÿà��JFIF������������ÿþ��;CREATOR: gd-jpeg  v1.0 (using IJG JPEG v62), quality = 95 

任何幫助的PRINTSTRING是極大的讚賞,希望這可以幫助其他人。

謝謝

回答

2

使用下面的請求而不是你正在發行

http://graph.facebook.com/1206433?fields=picture

這將一個JSON字符串以以下格式返回給你它包含原路徑的一個個人資料圖片。

{ 
    "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/260615_1206433_140418666_q.jpg" 
} 

解析字符串得到「圖片報」的路徑,並用它在你的代碼來獲取圖片。


下面是一個示例請求例如

注:http://profile.ak.fbcdn.net/hprofile-ak-snc4/260615_1206433_140418666_q.jpg通過解析在第一步驟JSON字符串獲得。

WebRequest request = WebRequest.Create("http://profile.ak.fbcdn.net/hprofile-ak-snc4/260615_1206433_140418666_q.jpg"); 
WebResponse response = request.GetResponse(); 
Stream stream = response.GetResponseStream(); 
pictureBox1.Image = Image.FromStream(stream); 

這會將圖像加載到Windows窗體應用程序中的圖片框中。

如果您需要幫助,請讓我知道。

+0

大這實際上幫助,現在我知道如何使用帶有圖片字段的JSONObject來獲取實際的重定向URL。只是爲了讓你知道,雖然我用這個代碼來檢索位圖'位= BitmapFactory.decodeStream((InputStream的)新的URL( \t \t \t \t \t \t「http://graph.facebook.com/"+facebookId+」/picture?type = medium「)。getContent());' – Danuofr

+0

順便說一下,我使用AsyncTask來檢索每個圖像,非常感謝Robin。 – Danuofr

0

可以使用?重定向=假後續 '/圖片' 獲得直接的聯繫

http://graph.facebook.com/+facebookid+/picture?redirect=false 

和響應包含URL靜態鏈接(JSON格式)

{"data":{ 
    "url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-frc1\/t1.0-1\/c126.33.409.409\/s50x50\/551571_4079894629426_190963543_n.jpg","is_silhouette":false} 
} 
相關問題