15

我遇到了适用于 Android 的 Facebook SDK 3.0 的问题。我想在不使用他们的 ProfilePictureView 小部件的情况下获取我的(和我的朋友)个人资料图片,所以如果我使用 Graph Explorer,我会看到 Json 响应是:

{
   "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"}}

有人可以帮助我吗?谢谢

克罗米尔

4

6 回答 6

10

一种更简单的方法是执行一个GET请求,graph.facebook.com/USER_ID/picture这样您就不必首先请求图片的 URL,然后执行另一个GET请求以从给定的 URL 下载图片。

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

于 2012-11-28T18:32:08.237 回答
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;
}
于 2013-07-13T20:30:02.837 回答
2

您可以通过向 Graph API 请求获取头像。您需要传递 accessToken 、用户 ID 并将重定向设置为 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();
于 2015-10-29T10:36:12.687 回答
1

据我所知,无法直接使用 facebook graph API 获取图像,因为他们的 API 旨在仅接受 JSON 响应。他们允许一个导致非 JSON 响应的请求似乎很奇怪,尤其奇怪的是他们会将这个请求放在他们的官方文档中 *( https://developers.facebook.com/docs/graph-api/reference /用户/图片/)。

我正在使用内置的 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;
}
于 2014-03-26T18:12:50.233 回答
0

我得到了答案......这是由于重定向。因此通过params而不是null

Bundle params = new Bundle();
params.putBoolean("redirect", false);
于 2016-02-11T10:08:31.203 回答