Android和Facebook:如何获取登录用户的图片 [英] Android and Facebook: How to get picture of logged in User

查看:78
本文介绍了Android和Facebook:如何获取登录用户的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序中使用官方的Facebook SDK。用户登录后,我可以得到用户的用户名,如下所示:

  Facebook mFacebook = new Facebook (APP_ID); 
// ...用户登录...
// String jsonUser = mFacebook.request(me / picture); // throws error
String jsonUser = mFacebook.request(me);
JSONObject obj = Util.parseJson(jsonUser);
String facebookId = obj.optString(id);
String name = obj.optString(name);

我也知道我可以使用这些链接访问个人资料图片:

  https://graph.facebook.com/<facebookId>/picture 
https://graph.facebook.com/< facebookId> / picture?type = large

我想用这个代码来填充个人资料图片:

  public static Drawable getPictureForFacebookId(String facebookId){

Drawable picture = null;
InputStream inputStream = null;

try {
inputStream = new URL(https://graph.facebook.com/+ facebookId +/ picture)。openStream();
} catch(Exception e){
e.printStackTrace();
返回null;

}
picture = Drawable.createFromStream(inputStream,facebook-pictures);

返回图片;
}

但它只是不工作。我总是收到以下错误:

  SSL握手失败:SSL库失败,通常是协议错误

我无法解决这个问题。似乎相当复杂(请此处这里)。那么还有什么其他选择可以获得成功登录到我的应用程序的Facebook用户的图片?

解决方案

请求(我 /图片)会抛出一个错误,因为服务器返回一个302(重定向到图像url),并且facebook sdk不处理这个。


I use the official Facebook SDK in my Android Application. After the user logs in, I can get the uid and the name of the facebook user like so:

Facebook mFacebook = new Facebook(APP_ID);
// ... user logs in ...
//String jsonUser = mFacebook.request("me/picture"); // throws error
String jsonUser = mFacebook.request("me");
JSONObject obj = Util.parseJson(jsonUser);
String facebookId = obj.optString("id");
String name = obj.optString("name");

I also know that the I can access the profile picture with those links:

https://graph.facebook.com/<facebookId>/picture 
https://graph.facebook.com/<facebookId>/picture?type=large

I would love to use this code to geht the profile picture:

public static Drawable getPictureForFacebookId(String facebookId) {

    Drawable picture = null;
    InputStream inputStream = null;

    try {
        inputStream = new URL("https://graph.facebook.com/" + facebookId + "/picture").openStream();
    } catch (Exception e) {        
     e.printStackTrace();
     return null;

    }
    picture = Drawable.createFromStream(inputStream, "facebook-pictures");

    return picture;
}

But it just wont work. I always get the following error:

SSL handshake failure: Failure in SSL library, usually a protocol error

And I cant solve this issue. It seems to be rather complicated(look here or here). So what other options are there to get the picture of a facebook user that successfully logged into my application?

解决方案

request("me/picture") throws an error because the server returns a 302 (redirect to the image url) and the facebook sdk does not handle this.

这篇关于Android和Facebook:如何获取登录用户的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆