Search in sources :

Example 16 with FacebookRequestError

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

the class TestUserManager method createTestAccount.

private JSONObject createTestAccount(List<String> permissions, Mode mode, String uniqueUserTag) {
    Bundle parameters = new Bundle();
    parameters.putString("installed", "true");
    parameters.putString("permissions", getPermissionsString(permissions));
    parameters.putString("access_token", getAppAccessToken());
    // to delete it at the end.
    if (mode == Mode.SHARED) {
        parameters.putString("name", String.format("Shared %s Testuser", getSharedTestAccountIdentifier(permissions, uniqueUserTag)));
    }
    String graphPath = String.format("%s/accounts/test-users", testApplicationId);
    GraphRequest createUserRequest = new GraphRequest(null, graphPath, parameters, HttpMethod.POST);
    GraphResponse response = createUserRequest.executeAndWait();
    FacebookRequestError error = response.getError();
    JSONObject testAccount = response.getJSONObject();
    if (error != null) {
        return null;
    } else {
        assert testAccount != null;
        // it later.
        if (mode == Mode.SHARED) {
            // the create request.
            try {
                testAccount.put("name", parameters.getString("name"));
            } catch (JSONException e) {
                Log.e(LOG_TAG, "Could not set name", e);
            }
            storeTestAccount(testAccount);
        }
        return testAccount;
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle) JSONException(org.json.JSONException) FacebookRequestError(com.facebook.FacebookRequestError)

Example 17 with FacebookRequestError

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

the class AppEventQueue method handleResponse.

private static void handleResponse(final AccessTokenAppIdPair accessTokenAppId, GraphRequest request, GraphResponse response, final SessionEventsState appEvents, FlushStatistics flushState) {
    FacebookRequestError error = response.getError();
    String resultDescription = "Success";
    FlushResult flushResult = FlushResult.SUCCESS;
    if (error != null) {
        final int NO_CONNECTIVITY_ERROR_CODE = -1;
        if (error.getErrorCode() == NO_CONNECTIVITY_ERROR_CODE) {
            resultDescription = "Failed: No Connectivity";
            flushResult = FlushResult.NO_CONNECTIVITY;
        } else {
            resultDescription = String.format("Failed:\n  Response: %s\n  Error %s", response.toString(), error.toString());
            flushResult = FlushResult.SERVER_ERROR;
        }
    }
    if (FacebookSdk.isLoggingBehaviorEnabled(LoggingBehavior.APP_EVENTS)) {
        String eventsJsonString = (String) request.getTag();
        String prettyPrintedEvents;
        try {
            JSONArray jsonArray = new JSONArray(eventsJsonString);
            prettyPrintedEvents = jsonArray.toString(2);
        } catch (JSONException exc) {
            prettyPrintedEvents = "<Can't encode events for debug logging>";
        }
        Logger.log(LoggingBehavior.APP_EVENTS, TAG, "Flush completed\nParams: %s\n  Result: %s\n  Events JSON: %s", request.getGraphObject().toString(), resultDescription, prettyPrintedEvents);
    }
    appEvents.clearInFlightAndStats(error != null);
    if (flushResult == FlushResult.NO_CONNECTIVITY) {
        // We may call this for multiple requests in a batch, which is slightly inefficient
        // since in principle we could call it once for all failed requests, but the impact is
        // likely to be minimal. We don't call this for other server errors, because if an event
        // failed because it was malformed, etc., continually retrying it will cause subsequent
        // events to not be logged either.
        FacebookSdk.getExecutor().execute(new Runnable() {

            @Override
            public void run() {
                AppEventStore.persistEvents(accessTokenAppId, appEvents);
            }
        });
    }
    if (flushResult != FlushResult.SUCCESS) {
        // We assume that connectivity issues are more significant to report than server issues.
        if (flushState.result != FlushResult.NO_CONNECTIVITY) {
            flushState.result = flushResult;
        }
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) FacebookRequestError(com.facebook.FacebookRequestError)

Example 18 with FacebookRequestError

use of com.facebook.FacebookRequestError 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 19 with FacebookRequestError

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

the class GraphObjectPagingLoader method requestCompleted.

private void requestCompleted(GraphResponse response) {
    GraphRequest request = response.getRequest();
    if (request != currentRequest) {
        return;
    }
    loading = false;
    currentRequest = null;
    FacebookRequestError requestError = response.getError();
    FacebookException exception = (requestError == null) ? null : requestError.getException();
    if (response.getJSONObject() == null && exception == null) {
        exception = new FacebookException("GraphObjectPagingLoader received neither a result nor an error.");
    }
    if (exception != null) {
        nextRequest = null;
        if (onErrorListener != null) {
            onErrorListener.onError(exception, this);
        }
    } else {
        addResults(response);
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) FacebookException(com.facebook.FacebookException) FacebookRequestError(com.facebook.FacebookRequestError)

Example 20 with FacebookRequestError

use of com.facebook.FacebookRequestError in project edx-app-android by edx.

the class FacebookProvider method notifyIfError.

/**
 * returns true if there was an error. The callback will be notified
 */
private boolean notifyIfError(Response response, Callback callback) {
    if (response.getError() != null) {
        FacebookRequestError error = response.getError();
        callback.onError(new SocialError(error.getException()));
        return true;
    }
    return false;
}
Also used : FacebookRequestError(com.facebook.FacebookRequestError)

Aggregations

FacebookRequestError (com.facebook.FacebookRequestError)20 JSONObject (org.json.JSONObject)10 GraphRequest (com.facebook.GraphRequest)9 JSONException (org.json.JSONException)9 GraphResponse (com.facebook.GraphResponse)8 Bundle (android.os.Bundle)7 FacebookException (com.facebook.FacebookException)6 Uri (android.net.Uri)3 ArrayList (java.util.ArrayList)3 JSONArray (org.json.JSONArray)3 FacebookCallback (com.facebook.FacebookCallback)2 FacebookGraphResponseException (com.facebook.FacebookGraphResponseException)2 FacebookOperationCanceledException (com.facebook.FacebookOperationCanceledException)2 FacebookServiceException (com.facebook.FacebookServiceException)2 Request (com.facebook.Request)2 GraphUserListCallback (com.facebook.Request.GraphUserListCallback)2 Response (com.facebook.Response)2 Session (com.facebook.Session)2 GraphUser (com.facebook.model.GraphUser)2 List (java.util.List)2