2012-11-28 156 views
15

我在使用Facebook SDK 3.0 for Android時遇到以下問題。 我想要得到我的(和我的朋友)資料照片上沒有使用他們的ProfilePictureView部件,所以如果我使用圖形瀏覽器我看到JSON響應是:使用Facebook SDK 3.0獲取個人資料圖片Android版

{ 
    "data": { 
     "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/372127_1377011138_1538716206_q.jpg", 
     "is_silhouette": false 
    } 
} 

我需要「URL」路徑來下載並顯示圖片,而是用下面的代碼:

Request.executeGraphPathRequestAsync(Session.getActiveSession(), 
            "me/picture", 
             new Request.Callback() { 
     @Override 
     public void onCompleted(Response response) { 
      GraphObject go = response.getGraphObject(); 
      Log.i("APP_NAME", go.toString());   
     } 
}); 

我獲得此:

GraphObject{graphObjectClass=GraphObject, 
    state={"FACEBOOK_NON_JSON_RESULT":"����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000"}} 

有人能幫助我嗎? 感謝

Cromir

+1

我有同樣的問題。你是如何解決它的? – Geek

+0

@Geek答案是將重定向設置爲false按照http://stackoverflow.com/questions/25055159/android-facebook-sdk-decoding-pictures-from-me-picture-graph-call – kha

回答

1

您可能需要啓用「圖片作爲字典」上的開發者控制檯您的應用程序的高級設置在https://developers.facebook.com/apps

+0

此設置啓用,實際上我獲得了一個Json響應,而不僅僅是url作爲字符串。 –

10

一個更簡單的方法是將執行GET請求graph.facebook.com/USER_ID/picture因此您不必首先請求圖片的網址,然後執行另一個GET請求從給定網址下載圖片。

而不是使用Request.executeGraphPathRequestAsync,只需對上面的URL執行正常的GET請求,例如, http://graph.facebook.com/4/picture

+0

它的工作原理!謝謝!! –

+0

如果我真的想要獲取網址,該怎麼辦? 「圖片爲字典」設置似乎沒有幫助。 –

+3

url始終是http://graph.facebook.com/YOUR_USER_ID/picture –

9
You can retreive user information for executeMeRequest in facebook 3.0 sdk. 

    public void executeMeRequest(Session session) { 

     Bundle bundle = new Bundle(); 
     bundle.putString("fields", "picture"); 
     final Request request = new Request(session, "me", bundle, 
       HttpMethod.GET, new Request.Callback() { 

      @Override 
      public void onCompleted(Response response) { 
       GraphObject graphObject = response.getGraphObject(); 
       if(graphObject != null) { 
        try { 
         JSONObject jsonObject = graphObject.getInnerJSONObject(); 
         JSONObject obj = jsonObject.getJSONObject("picture").getJSONObject("data"); 
         final String url = obj.getString("url"); 
          new Thread(new Runnable() { 

           @Override 
           public void run() { 

            final Bitmap bitmap = BitmapFactory.decodeStream(HttpRequest(url); 
            runOnUiThread(new Runnable() { 

             @Override 
             public void run() { 
              imageView.setImageBitmap(bitmap); 
             } 
            }); 
           } 
          }).start(); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }); 
     Request.executeBatchAsync(request); 
    } 

public static InputStream HttpRequest(String strUrl) { 

    HttpResponse responce = null; 
    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(); 
     request.setURI(new URI(strUrl)); 
     responce = httpClient.execute(request); 
     HttpEntity entity = responce.getEntity(); 
     return entity.getContent(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
+1

這非常有效。我只是不喜歡使用HttpRequest方法;相反,我用InputStream in = new java.net.URL(url).openStream(); final Bitmap bitmap = BitmapFactory.decodeStream(in); – Proverbio

1

據我所知,不可能直接使用facebook圖形API獲取圖像,因爲它們的API被設計爲只接受JSON響應。看起來很奇怪,他們會允許導致非JSON響應的請求,尤其奇怪的是,他們會將該請求放入其官方文檔*(https://developers.facebook.com/docs/graph-api/reference/user/picture/)。

我使用內置的Android DefaultHttpClient庫。

private Bitmap downloadImage(url) { 
    Bitmap image = null; 
    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpGet request = new HttpGet(imageUrl); 
    try { 
     HttpResponse response = client.execute(request); 
     HttpEntity entity = response.getEntity(); 

     int imageLength = (int)(entity.getContentLength()); 

     InputStream is = entity.getContent(); 

     byte[] imageBlob = new byte[imageLength]; 

     int bytesRead = 0; 

     // Pull the image's byte array 

     while (bytesRead < imageLength) { 
      int n = is.read(imageBlob, bytesRead, imageLength - bytesRead); 
      bytesRead= n; 
     } 

     image = BitmapFactory.decodeByteArray(imageBlob, 0, imageBlob.length); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return image; 
} 
2

您可以通過向Graph API請求獲取配置文件圖片。您需要傳遞 accessToken,用戶標識並將重定向設置爲false。然後Graph API返回JSON,並從JSON中獲取url字段,這是用戶配置文件。

GraphRequest request = new GraphRequest(accessToken, "/" + userID + "/picture",null,HttpMethod.GET, new GraphRequest.Callback() { 
       @Override 
       public void onCompleted(GraphResponse response) { 
        Log.d("Photo Profile", response.getJSONObject().toString()); 
        JSONObject jsonObject = response.getJSONObject(); 
        try { 

         JSONObject data = jsonObject.getJSONObject("data"); 
         String url = data.getString("url"); 
         Picasso.with(getApplicationContext()).load(url).into(ivProfilePicture); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      Bundle parameters =new Bundle(); 
      parameters.putString("type","large"); 
      parameters.putBoolean("redirect",false); 
      request.setParameters(parameters); 
      request.executeAsync(); 
0

我得到了答案......這是由於重定向。因此通過params而不是null

Bundle params = new Bundle(); 
params.putBoolean("redirect", false); 
相關問題