使用Facebook的SDK 3.0的Android检索资料图片(Retrieve profile

2019-08-03 07:08发布

我已经与Facebook SDK 3.0的Android以下问题。 我想拿到我(和我的朋友)资料照片上没有使用他们的ProfilePictureView部件,所以如果我使用图形浏览器我看到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"}}

有人能帮助我吗? 谢谢

Cromir

Answer 1:

一个简单的方法是执行GET请求graph.facebook.com/USER_ID/picture所以你不必第一个请求的URL的图片,然后执行另一个GET请求,从给定的URL下载的图片。

而不是使用Request.executeGraphPathRequestAsync ,只是做一个正常的GET请求,上面的网址,例如http://graph.facebook.com/4/picture



Answer 2:

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


Answer 3:

您可以通过请求图形API获得的个人资料图片。 你需要传递的accessToken,用户ID,并设定了重定向错误。 然后图形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();


Answer 4:

您可能需要启用“图片作为字典”上的开发者控制台您的应用程序的高级设置在https://developers.facebook.com/apps



Answer 5:

据我所知,这是不可能得到像直接使用Facebook的图形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;
}


Answer 6:

我得到的答案......这是因为重定向。 因此通过params代替null

Bundle params = new Bundle();
params.putBoolean("redirect", false);


文章来源: Retrieve profile picture using Facebook SDK 3.0 for Android