use of com.facebook.FacebookGraphResponseException in project facebook-android-sdk by facebook.
the class ShareApi method stageOpenGraphObject.
private void stageOpenGraphObject(final ShareOpenGraphObject object, final CollectionMapper.OnMapValueCompleteListener onOpenGraphObjectStagedListener) {
String type = object.getString("type");
if (type == null) {
type = object.getString("og:type");
}
if (type == null) {
onOpenGraphObjectStagedListener.onError(new FacebookException("Open Graph objects must contain a type value."));
return;
}
final JSONObject stagedObject = new JSONObject();
final CollectionMapper.Collection<String> collection = new CollectionMapper.Collection<String>() {
@Override
public Iterator<String> keyIterator() {
return object.keySet().iterator();
}
@Override
public Object get(String key) {
return object.get(key);
}
@Override
public void set(String key, Object value, CollectionMapper.OnErrorListener onErrorListener) {
try {
stagedObject.put(key, value);
} catch (final JSONException ex) {
String message = ex.getLocalizedMessage();
if (message == null) {
message = "Error staging object.";
}
onErrorListener.onError(new FacebookException(message));
}
}
};
final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
final FacebookRequestError error = response.getError();
if (error != null) {
String message = error.getErrorMessage();
if (message == null) {
message = "Error staging Open Graph object.";
}
onOpenGraphObjectStagedListener.onError(new FacebookGraphResponseException(response, message));
return;
}
final JSONObject data = response.getJSONObject();
if (data == null) {
onOpenGraphObjectStagedListener.onError(new FacebookGraphResponseException(response, "Error staging Open Graph object."));
return;
}
final String stagedObjectId = data.optString("id");
if (stagedObjectId == null) {
onOpenGraphObjectStagedListener.onError(new FacebookGraphResponseException(response, "Error staging Open Graph object."));
return;
}
onOpenGraphObjectStagedListener.onComplete(stagedObjectId);
}
};
final String ogType = type;
final CollectionMapper.OnMapperCompleteListener onMapperCompleteListener = new CollectionMapper.OnMapperCompleteListener() {
@Override
public void onComplete() {
final String objectString = stagedObject.toString();
final Bundle parameters = new Bundle();
parameters.putString("object", objectString);
try {
new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath("objects/" + URLEncoder.encode(ogType, DEFAULT_CHARSET)), parameters, HttpMethod.POST, requestCallback).executeAsync();
} catch (final UnsupportedEncodingException ex) {
String message = ex.getLocalizedMessage();
if (message == null) {
message = "Error staging Open Graph object.";
}
onOpenGraphObjectStagedListener.onError(new FacebookException(message));
}
}
@Override
public void onError(FacebookException exception) {
onOpenGraphObjectStagedListener.onError(exception);
}
};
stageCollectionValues(collection, onMapperCompleteListener);
}
use of com.facebook.FacebookGraphResponseException in project facebook-android-sdk by facebook.
the class ShareApi method stagePhoto.
private void stagePhoto(final SharePhoto photo, final CollectionMapper.OnMapValueCompleteListener onPhotoStagedListener) {
final Bitmap bitmap = photo.getBitmap();
final Uri imageUrl = photo.getImageUrl();
if ((bitmap != null) || (imageUrl != null)) {
final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
final FacebookRequestError error = response.getError();
if (error != null) {
String message = error.getErrorMessage();
if (message == null) {
message = "Error staging photo.";
}
onPhotoStagedListener.onError(new FacebookGraphResponseException(response, message));
return;
}
final JSONObject data = response.getJSONObject();
if (data == null) {
onPhotoStagedListener.onError(new FacebookException("Error staging photo."));
return;
}
final String stagedImageUri = data.optString("uri");
if (stagedImageUri == null) {
onPhotoStagedListener.onError(new FacebookException("Error staging photo."));
return;
}
final JSONObject stagedObject = new JSONObject();
try {
stagedObject.put("url", stagedImageUri);
stagedObject.put("user_generated", photo.getUserGenerated());
} catch (final JSONException ex) {
String message = ex.getLocalizedMessage();
if (message == null) {
message = "Error staging photo.";
}
onPhotoStagedListener.onError(new FacebookException(message));
return;
}
onPhotoStagedListener.onComplete(stagedObject);
}
};
if (bitmap != null) {
ShareInternalUtility.newUploadStagingResourceWithImageRequest(AccessToken.getCurrentAccessToken(), bitmap, requestCallback).executeAsync();
} else {
try {
ShareInternalUtility.newUploadStagingResourceWithImageRequest(AccessToken.getCurrentAccessToken(), imageUrl, requestCallback).executeAsync();
} catch (final FileNotFoundException ex) {
String message = ex.getLocalizedMessage();
if (message == null) {
message = "Error staging photo.";
}
onPhotoStagedListener.onError(new FacebookException(message));
}
}
} else {
onPhotoStagedListener.onError(new FacebookException("Photos must have an imageURL or bitmap."));
}
}
Aggregations