0
今天我更新了我們的Android Facebook SDK到最新版本(4.4.1)。我現在看到一些奇怪的行爲。Android Facebook SDK 4.4.1 - ShareApi.share(...)發出3個請求
當我打電話:
ShareApi.share(contentForSharing(), shareCallback);
我看到了三次請求。第一succeedes:
[{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}]
此外,圖的故事出現在我的活動日誌,正好顯示如預期,但不在我的個人資料或主頁上。
有沒有其他人遇到過這個問題?
這裏是我的代碼發佈:
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,消息)的參數,我已經檢查了我的發佈方法在整個過程中只被調用一次。