Android Facebook SDK 4.4.1 - ShareApi.share(...) 发出 3 个请求
Android Facebook SDK 4.4.1 - ShareApi.share(...) makes 3 requests
今天我将我们的 Android Facebook SDK 更新到最新版本 (4.4.1)。我现在看到一些奇怪的行为。
当我打电话时:
ShareApi.share(contentForSharing(), shareCallback);
我看到三个请求。第一个成功:
[{Response: responseCode: 200, graphObject: {"id":"552185138255521"}, error: null}]
第二次失败:
[{Response: responseCode: 500, graphObject: null, error: {HttpStatus: 500, errorCode: 100, errorType: FacebookApiException, errorMessage: Invalid parameter}}]
第三次成功:
[{Response: responseCode: 200, graphObject: {"success":true}, error: null}]
此外,图表故事出现在我的 Activity 日志中,显示完全符合预期,但没有出现在我的个人资料或主页供稿页面上。
还有其他人 运行 参与其中吗?
这是我的发布代码:
private void publishToFacebook() {
ShareApi.share(contentForSharing(), shareCallback);
}
...内容构建者:
private ShareOpenGraphContent contentForSharing() {
String previewPropertyName = "MyApp:MyObject";
ShareOpenGraphAction.Builder actionBuilder = new ShareOpenGraphAction.Builder().setActionType("MyApp:MyAction");
ShareOpenGraphObject fbObject = buildGraphObject();
actionBuilder.putObject(previewPropertyName, fbObject);
actionBuilder.putString("fb:explicitly_shared", "true");
ShareOpenGraphContent.Builder contentBuilder =
new ShareOpenGraphContent.Builder()
.setAction(actionBuilder.build())
.setPreviewPropertyName(previewPropertyName);
return contentBuilder.build();
}
...对象生成器
private ShareOpenGraphObject buildGraphObject() {
if (mLocalObject!=null) {
Object[] urlArgs =
new Object[] {
"MyApp:MyObject",
mLocalObject.getScore(),
mLocalObject.getTitle(),
mLocalObject.getDescription(),
mLocalObject.getSummary() };
String url = String.format("http://www.myappwebsite.com/obj.php?"+
"fb:app_id=xxxxxxxxxxxx&og:type=%s"+
"&MyApp:score=%s"+
"&og:title=%s"+
"&og:description=%s"+
"&og:image=http://www.myappwebsite.com/images/icon.png"+
"&body=%s", urlArgs);
ShareOpenGraphObject obj = new ShareOpenGraphObject.Builder()
.putString("og:type", "MyApp:MyObject")
.putString("og:image", "http://www.myappwebsite.com/images/icon.png")
.putString("og:image:width", "100")
.putString("og:image:height", "100")
.putString("og:title", mLocalObject.getTitle())
.putString("MyApp:score", mLocalObject.getScore())
.putString("og:description", mLocalObject.getDescription())
.putString("og:url", url).build();
return obj;
}
return null;
}
...响应回调:
private FacebookCallback<Sharer.Result> shareCallback =
new FacebookCallback<Sharer.Result>() {
@Override
public void onCancel() {
dismissProgressDialog();
}
@Override
public void onError(FacebookException error) {
if (error instanceof FacebookGraphResponseException) {
Timber.d("an error occurred posting to facebook");
FacebookGraphResponseException graphError =
(FacebookGraphResponseException) error;
if (graphError.getGraphResponse() != null) {
handleFacebookError(graphError.getGraphResponse());
return;
}
}
dismissProgressDialog();
}
@Override
public void onSuccess(Sharer.Result result) {
if (getActivity() == null) {
Timber.d("ACTIVITY IS NULL");
// if the user removes the app from the website,
// then a request will have caused the session to
// close (since the token is no longer valid),
// which means the splash fragment will be shown
// rather than this one, causing activity to be null.
// If the activity is null, then we cannot
// show any dialogs, so we return.
return;
}
dismissProgressDialog();
Toast.makeText(getActivity(), getResources().getString(R.string.success), Toast.LENGTH_SHORT).show();
getActivity().finish();
}
};
...最后是错误处理程序
private void handleFacebookError(GraphResponse response) {
FacebookRequestError error = null;
if (response!=null) {
error = response.getError();
}
DialogInterface.OnClickListener listener = null;
String dialogBody = null;
if (error == null) {
dialogBody = getString(R.string.error_dialog_default_text);
} else {
switch (error.getCategory()) {
case LOGIN_RECOVERABLE:
// There is a login issue that can be resolved by the LoginManager.
LoginManager.getInstance().resolveError(this, response);
return;
case TRANSIENT:
dialogBody = getString(R.string.fb_error_transient);
break;
case OTHER:
default:
// an unknown issue occurred, this could be a code error, or
// a server side issue, log the issue, and either ask the
// user to retry, or file a bug
dialogBody = getString(R.string.fb_error_unknown, error.getErrorMessage());
break;
}
}
String title = error.getErrorUserTitle();
String message = error.getErrorUserMessage();
if (message == null) {
message = dialogBody;
}
if (title == null) {
title = getResources().getString(R.string.fb_error_dialog_title);
}
new AlertDialog.Builder(getActivity())
.setPositiveButton(R.string.fb_error_dialog_button_text, listener)
.setTitle(title)
.setMessage(message)
.show();
}
请注意,我使用的所有参数均已批准且有效(即 fb:explicitly_shared,消息),并且我已检查我的发布方法在整个过程中仅被调用一次。
我成功了;我不是很懂,但是我懂了。
请注意,在 'contentBuilder' 函数中,我将对象声明为 "MyApp:MyObject",这是在我们的 iOS 应用程序中声明图形对象的方式。好吧,这在 Android 中不起作用。该对象仅使用 "MyObject" 声明...同样,我真的不明白这是为什么,但现在它正在工作。
今天我将我们的 Android Facebook SDK 更新到最新版本 (4.4.1)。我现在看到一些奇怪的行为。
当我打电话时:
ShareApi.share(contentForSharing(), shareCallback);
我看到三个请求。第一个成功:
[{Response: responseCode: 200, graphObject: {"id":"552185138255521"}, error: null}]
第二次失败:
[{Response: responseCode: 500, graphObject: null, error: {HttpStatus: 500, errorCode: 100, errorType: FacebookApiException, errorMessage: Invalid parameter}}]
第三次成功:
[{Response: responseCode: 200, graphObject: {"success":true}, error: null}]
此外,图表故事出现在我的 Activity 日志中,显示完全符合预期,但没有出现在我的个人资料或主页供稿页面上。
还有其他人 运行 参与其中吗?
这是我的发布代码:
private void publishToFacebook() {
ShareApi.share(contentForSharing(), shareCallback);
}
...内容构建者:
private ShareOpenGraphContent contentForSharing() {
String previewPropertyName = "MyApp:MyObject";
ShareOpenGraphAction.Builder actionBuilder = new ShareOpenGraphAction.Builder().setActionType("MyApp:MyAction");
ShareOpenGraphObject fbObject = buildGraphObject();
actionBuilder.putObject(previewPropertyName, fbObject);
actionBuilder.putString("fb:explicitly_shared", "true");
ShareOpenGraphContent.Builder contentBuilder =
new ShareOpenGraphContent.Builder()
.setAction(actionBuilder.build())
.setPreviewPropertyName(previewPropertyName);
return contentBuilder.build();
}
...对象生成器
private ShareOpenGraphObject buildGraphObject() {
if (mLocalObject!=null) {
Object[] urlArgs =
new Object[] {
"MyApp:MyObject",
mLocalObject.getScore(),
mLocalObject.getTitle(),
mLocalObject.getDescription(),
mLocalObject.getSummary() };
String url = String.format("http://www.myappwebsite.com/obj.php?"+
"fb:app_id=xxxxxxxxxxxx&og:type=%s"+
"&MyApp:score=%s"+
"&og:title=%s"+
"&og:description=%s"+
"&og:image=http://www.myappwebsite.com/images/icon.png"+
"&body=%s", urlArgs);
ShareOpenGraphObject obj = new ShareOpenGraphObject.Builder()
.putString("og:type", "MyApp:MyObject")
.putString("og:image", "http://www.myappwebsite.com/images/icon.png")
.putString("og:image:width", "100")
.putString("og:image:height", "100")
.putString("og:title", mLocalObject.getTitle())
.putString("MyApp:score", mLocalObject.getScore())
.putString("og:description", mLocalObject.getDescription())
.putString("og:url", url).build();
return obj;
}
return null;
}
...响应回调:
private FacebookCallback<Sharer.Result> shareCallback =
new FacebookCallback<Sharer.Result>() {
@Override
public void onCancel() {
dismissProgressDialog();
}
@Override
public void onError(FacebookException error) {
if (error instanceof FacebookGraphResponseException) {
Timber.d("an error occurred posting to facebook");
FacebookGraphResponseException graphError =
(FacebookGraphResponseException) error;
if (graphError.getGraphResponse() != null) {
handleFacebookError(graphError.getGraphResponse());
return;
}
}
dismissProgressDialog();
}
@Override
public void onSuccess(Sharer.Result result) {
if (getActivity() == null) {
Timber.d("ACTIVITY IS NULL");
// if the user removes the app from the website,
// then a request will have caused the session to
// close (since the token is no longer valid),
// which means the splash fragment will be shown
// rather than this one, causing activity to be null.
// If the activity is null, then we cannot
// show any dialogs, so we return.
return;
}
dismissProgressDialog();
Toast.makeText(getActivity(), getResources().getString(R.string.success), Toast.LENGTH_SHORT).show();
getActivity().finish();
}
};
...最后是错误处理程序
private void handleFacebookError(GraphResponse response) {
FacebookRequestError error = null;
if (response!=null) {
error = response.getError();
}
DialogInterface.OnClickListener listener = null;
String dialogBody = null;
if (error == null) {
dialogBody = getString(R.string.error_dialog_default_text);
} else {
switch (error.getCategory()) {
case LOGIN_RECOVERABLE:
// There is a login issue that can be resolved by the LoginManager.
LoginManager.getInstance().resolveError(this, response);
return;
case TRANSIENT:
dialogBody = getString(R.string.fb_error_transient);
break;
case OTHER:
default:
// an unknown issue occurred, this could be a code error, or
// a server side issue, log the issue, and either ask the
// user to retry, or file a bug
dialogBody = getString(R.string.fb_error_unknown, error.getErrorMessage());
break;
}
}
String title = error.getErrorUserTitle();
String message = error.getErrorUserMessage();
if (message == null) {
message = dialogBody;
}
if (title == null) {
title = getResources().getString(R.string.fb_error_dialog_title);
}
new AlertDialog.Builder(getActivity())
.setPositiveButton(R.string.fb_error_dialog_button_text, listener)
.setTitle(title)
.setMessage(message)
.show();
}
请注意,我使用的所有参数均已批准且有效(即 fb:explicitly_shared,消息),并且我已检查我的发布方法在整个过程中仅被调用一次。
我成功了;我不是很懂,但是我懂了。
请注意,在 'contentBuilder' 函数中,我将对象声明为 "MyApp:MyObject",这是在我们的 iOS 应用程序中声明图形对象的方式。好吧,这在 Android 中不起作用。该对象仅使用 "MyObject" 声明...同样,我真的不明白这是为什么,但现在它正在工作。