Facebook Android SDK 4.14使用详解(4)


获取用户头像的url在刚才步骤拿不到,必须再开一个异步请求向facebook请求用户头像的url,在这个过程中我们还可以指定我们想要的头像的分辨率,保证拿到的头像清晰,这一步需要用到上一步得到的userId,然后异步获得一个json串,解析出我们想要的头像url,并通过接口将获得结果回调给ui界面,代码如下

/** * 通过facebook的userId,联网异步获取用户的头像url * @param facebookUserId */ public static void getFacebookUserPictureAsync(String facebookUserId, final FacebookUserImageCallback callback) { JLogUtils.i("AlexFB","准备获取用户头像"); if(callback == null || facebookUserId == null || facebookUserId.length()==0)return; Bundle parameters = new Bundle(); parameters.putBoolean("redirect", false); parameters.putString("height", "200"); parameters.putString("type", "normal"); parameters.putString("width", "200"); GraphRequest graphRequest= new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + facebookUserId + "/picture", parameters, HttpMethod.GET, new GraphRequest.Callback() { public void onCompleted(GraphResponse response) { if (response == null) { callback.onFailed("获取用户facebook头像失败"); return; } if (response.getError() != null) { FacebookRequestError facebookRequestError = response.getError(); callback.onFailed("获取用户facebook头像失败2::" + facebookRequestError.getErrorMessage()); return; } JSONObject responseJsonObject = response.getJSONObject(); if (responseJsonObject == null) { callback.onFailed("获取用户facebook头像失败3"); return; } JLogUtils.i("AlexFB", "facebook直接返回的头像信息是" + responseJsonObject.toString()); String avatarUrl = ""; try { JSONObject dataJsonObject = responseJsonObject.getJSONObject("data"); avatarUrl = dataJsonObject.getString("url"); avatarUrl = URLEncoder.encode(avatarUrl, "UTF-8"); JLogUtils.i("AlexFB", "用户头像获取完毕 avatarUrl:" + avatarUrl); } catch (Exception e) { callback.onFailed("获取用户facebook头像失败4"+e.getStackTrace().toString()); } callback.onCompleted(avatarUrl); } } ); JLogUtils.i("AlexFB","version:"+graphRequest.getVersion()+""); graphRequest.executeAsync(); }


UI界面调用代码:

AlxFacebookHelper.getFacebookUserPictureAsync(facebookUserId,new AlxFacebookHelper.FacebookUserImageCallback() { @Override public void onCompleted(String imageUrl) { //成功获取到了头像之后 loginRegisterActivity.facebookUser.setAvatar(imageUrl); //用户的头像和基本信息都获取完毕,现在可以向我们自己的服务器注册facebook用户了 ... } @Override public void onFailed(String reason) { AlxFacebookHelper.signOut();//如果获取失败了,别忘了将整个登录结果回滚 JLogUtils.i("AlexFB",reason); } });



好了,基本的登录过程现在就完成了,下面说一下分享的代码:

因为有了SDK,在使用分享功能呢的时候会在自己的APP上面压一个cardView,相对于旧版SDK将一个Activity直接压在自己APP上来说,这样的用户体验更好一点

Facebook Android SDK 4.14使用详解



下面说一下具体的代码实现:

/** * 通过facebook sdk的默认弹出框进行分享 * @param activity */ public static void showFacebookShareDialog(Activity activity,String link,String description,String caption,String imageUrl,ShareFacebookCallback callback) { if (activity == null) { if(callback!=null)callback.onFailure("activity 为空"); return; } if (!ShareDialog.canShow(ShareLinkContent.class)) { if(callback!=null)callback.onFailure("现在不能分享"); return; } try { ShareDialog shareDialog = new ShareDialog(activity); ShareLinkContent.Builder builder = new ShareLinkContent.Builder(); if(!TextUtils.isEmpty(caption)) builder.setContentTitle(caption); if(!TextUtils.isEmpty(description)) builder.setContentDescription(description); if(!TextUtils.isEmpty(link))builder.setContentUrl(Uri.parse(link)); if(!TextUtils.isEmpty(imageUrl)) builder.setImageUrl(Uri.parse(imageUrl)); ShareLinkContent linkContent= builder.build(); shareDialog.show(linkContent); if(callback!=null)callback.onSuccess(); } catch (Exception ex) { if(callback!=null)callback.onFailure(ex.getStackTrace().toString()); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/aaf8a4aa7de72bf2f0c66df92e0a5e4b.html