Search in sources :

Example 31 with GraphRequest

use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.

the class ShareInternalUtility method newUploadStagingResourceWithImageRequest.

/**
 * Creates a new Request configured to upload an image to create a staging resource. Staging
 * resources allow you to post binary data such as images, in preparation for a post of an Open
 * Graph object or action which references the image. The URI returned when uploading a staging
 * resource may be passed as the image property for an Open Graph object or action.
 *
 * @param accessToken the access token to use, or null
 * @param image       the image to upload
 * @param callback    a callback that will be called when the request is completed to handle
 *                    success or error conditions
 * @return a Request that is ready to execute
 */
public static GraphRequest newUploadStagingResourceWithImageRequest(AccessToken accessToken, Bitmap image, Callback callback) {
    Bundle parameters = new Bundle(1);
    parameters.putParcelable(STAGING_PARAM, image);
    return new GraphRequest(accessToken, MY_STAGING_RESOURCES, parameters, HttpMethod.POST, callback);
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle)

Example 32 with GraphRequest

use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.

the class ShareInternalUtility method newUploadStagingResourceWithImageRequest.

/**
 * Creates a new Request configured to upload an image to create a staging resource. Staging
 * resources allow you to post binary data such as images, in preparation for a post of an Open
 * Graph object or action which references the image. The URI returned when uploading a staging
 * resource may be passed as the image property for an Open Graph object or action.
 *
 * @param accessToken the access token to use, or null
 * @param imageUri    the file:// or content:// Uri pointing to the image to upload
 * @param callback    a callback that will be called when the request is completed to handle
 *                    success or error conditions
 * @return a Request that is ready to execute
 * @throws FileNotFoundException
 */
public static GraphRequest newUploadStagingResourceWithImageRequest(AccessToken accessToken, Uri imageUri, Callback callback) throws FileNotFoundException {
    if (Utility.isFileUri(imageUri)) {
        return newUploadStagingResourceWithImageRequest(accessToken, new File(imageUri.getPath()), callback);
    } else if (!Utility.isContentUri(imageUri)) {
        throw new FacebookException("The image Uri must be either a file:// or content:// Uri");
    }
    GraphRequest.ParcelableResourceWithMimeType<Uri> resourceWithMimeType = new GraphRequest.ParcelableResourceWithMimeType<>(imageUri, "image/png");
    Bundle parameters = new Bundle(1);
    parameters.putParcelable(STAGING_PARAM, resourceWithMimeType);
    return new GraphRequest(accessToken, MY_STAGING_RESOURCES, parameters, HttpMethod.POST, callback);
}
Also used : GraphRequest(com.facebook.GraphRequest) FacebookException(com.facebook.FacebookException) Bundle(android.os.Bundle) File(java.io.File) Uri(android.net.Uri)

Example 33 with GraphRequest

use of com.facebook.GraphRequest 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);
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FacebookRequestError(com.facebook.FacebookRequestError) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException) CollectionMapper(com.facebook.internal.CollectionMapper) JSONObject(org.json.JSONObject) FacebookGraphResponseException(com.facebook.FacebookGraphResponseException)

Example 34 with GraphRequest

use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.

the class ShareApi method shareLinkContent.

private void shareLinkContent(final ShareLinkContent linkContent, final FacebookCallback<Sharer.Result> callback) {
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject data = response.getJSONObject();
            final String postId = (data == null ? null : data.optString("id"));
            ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
        }
    };
    final Bundle parameters = new Bundle();
    this.addCommonParameters(parameters, linkContent);
    parameters.putString("message", this.getMessage());
    parameters.putString("link", Utility.getUriString(linkContent.getContentUrl()));
    parameters.putString("picture", Utility.getUriString(linkContent.getImageUrl()));
    parameters.putString("name", linkContent.getContentTitle());
    parameters.putString("description", linkContent.getContentDescription());
    parameters.putString("ref", linkContent.getRef());
    new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath("feed"), parameters, HttpMethod.POST, requestCallback).executeAsync();
}
Also used : GraphRequest(com.facebook.GraphRequest) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle)

Example 35 with GraphRequest

use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.

the class ShareApi method shareOpenGraphContent.

private void shareOpenGraphContent(final ShareOpenGraphContent openGraphContent, final FacebookCallback<Sharer.Result> callback) {
    // In order to create a new Open Graph action using a custom object that does not already
    // exist (objectID or URL), you must first send a request to post the object and then
    // another to post the action.  If a local image is supplied with the object or action, that
    // must be staged first and then referenced by the staging URL that is returned by that
    // request.
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject data = response.getJSONObject();
            final String postId = (data == null ? null : data.optString("id"));
            ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
        }
    };
    final ShareOpenGraphAction action = openGraphContent.getAction();
    final Bundle parameters = action.getBundle();
    this.addCommonParameters(parameters, openGraphContent);
    if (!Utility.isNullOrEmpty(this.getMessage())) {
        parameters.putString("message", this.getMessage());
    }
    final CollectionMapper.OnMapperCompleteListener stageCallback = new CollectionMapper.OnMapperCompleteListener() {

        @Override
        public void onComplete() {
            try {
                handleImagesOnAction(parameters);
                new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath(URLEncoder.encode(action.getActionType(), DEFAULT_CHARSET)), parameters, HttpMethod.POST, requestCallback).executeAsync();
            } catch (final UnsupportedEncodingException ex) {
                ShareInternalUtility.invokeCallbackWithException(callback, ex);
            }
        }

        @Override
        public void onError(FacebookException exception) {
            ShareInternalUtility.invokeCallbackWithException(callback, exception);
        }
    };
    this.stageOpenGraphAction(parameters, stageCallback);
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException) CollectionMapper(com.facebook.internal.CollectionMapper)

Aggregations

GraphRequest (com.facebook.GraphRequest)52 Bundle (android.os.Bundle)39 GraphResponse (com.facebook.GraphResponse)25 JSONObject (org.json.JSONObject)22 FacebookException (com.facebook.FacebookException)15 JSONException (org.json.JSONException)15 FacebookRequestError (com.facebook.FacebookRequestError)8 AccessToken (com.facebook.AccessToken)7 FacebookCallback (com.facebook.FacebookCallback)5 Uri (android.net.Uri)4 JSONArray (org.json.JSONArray)4 Test (org.junit.Test)3 Intent (android.content.Intent)2 Location (android.location.Location)2 GraphRequestBatch (com.facebook.GraphRequestBatch)2 Profile (com.facebook.Profile)2 CollectionMapper (com.facebook.internal.CollectionMapper)2 CurrentPlaceFeedbackRequestParams (com.facebook.places.model.CurrentPlaceFeedbackRequestParams)2 PlaceInfoRequestParams (com.facebook.places.model.PlaceInfoRequestParams)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2