Search in sources :

Example 11 with FacebookRequestError

use of com.facebook.FacebookRequestError in project Klyph by jonathangerbaud.

the class BaseAsyncRequest method handleResponse.

protected void handleResponse(com.facebook.Response response) {
    if (KlyphFlags.LOG_REQUEST_PERFORMANCE) {
        Log.d("TIME " + query, "requestTime = " + (new Date().getTime() - startTime));
        startTime = new Date().getTime();
    }
    if ((response.getError() == null || response.getError().getErrorCode() == -1) && (response.getGraphObject() != null || response.getGraphObjectList() != null)) {
        try {
            if (subQuery.isFQL()) {
                JSONObject jsonResponse = response.getGraphObject().getInnerJSONObject();
                if (subQuery.returnId()) {
                    String id = (String) response.getGraphObject().getProperty("id");
                    doCallBack(id);
                } else if (!subQuery.getHttpMethod().equals(HttpMethod.GET)) {
                    doCallBack(new ArrayList<GraphObject>());
                } else if (subQuery.isMultiQuery()) {
                    JSONArray data = (JSONArray) jsonResponse.getJSONArray("data");
                    new DeserializeTask(subQuery, previousResults, this).execute(data);
                } else {
                    JSONArray data = (JSONArray) jsonResponse.getJSONArray("data");
                    new DeserializeTask2(subQuery, previousResults, this).execute(data);
                }
            } else {
                if (subQuery.returnId()) {
                    String id = (String) response.getGraphObject().getProperty("id");
                    doCallBack(id);
                } else if (!subQuery.isMultiQuery()) {
                    JSONObject jsonResponse = response.getGraphObject().getInnerJSONObject();
                    doCallBack(subQuery.handleResult(jsonResponse));
                } else /*
					 * else
					 * {
					 * JSONArray data = response.getGraphObjectList().getInnerJSONArray();
					 * new DeserializeTask3(subQuery, previousResults, this).execute(data);
					 * }
					 */
                {
                    JSONObject jsonResponse = response.getGraphObject().getInnerJSONObject();
                    JSONObject paging = jsonResponse.optJSONObject("paging");
                    if (paging != null) {
                        JSONObject cursors = paging.optJSONObject("cursors");
                        if (cursors != null) {
                            afterCursor = cursors.optString("after");
                            beforeCursor = cursors.optString("before");
                        }
                        pagingBefore = paging.optString("before");
                        pagingNext = paging.optString("next");
                    }
                    JSONArray data = (JSONArray) jsonResponse.getJSONArray("data");
                    new DeserializeTask2(subQuery, previousResults, this).execute(data);
                }
            }
        } catch (JSONException e) {
            RequestError error = new RequestError(999, "JSONException", e.getMessage());
            doCallBack(error);
        }
    } else {
        longInfo("Error " + response.getError());
        FacebookRequestError fbError = response.getError();
        if (KlyphFlags.ENABLE_BUG_REPORT) {
        /*
				 * if (query == Query.POST_LIKE || query == Query.POST_UNLIKE || query == Query.POST_COMMENT
				 * || query == Query.NOTIFICATIONS)
				 * {
				 * ACRA.getErrorReporter().handleSilentException(fbError.getException());
				 * }
				 */
        }
        RequestError error = new RequestError(fbError.getErrorCode(), fbError.getErrorType(), fbError.getErrorMessage());
        doCallBack(error);
    }
}
Also used : FacebookRequestError(com.facebook.FacebookRequestError) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Date(java.util.Date) FacebookRequestError(com.facebook.FacebookRequestError)

Example 12 with FacebookRequestError

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

the class WebLoginMethodHandler method onComplete.

protected void onComplete(LoginClient.Request request, Bundle values, FacebookException error) {
    LoginClient.Result outcome;
    e2e = null;
    if (values != null) {
        // Actual e2e we got from the dialog should be used for logging.
        if (values.containsKey(ServerProtocol.DIALOG_PARAM_E2E)) {
            e2e = values.getString(ServerProtocol.DIALOG_PARAM_E2E);
        }
        try {
            AccessToken token = createAccessTokenFromWebBundle(request.getPermissions(), values, getTokenSource(), request.getApplicationId());
            outcome = LoginClient.Result.createTokenResult(loginClient.getPendingRequest(), token);
            // Ensure any cookies set by the dialog are saved
            // This is to work around a bug where CookieManager may fail to instantiate if
            // CookieSyncManager has never been created.
            CookieSyncManager syncManager = CookieSyncManager.createInstance(loginClient.getActivity());
            syncManager.sync();
            saveCookieToken(token.getToken());
        } catch (FacebookException ex) {
            outcome = LoginClient.Result.createErrorResult(loginClient.getPendingRequest(), null, ex.getMessage());
        }
    } else {
        if (error instanceof FacebookOperationCanceledException) {
            outcome = LoginClient.Result.createCancelResult(loginClient.getPendingRequest(), "User canceled log in.");
        } else {
            // Something went wrong, don't log a completion event since it will skew timing
            // results.
            e2e = null;
            String errorCode = null;
            String errorMessage = error.getMessage();
            if (error instanceof FacebookServiceException) {
                FacebookRequestError requestError = ((FacebookServiceException) error).getRequestError();
                errorCode = String.format(Locale.ROOT, "%d", requestError.getErrorCode());
                errorMessage = requestError.toString();
            }
            outcome = LoginClient.Result.createErrorResult(loginClient.getPendingRequest(), null, errorMessage, errorCode);
        }
    }
    if (!Utility.isNullOrEmpty(e2e)) {
        logWebLoginCompleted(e2e);
    }
    loginClient.completeAndValidate(outcome);
}
Also used : FacebookOperationCanceledException(com.facebook.FacebookOperationCanceledException) AccessToken(com.facebook.AccessToken) CookieSyncManager(android.webkit.CookieSyncManager) FacebookException(com.facebook.FacebookException) FacebookServiceException(com.facebook.FacebookServiceException) FacebookRequestError(com.facebook.FacebookRequestError)

Example 13 with FacebookRequestError

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

the class DeviceShareDialogFragment method finishActivity.

private void finishActivity(int resultCode, Intent data) {
    if (currentRequestState != null) {
        DeviceRequestsHelper.cleanUpAdvertisementService(currentRequestState.getUserCode());
    }
    FacebookRequestError error = data.getParcelableExtra(EXTRA_ERROR);
    if (error != null) {
        Toast.makeText(getContext(), error.getErrorMessage(), Toast.LENGTH_SHORT).show();
    }
    if (isAdded()) {
        Activity activity = getActivity();
        activity.setResult(resultCode, data);
        activity.finish();
    }
}
Also used : Activity(android.app.Activity) FacebookRequestError(com.facebook.FacebookRequestError)

Example 14 with FacebookRequestError

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

use of com.facebook.FacebookRequestError 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."));
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) Uri(android.net.Uri) FacebookRequestError(com.facebook.FacebookRequestError) Bitmap(android.graphics.Bitmap) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException) FacebookGraphResponseException(com.facebook.FacebookGraphResponseException)

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