Search in sources :

Example 46 with GraphRequest

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

the class AppEventsLogger method updateUserProperties.

public static void updateUserProperties(final Bundle parameters, final String applicationID, final GraphRequest.Callback callback) {
    final String userID = getUserID();
    if (userID == null || userID.isEmpty()) {
        Logger.log(LoggingBehavior.APP_EVENTS, TAG, "AppEventsLogger userID cannot be null or empty");
        return;
    }
    getAnalyticsExecutor().execute(new Runnable() {

        @Override
        public void run() {
            Bundle userPropertiesParams = new Bundle();
            userPropertiesParams.putString("user_unique_id", userID);
            userPropertiesParams.putBundle("custom_data", parameters);
            // This call must be run on the background thread
            AttributionIdentifiers identifiers = AttributionIdentifiers.getAttributionIdentifiers(FacebookSdk.getApplicationContext());
            if (identifiers != null && identifiers.getAndroidAdvertiserId() != null) {
                userPropertiesParams.putString("advertiser_id", identifiers.getAndroidAdvertiserId());
            }
            Bundle data = new Bundle();
            try {
                JSONObject userData = BundleJSONConverter.convertToJSON(userPropertiesParams);
                JSONArray dataArray = new JSONArray();
                dataArray.put(userData);
                data.putString("data", dataArray.toString());
            } catch (JSONException ex) {
                throw new FacebookException("Failed to construct request", ex);
            }
            GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), String.format(Locale.US, "%s/user_properties", applicationID), data, HttpMethod.POST, callback);
            request.setSkipClientToken(true);
            request.executeAsync();
        }
    });
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) AttributionIdentifiers(com.facebook.internal.AttributionIdentifiers) Bundle(android.os.Bundle) FacebookException(com.facebook.FacebookException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 47 with GraphRequest

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

the class FetchedAppSettingsManager method getAppSettingsQueryResponse.

// Note that this method makes a synchronous Graph API call, so should not be called from the
// main thread.
private static JSONObject getAppSettingsQueryResponse(String applicationId) {
    Bundle appSettingsParams = new Bundle();
    appSettingsParams.putString(APPLICATION_FIELDS, TextUtils.join(",", APP_SETTING_FIELDS));
    GraphRequest request = GraphRequest.newGraphPathRequest(null, applicationId, null);
    request.setSkipClientToken(true);
    request.setParameters(appSettingsParams);
    return request.executeAndWait().getJSONObject();
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle)

Example 48 with GraphRequest

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

the class ShareGraphRequest method createOpenGraphObject.

/**
 * Create an User Owned Open Graph object
 *
 * Use this method to create an open graph object, which can then be posted utilizing the same
 * GraphRequest methods as other GraphRequests.
 *
 * @param openGraphObject The open graph object to create. Only SharePhotos with the imageUrl
 *                        set are accepted through this helper method.
 * @return GraphRequest for creating the given openGraphObject
 * @throws FacebookException thrown in the case of a JSONException or in the case of invalid
 *                           format for SharePhoto (missing imageUrl)
 */
public static GraphRequest createOpenGraphObject(final ShareOpenGraphObject openGraphObject) throws FacebookException {
    String type = openGraphObject.getString("type");
    if (type == null) {
        type = openGraphObject.getString("og:type");
    }
    if (type == null) {
        throw new FacebookException("Open graph object type cannot be null");
    }
    try {
        JSONObject stagedObject = (JSONObject) OpenGraphJSONUtility.toJSONValue(openGraphObject, new OpenGraphJSONUtility.PhotoJSONProcessor() {

            @Override
            public JSONObject toJSONObject(SharePhoto photo) {
                Uri photoUri = photo.getImageUrl();
                JSONObject photoJSONObject = new JSONObject();
                try {
                    photoJSONObject.put(NativeProtocol.IMAGE_URL_KEY, photoUri.toString());
                } catch (Exception e) {
                    throw new FacebookException("Unable to attach images", e);
                }
                return photoJSONObject;
            }
        });
        String ogType = type;
        Bundle parameters = new Bundle();
        parameters.putString("object", stagedObject.toString());
        String graphPath = String.format(Locale.ROOT, "%s/%s", "me", "objects/" + ogType);
        return new GraphRequest(AccessToken.getCurrentAccessToken(), graphPath, parameters, HttpMethod.POST);
    } catch (JSONException e) {
        throw new FacebookException(e.getMessage());
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) SharePhoto(com.facebook.share.model.SharePhoto) FacebookException(com.facebook.FacebookException) Bundle(android.os.Bundle) JSONException(org.json.JSONException) Uri(android.net.Uri) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException)

Example 49 with GraphRequest

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

the class FacebookAppLinkResolver method getAppLinkFromUrlsInBackground.

/**
 * Asynchronously resolves App Link data for multiple URLs
 *
 * @param uris A list of Uri objects to resolve into App Links
 * @return A Task that, when successful, will return a Map of Uri->AppLink for each Uri that was
 * successfully resolved into an App Link. Uris that could not be resolved into App Links will
 * not be present in the Map. In the case of general server errors, the task will be completed
 * with the corresponding error.
 */
public Task<Map<Uri, AppLink>> getAppLinkFromUrlsInBackground(List<Uri> uris) {
    final Map<Uri, AppLink> appLinkResults = new HashMap<Uri, AppLink>();
    final HashSet<Uri> urisToRequest = new HashSet<Uri>();
    StringBuilder graphRequestFields = new StringBuilder();
    for (Uri uri : uris) {
        AppLink appLink;
        synchronized (cachedAppLinks) {
            appLink = cachedAppLinks.get(uri);
        }
        if (appLink != null) {
            appLinkResults.put(uri, appLink);
        } else {
            if (!urisToRequest.isEmpty()) {
                graphRequestFields.append(',');
            }
            graphRequestFields.append(uri.toString());
            urisToRequest.add(uri);
        }
    }
    if (urisToRequest.isEmpty()) {
        return Task.forResult(appLinkResults);
    }
    final Task<Map<Uri, AppLink>>.TaskCompletionSource taskCompletionSource = Task.create();
    Bundle appLinkRequestParameters = new Bundle();
    appLinkRequestParameters.putString("ids", graphRequestFields.toString());
    appLinkRequestParameters.putString("fields", String.format("%s.fields(%s,%s)", APP_LINK_KEY, APP_LINK_ANDROID_TARGET_KEY, APP_LINK_WEB_TARGET_KEY));
    GraphRequest appLinkRequest = new GraphRequest(// token
    AccessToken.getCurrentAccessToken(), /* Access Token */
    "", /* Graph path */
    appLinkRequestParameters, /* Query parameters */
    null, /* HttpMethod */
    new GraphRequest.Callback() {

        /* Callback */
        @Override
        public void onCompleted(GraphResponse response) {
            FacebookRequestError error = response.getError();
            if (error != null) {
                taskCompletionSource.setError(error.getException());
                return;
            }
            JSONObject responseJson = response.getJSONObject();
            if (responseJson == null) {
                taskCompletionSource.setResult(appLinkResults);
                return;
            }
            for (Uri uri : urisToRequest) {
                String uriString = uri.toString();
                if (!responseJson.has(uriString)) {
                    continue;
                }
                JSONObject urlData;
                try {
                    urlData = responseJson.getJSONObject(uri.toString());
                    JSONObject appLinkData = urlData.getJSONObject(APP_LINK_KEY);
                    JSONArray rawTargets = appLinkData.getJSONArray(APP_LINK_ANDROID_TARGET_KEY);
                    int targetsCount = rawTargets.length();
                    List<AppLink.Target> targets = new ArrayList<AppLink.Target>(targetsCount);
                    for (int i = 0; i < targetsCount; i++) {
                        AppLink.Target target = getAndroidTargetFromJson(rawTargets.getJSONObject(i));
                        if (target != null) {
                            targets.add(target);
                        }
                    }
                    Uri webFallbackUrl = getWebFallbackUriFromJson(uri, appLinkData);
                    AppLink appLink = new AppLink(uri, targets, webFallbackUrl);
                    appLinkResults.put(uri, appLink);
                    synchronized (cachedAppLinks) {
                        cachedAppLinks.put(uri, appLink);
                    }
                } catch (JSONException e) {
                    // The data for this uri was missing or badly formed.
                    continue;
                }
            }
            taskCompletionSource.setResult(appLinkResults);
        }
    });
    appLinkRequest.executeAsync();
    return taskCompletionSource.getTask();
}
Also used : Task(bolts.Task) GraphRequest(com.facebook.GraphRequest) HashMap(java.util.HashMap) Bundle(android.os.Bundle) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Uri(android.net.Uri) FacebookRequestError(com.facebook.FacebookRequestError) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) AppLink(bolts.AppLink) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 50 with GraphRequest

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

the class PickerFragment method loadData.

/**
 * Causes the picker to load data from the service and display it to the user.
 *
 * @param forceReload if true, data will be loaded even if there is already data being displayed (or loading);
 *                    if false, data will not be re-loaded if it is already displayed (or loading)
 * @param selectIds ids to select, if they are present in the loaded data
 */
public void loadData(boolean forceReload, Set<String> selectIds) {
    if (!forceReload && loadingStrategy.isDataPresentOrLoading()) {
        return;
    }
    selectionHint = selectIds;
    clearResults();
    GraphRequest request = getRequestForLoadData();
    if (request != null) {
        onLoadingData();
        loadingStrategy.startLoading(request);
    }
}
Also used : GraphRequest(com.facebook.GraphRequest)

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