开发者

How to get link avatar facebook direct by id

I use http://graph.facebook.com/id/picture to get avatar facebook to show ImageView android. But it doesn't return an image. It returns some response headers including a 302 redirect, and a location header.

When I run the link in your browser, it will show directly link.

http://graph.facebook.com/hieu.trankim/picture

Direct Link: http://profile.ak.fbcdn.net/hprofile-ak开发者_StackOverflow-snc4/203317_1661930338_3279866_q.jpg

I want to get direct link. Please help me.


302 redirects provide the new location as the Location: header. You can make the HttpURLConnection class not follow redirects by: HttpURLConnection.setFollowRedirects(false);

Thus the direct link can be obtained by:

String address = "http://graph.facebook.com/hieu.trankim/picture";
URL url = new URL(address);
HttpURLConnection.setFollowRedirects(false); //Do _not_ follow redirects!
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String newLocation = connection.getHeaderField("Location");

(Exceptions omitted for clarity)

The redirection link will then be stored in the newLocation variable. Don't forget to change setFollowRedirects(true) afterwards in case you want to connect to a site and follow them again. (or use the setInstanceFollowRedirects method).

Source: HttpURLConnection JavaDocs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜